Skip to content

Commit e81022a

Browse files
committed
Add sequence message mode for /since/<seqno>
Using `after=seqno` was wrong because `after` gives us messages with ids since the given value, but /since/ is meant to fetch since a given seqno (so that we get edits/deletions).
1 parent 179fa07 commit e81022a

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

sogs/model/room.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ def get_messages_for(
465465
self,
466466
user: Optional[User],
467467
*,
468+
sequence: Optional[int] = None,
468469
after: Optional[int] = None,
469470
before: Optional[int] = None,
470471
recent: bool = False,
@@ -477,7 +478,12 @@ def get_messages_for(
477478
whispers meant to be displayed to moderators.
478479
479480
Exactly one of `after`, `begin`, `recent` or `single` must be specified:
480-
- `after=N` returns messages with ids greater than N in ascending order
481+
- `sequence=N` returns messages that have been posted, edited, or deleted since the given
482+
`seqno` (that is: the have seqno greater than N). Messages are returned in sequence
483+
order.
484+
- `after=N` returns messages with ids greater than N in ascending order. This is normally
485+
*not* what you want for fetching messages as it omits edits and deletions; typically you
486+
want to retrieve by seqno instead.
481487
- `before=N` returns messages with ids less than N in descending order
482488
- `recent=True` returns the most recent messages in descending order
483489
- `single=123` returns a singleton list containing the single message with the given message
@@ -491,11 +497,15 @@ def get_messages_for(
491497
mod = self.check_moderator(user)
492498
msgs = []
493499

494-
opt_count = sum((after is not None, before is not None, recent, single is not None))
500+
opt_count = sum(arg is not None for arg in (sequence, after, before, single)) + bool(recent)
495501
if opt_count == 0:
496-
raise RuntimeError("Exactly one of before=, after=, recent=, or single= is required")
502+
raise RuntimeError(
503+
"Exactly one of sequence=, before=, after=, recent=, or single= is required"
504+
)
497505
if opt_count > 1:
498-
raise RuntimeError("Cannot specify more than one of before=, after=, recent=, single=")
506+
raise RuntimeError(
507+
"Cannot specify more than one of sequence=, before=, after=, recent=, single="
508+
)
499509

500510
# Handle id mapping from an old database import in case the client is requesting
501511
# messages since some id from the old db.
@@ -525,8 +535,9 @@ def get_messages_for(
525535
for row in query(
526536
f"""
527537
SELECT * FROM message_details
528-
WHERE room = :r AND data IS NOT NULL AND NOT filtered
538+
WHERE room = :r AND NOT filtered {'AND data IS NOT NULL' if sequence is None else ''}
529539
{
540+
'AND seqno > :sequence' if sequence is not None else
530541
'AND id > :after' if after is not None else
531542
'AND id < :before' if before is not None else
532543
'AND id = :single' if single is not None else
@@ -535,11 +546,13 @@ def get_messages_for(
535546
AND ({whisper_clause})
536547
{
537548
'' if single is not None else
549+
'ORDER BY seqno ASC LIMIT :limit' if sequence is not None else
538550
'ORDER BY id ASC LIMIT :limit' if after is not None else
539551
'ORDER BY id DESC LIMIT :limit'
540552
}
541553
""",
542554
r=self.id,
555+
sequence=sequence,
543556
after=after,
544557
before=before,
545558
single=single,

sogs/routes/rooms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def messages_since(room, seqno):
9292

9393
limit = utils.get_int_param('limit', 100, min=1, max=256, truncate=True)
9494

95-
return utils.jsonify_with_base64(room.get_messages_for(g.user, limit=limit, after=seqno))
95+
return utils.jsonify_with_base64(room.get_messages_for(g.user, limit=limit, sequence=seqno))
9696

9797

9898
@rooms.get("/room/<Room:room>/messages/before/<int:msg_id>")

0 commit comments

Comments
 (0)