From d24ef5a4f4366c30f4aa7f798086780d23612526 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 14 Jan 2022 15:30:17 +0000 Subject: Use a set for _handle_typing and _handle_reciepts as well This wasn't necessary, but mypy complained that 'events' could be both a List and a Set of JsonDict. So rather than define it with a Union type as such, I figured it'd be best to convert the other methods to use Set as well. We may even eliminate other duplicates in the process. --- synapse/handlers/appservice.py | 12 ++++++------ synapse/handlers/receipts.py | 12 ++++++------ synapse/handlers/typing.py | 8 ++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/synapse/handlers/appservice.py b/synapse/handlers/appservice.py index 07cb584395..742d7f0ce3 100644 --- a/synapse/handlers/appservice.py +++ b/synapse/handlers/appservice.py @@ -309,7 +309,7 @@ class ApplicationServicesHandler: async def _handle_typing( self, service: ApplicationService, new_token: int - ) -> List[JsonDict]: + ) -> Set[JsonDict]: """ Return the typing events since the given stream token that the given application service should receive. @@ -323,7 +323,7 @@ class ApplicationServicesHandler: new_token: A typing event stream token. Returns: - A list of JSON dictionaries containing data derived from the typing events that + A set of JSON dictionaries containing data derived from the typing events that should be sent to the given application service. """ typing_source = self.event_sources.sources.typing @@ -344,7 +344,7 @@ class ApplicationServicesHandler: async def _handle_receipts( self, service: ApplicationService, new_token: Optional[int] - ) -> List[JsonDict]: + ) -> Set[JsonDict]: """ Return the latest read receipts that the given application service should receive. @@ -360,7 +360,7 @@ class ApplicationServicesHandler: token. Prevents accidentally duplicating work. Returns: - A list of JSON dictionaries containing data derived from the read receipts that + A set of JSON dictionaries containing data derived from the read receipts that should be sent to the given application service. """ from_key = await self.store.get_type_stream_id_for_appservice( @@ -370,7 +370,7 @@ class ApplicationServicesHandler: logger.debug( "Rejecting token lower than or equal to stored: %s" % (new_token,) ) - return [] + return set() receipts_source = self.event_sources.sources.receipt receipts, _ = await receipts_source.get_new_events_as( @@ -412,7 +412,7 @@ class ApplicationServicesHandler: logger.debug( "Rejecting token lower than or equal to stored: %s" % (new_token,) ) - return [] + return set() for user in users: if isinstance(user, str): diff --git a/synapse/handlers/receipts.py b/synapse/handlers/receipts.py index 5cb1ff749d..0cdcd8f442 100644 --- a/synapse/handlers/receipts.py +++ b/synapse/handlers/receipts.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import logging -from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple +from typing import TYPE_CHECKING, Iterable, List, Optional, Set, Tuple from synapse.api.constants import ReadReceiptEventFields, ReceiptTypes from synapse.appservice import ApplicationService @@ -240,7 +240,7 @@ class ReceiptEventSource(EventSource[int, JsonDict]): async def get_new_events_as( self, from_key: int, service: ApplicationService - ) -> Tuple[List[JsonDict], int]: + ) -> Tuple[Set[JsonDict], int]: """Returns a set of new read receipt events that an appservice may be interested in. @@ -250,7 +250,7 @@ class ReceiptEventSource(EventSource[int, JsonDict]): Returns: A two-tuple containing the following: - * A list of json dictionaries derived from read receipts that the + * A set of json dictionaries derived from read receipts that the appservice may be interested in. * The current read receipt stream token. """ @@ -258,7 +258,7 @@ class ReceiptEventSource(EventSource[int, JsonDict]): to_key = self.get_current_key() if from_key == to_key: - return [], to_key + return set(), to_key # Fetch all read receipts for all rooms, up to a limit of 100. This is ordered # by most recent. @@ -267,12 +267,12 @@ class ReceiptEventSource(EventSource[int, JsonDict]): ) # Then filter down to rooms that the AS can read - events = [] + events = set() for room_id, event in rooms_to_events.items(): if not await service.matches_user_in_member_list(room_id, self.store): continue - events.append(event) + events.add(event) return events, to_key diff --git a/synapse/handlers/typing.py b/synapse/handlers/typing.py index e43c22832d..d679c8bd74 100644 --- a/synapse/handlers/typing.py +++ b/synapse/handlers/typing.py @@ -464,7 +464,7 @@ class TypingNotificationEventSource(EventSource[int, JsonDict]): async def get_new_events_as( self, from_key: int, service: ApplicationService - ) -> Tuple[List[JsonDict], int]: + ) -> Tuple[Set[JsonDict], int]: """Returns a set of new typing events that an appservice may be interested in. @@ -474,14 +474,14 @@ class TypingNotificationEventSource(EventSource[int, JsonDict]): Returns: A two-tuple containing the following: - * A list of json dictionaries derived from typing events that the + * A set of json dictionaries derived from typing events that the appservice may be interested in. * The latest known room serial. """ with Measure(self.clock, "typing.get_new_events_as"): handler = self.get_typing_handler() - events = [] + events = set() for room_id in handler._room_serials.keys(): if handler._room_serials[room_id] <= from_key: continue @@ -491,7 +491,7 @@ class TypingNotificationEventSource(EventSource[int, JsonDict]): ): continue - events.append(self._make_event_for(room_id)) + events.add(self._make_event_for(room_id)) return events, handler._latest_room_serial -- cgit 1.4.1