-
Notifications
You must be signed in to change notification settings - Fork 419
Add log to determine whether clients are using /messages as expected
#19226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MadLittleMods
wants to merge
8
commits into
develop
Choose a base branch
from
madlittlemods/better-messages-endpoint-typing
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+208
−42
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b7b924c
Better typing and separation of concerns for the `/messages` endpoint
MadLittleMods 61ed7e6
Update admin endpoint
MadLittleMods 1b50622
Add log for `/messages` responses
MadLittleMods 085ad0a
Add changelog
MadLittleMods 643aaf5
Fix format string
MadLittleMods 1401a7d
Serialize token for easy ctrl+f in the next request
MadLittleMods 958647b
Fix lint
MadLittleMods b9e2a46
Better explanation of order
MadLittleMods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,22 +21,25 @@ | |
| import logging | ||
| from typing import TYPE_CHECKING, cast | ||
|
|
||
| import attr | ||
|
|
||
| from twisted.python.failure import Failure | ||
|
|
||
| from synapse.api.constants import Direction, EventTypes, Membership | ||
| from synapse.api.errors import SynapseError | ||
| from synapse.api.filtering import Filter | ||
| from synapse.events.utils import SerializeEventConfig | ||
| from synapse.events import EventBase | ||
| from synapse.handlers.relations import BundledAggregations | ||
| from synapse.handlers.worker_lock import NEW_EVENT_DURING_PURGE_LOCK_NAME | ||
| from synapse.logging.opentracing import trace | ||
| from synapse.rest.admin._base import assert_user_is_admin | ||
| from synapse.streams.config import PaginationConfig | ||
| from synapse.types import ( | ||
| JsonDict, | ||
| JsonMapping, | ||
| Requester, | ||
| ScheduledTask, | ||
| StreamKeyType, | ||
| StreamToken, | ||
| TaskStatus, | ||
| ) | ||
| from synapse.types.handlers import ShutdownRoomParams, ShutdownRoomResponse | ||
|
|
@@ -69,6 +72,55 @@ | |
| SHUTDOWN_AND_PURGE_ROOM_ACTION_NAME = "shutdown_and_purge_room" | ||
|
|
||
|
|
||
| @attr.s(slots=True, frozen=True, auto_attribs=True) | ||
| class GetMessagesResult: | ||
| """ | ||
| Everything needed to serialize a `/messages` response. | ||
| """ | ||
|
|
||
| messages_chunk: list[EventBase] | ||
| """ | ||
| A list of room events. | ||
|
|
||
| When the request was `Direction.FORWARDS`, the events are in chronological order. | ||
|
|
||
| When the request was `Direction.BACKWARDS`, the events are in reverse chronological order. | ||
|
|
||
| Note that an empty chunk does not necessarily imply that no more events are | ||
| available. Clients should continue to paginate until no `end_token` property is returned. | ||
| """ | ||
|
|
||
| bundled_aggregations: dict[str, BundledAggregations] | ||
| """ | ||
| A map of event ID to the bundled aggregations for the events in the chunk. | ||
|
|
||
| If an event doesn't have any bundled aggregations, it may not appear in the map. | ||
| """ | ||
|
|
||
| state: list[EventBase] | None | ||
| """ | ||
| A list of state events relevant to showing the chunk. For example, if | ||
| lazy_load_members is enabled in the filter then this may contain the membership | ||
| events for the senders of events in the chunk. | ||
| """ | ||
|
|
||
| start_token: StreamToken | ||
| """ | ||
| Token corresponding to the start of chunk. This will be the same as the value given | ||
| in `from` query parameter of the `/messages` request. | ||
| """ | ||
|
|
||
| end_token: StreamToken | None | ||
| """ | ||
| A token corresponding to the end of chunk. This token can be passed back to this | ||
| endpoint to request further events. | ||
|
|
||
| If no further events are available (either because we have reached the start of the | ||
| timeline, or because the user does not have permission to see any more events), this | ||
| property is omitted from the response. | ||
| """ | ||
|
|
||
|
|
||
| class PaginationHandler: | ||
| """Handles pagination and purge history requests. | ||
|
|
||
|
|
@@ -417,7 +469,7 @@ async def get_messages( | |
| as_client_event: bool = True, | ||
| event_filter: Filter | None = None, | ||
| use_admin_priviledge: bool = False, | ||
| ) -> JsonDict: | ||
| ) -> GetMessagesResult: | ||
| """Get messages in a room. | ||
|
|
||
| Args: | ||
|
|
@@ -616,10 +668,13 @@ async def get_messages( | |
| # 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), | ||
| } | ||
| return GetMessagesResult( | ||
| messages_chunk=[], | ||
| bundled_aggregations={}, | ||
| state=None, | ||
| start_token=from_token, | ||
| end_token=None, | ||
| ) | ||
|
|
||
| if event_filter: | ||
| events = await event_filter.filter(events) | ||
|
|
@@ -635,11 +690,13 @@ async def get_messages( | |
| # 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), | ||
| } | ||
| return GetMessagesResult( | ||
| messages_chunk=[], | ||
| bundled_aggregations={}, | ||
| state=None, | ||
| start_token=from_token, | ||
| end_token=next_token, | ||
| ) | ||
|
|
||
| state = None | ||
| if event_filter and event_filter.lazy_load_members and len(events) > 0: | ||
|
|
@@ -656,38 +713,20 @@ async def get_messages( | |
|
|
||
| if state_ids: | ||
| state_dict = await self.store.get_events(list(state_ids.values())) | ||
| state = state_dict.values() | ||
| state = list(state_dict.values()) | ||
|
|
||
| aggregations = await self._relations_handler.get_bundled_aggregations( | ||
| events, user_id | ||
| ) | ||
|
|
||
| time_now = self.clock.time_msec() | ||
|
|
||
| serialize_options = SerializeEventConfig( | ||
| as_client_event=as_client_event, requester=requester | ||
| return GetMessagesResult( | ||
| messages_chunk=events, | ||
| bundled_aggregations=aggregations, | ||
| state=state, | ||
| start_token=from_token, | ||
| end_token=next_token, | ||
| ) | ||
|
|
||
| chunk = { | ||
| "chunk": ( | ||
| await self._event_serializer.serialize_events( | ||
| events, | ||
| time_now, | ||
| config=serialize_options, | ||
| bundle_aggregations=aggregations, | ||
| ) | ||
| ), | ||
| "start": await from_token.to_string(self.store), | ||
| "end": await next_token.to_string(self.store), | ||
| } | ||
|
|
||
| if state: | ||
| chunk["state"] = await self._event_serializer.serialize_events( | ||
| state, time_now, config=serialize_options | ||
| ) | ||
|
|
||
| return chunk | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better separation of concerns. We now do the serializing/encoding at the REST layer instead of in the handler (see |
||
|
|
||
| async def _shutdown_and_purge_room( | ||
| self, | ||
| task: ScheduledTask, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: we should also state that this field is omitted from the response if it's none, as we do with
end_token.And perhaps especially note that empty lists will be omitted as well.