From 831d7b1d8b942110176350f4129fd068c68929ce Mon Sep 17 00:00:00 2001 From: Jacek Kusnierz Date: Sat, 28 May 2022 20:18:33 +0200 Subject: [PATCH 1/6] Don't return end if no more events --- synapse/handlers/pagination.py | 1 - 1 file changed, 1 deletion(-) diff --git a/synapse/handlers/pagination.py b/synapse/handlers/pagination.py index 6f4820c240cc..53d5c8be3ebb 100644 --- a/synapse/handlers/pagination.py +++ b/synapse/handlers/pagination.py @@ -527,7 +527,6 @@ async def get_messages( return { "chunk": [], "start": await from_token.to_string(self.store), - "end": await next_token.to_string(self.store), } state = None From 700e05748d6317d7d75c83726d0fde7cbd322284 Mon Sep 17 00:00:00 2001 From: Jacek Kusnierz Date: Sun, 29 May 2022 12:31:57 +0200 Subject: [PATCH 2/6] Add changelog.d entry --- changelog.d/12903.bugfix | 1 + 1 file changed, 1 insertion(+) 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..01685ef983b2 --- /dev/null +++ b/changelog.d/12903.bugfix @@ -0,0 +1 @@ +Do not return end attribute when there are no more events From 2e99b9d7abab6af99008637f4d34cb03ca568343 Mon Sep 17 00:00:00 2001 From: Jacek Kusnierz Date: Sun, 29 May 2022 21:48:35 +0200 Subject: [PATCH 3/6] add missing dot --- changelog.d/12903.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/12903.bugfix b/changelog.d/12903.bugfix index 01685ef983b2..91973bd8647a 100644 --- a/changelog.d/12903.bugfix +++ b/changelog.d/12903.bugfix @@ -1 +1 @@ -Do not return end attribute when there are no more events +Do not return end attribute when there are no more events. From 09c891e3310c37e1e12b774c065e4e63af874be4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20Ku=C5=9Bnierz?= Date: Mon, 30 May 2022 19:26:02 +0200 Subject: [PATCH 4/6] Update changelog.d/12903.bugfix Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> --- changelog.d/12903.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/12903.bugfix b/changelog.d/12903.bugfix index 91973bd8647a..f26439948371 100644 --- a/changelog.d/12903.bugfix +++ b/changelog.d/12903.bugfix @@ -1 +1 @@ -Do not return end attribute when there are no more events. +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. From a530133957fabbbc8c4a8a4d860b791eab867704 Mon Sep 17 00:00:00 2001 From: Jacek Kusnierz Date: Mon, 30 May 2022 20:18:24 +0200 Subject: [PATCH 5/6] Add no events no end handler back, add some comments for clarification. Signed-off-by: Jacek Kusnierz --- synapse/handlers/pagination.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/synapse/handlers/pagination.py b/synapse/handlers/pagination.py index 53d5c8be3ebb..f3a165bd2b23 100644 --- a/synapse/handlers/pagination.py +++ b/synapse/handlers/pagination.py @@ -515,18 +515,28 @@ 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 + # do not return end - there's 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": [], "start": await from_token.to_string(self.store), + "end": await next_token.to_string(self.store), } state = None From 1e8c19e19d35c1eff20c82ec9fe900614763201d Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Mon, 30 May 2022 19:50:15 +0100 Subject: [PATCH 6/6] Update synapse/handlers/pagination.py --- synapse/handlers/pagination.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/pagination.py b/synapse/handlers/pagination.py index f3a165bd2b23..35afe6b8559e 100644 --- a/synapse/handlers/pagination.py +++ b/synapse/handlers/pagination.py @@ -515,8 +515,10 @@ async def get_messages( next_token = from_token.copy_and_replace(StreamKeyType.ROOM, next_key) - # if no events are returned from pagination - # do not return end - there's no need for further queries + # 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": [],