From c4f548e05d9a1858787d3a0883a5393d315473d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20Ku=C5=9Bnierz?= Date: Mon, 30 May 2022 22:03:52 +0200 Subject: [PATCH] Don't return `end` from `/messages` if there are no more events (#12903) Signed-off-by: Jacek Kusnierz --- changelog.d/12903.bugfix | 1 + synapse/handlers/pagination.py | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 changelog.d/12903.bugfix diff --git a/changelog.d/12903.bugfix b/changelog.d/12903.bugfix new file mode 100644 index 000000000000..f26439948371 --- /dev/null +++ b/changelog.d/12903.bugfix @@ -0,0 +1 @@ +Fix a long-standing bug which caused the `/messages` endpoint to return an incorrect `end` attribute when there were no more events. Contributed by @Vetchu. diff --git a/synapse/handlers/pagination.py b/synapse/handlers/pagination.py index 6f4820c240cc..35afe6b8559e 100644 --- a/synapse/handlers/pagination.py +++ b/synapse/handlers/pagination.py @@ -515,14 +515,25 @@ async def get_messages( next_token = from_token.copy_and_replace(StreamKeyType.ROOM, next_key) - if events: - if event_filter: - events = await event_filter.filter(events) + # if no events are returned from pagination, that implies + # we have reached the end of the available events. + # In that case we do not return end, to tell the client + # there is no need for further queries. + if not events: + return { + "chunk": [], + "start": await from_token.to_string(self.store), + } - events = await filter_events_for_client( - self.storage, user_id, events, is_peeking=(member_event_id is None) - ) + if event_filter: + events = await event_filter.filter(events) + + events = await filter_events_for_client( + self.storage, user_id, events, is_peeking=(member_event_id is None) + ) + # if after the filter applied there are no more events + # return immediately - but there might be more in next_token batch if not events: return { "chunk": [],