Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit c9c3aea

Browse files
Fix providing a RoomStreamToken instance to _notify_app_services_ephemeral (#11137)
Co-authored-by: Richard van der Hoff <[email protected]>
1 parent 7537201 commit c9c3aea

File tree

5 files changed

+30
-37
lines changed

5 files changed

+30
-37
lines changed

changelog.d/11137.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove and document unnecessary `RoomStreamToken` checks in application service ephemeral event code.

synapse/handlers/appservice.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ async def handle_room_events(events: Iterable[EventBase]) -> None:
182182
def notify_interested_services_ephemeral(
183183
self,
184184
stream_key: str,
185-
new_token: Optional[int],
185+
new_token: Union[int, RoomStreamToken],
186186
users: Optional[Collection[Union[str, UserID]]] = None,
187187
) -> None:
188188
"""
@@ -203,7 +203,7 @@ def notify_interested_services_ephemeral(
203203
Appservices will only receive ephemeral events that fall within their
204204
registered user and room namespaces.
205205
206-
new_token: The latest stream token.
206+
new_token: The stream token of the event.
207207
users: The users that should be informed of the new event, if any.
208208
"""
209209
if not self.notify_appservices:
@@ -212,6 +212,19 @@ def notify_interested_services_ephemeral(
212212
if stream_key not in ("typing_key", "receipt_key", "presence_key"):
213213
return
214214

215+
# Assert that new_token is an integer (and not a RoomStreamToken).
216+
# All of the supported streams that this function handles use an
217+
# integer to track progress (rather than a RoomStreamToken - a
218+
# vector clock implementation) as they don't support multiple
219+
# stream writers.
220+
#
221+
# As a result, we simply assert that new_token is an integer.
222+
# If we do end up needing to pass a RoomStreamToken down here
223+
# in the future, using RoomStreamToken.stream (the minimum stream
224+
# position) to convert to an ascending integer value should work.
225+
# Additional context: https://github.com/matrix-org/synapse/pull/11137
226+
assert isinstance(new_token, int)
227+
215228
services = [
216229
service
217230
for service in self.store.get_app_services()
@@ -231,14 +244,13 @@ async def _notify_interested_services_ephemeral(
231244
self,
232245
services: List[ApplicationService],
233246
stream_key: str,
234-
new_token: Optional[int],
247+
new_token: int,
235248
users: Collection[Union[str, UserID]],
236249
) -> None:
237250
logger.debug("Checking interested services for %s" % (stream_key))
238251
with Measure(self.clock, "notify_interested_services_ephemeral"):
239252
for service in services:
240-
# Only handle typing if we have the latest token
241-
if stream_key == "typing_key" and new_token is not None:
253+
if stream_key == "typing_key":
242254
# Note that we don't persist the token (via set_type_stream_id_for_appservice)
243255
# for typing_key due to performance reasons and due to their highly
244256
# ephemeral nature.

synapse/notifier.py

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -383,29 +383,6 @@ def _notify_app_services(self, max_room_stream_token: RoomStreamToken):
383383
except Exception:
384384
logger.exception("Error notifying application services of event")
385385

386-
def _notify_app_services_ephemeral(
387-
self,
388-
stream_key: str,
389-
new_token: Union[int, RoomStreamToken],
390-
users: Optional[Collection[Union[str, UserID]]] = None,
391-
) -> None:
392-
"""Notify application services of ephemeral event activity.
393-
394-
Args:
395-
stream_key: The stream the event came from.
396-
new_token: The value of the new stream token.
397-
users: The users that should be informed of the new event, if any.
398-
"""
399-
try:
400-
stream_token = None
401-
if isinstance(new_token, int):
402-
stream_token = new_token
403-
self.appservice_handler.notify_interested_services_ephemeral(
404-
stream_key, stream_token, users or []
405-
)
406-
except Exception:
407-
logger.exception("Error notifying application services of event")
408-
409386
def _notify_pusher_pool(self, max_room_stream_token: RoomStreamToken):
410387
try:
411388
self._pusher_pool.on_new_notifications(max_room_stream_token)
@@ -467,12 +444,15 @@ def on_new_event(
467444

468445
self.notify_replication()
469446

470-
# Notify appservices
471-
self._notify_app_services_ephemeral(
472-
stream_key,
473-
new_token,
474-
users,
475-
)
447+
# Notify appservices.
448+
try:
449+
self.appservice_handler.notify_interested_services_ephemeral(
450+
stream_key,
451+
new_token,
452+
users,
453+
)
454+
except Exception:
455+
logger.exception("Error notifying application services of event")
476456

477457
def on_new_replication_data(self) -> None:
478458
"""Used to inform replication listeners that something has happened

synapse/storage/databases/main/devices.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ async def add_user_signature_change_to_streams(
427427
user_ids: the users who were signed
428428
429429
Returns:
430-
THe new stream ID.
430+
The new stream ID.
431431
"""
432432

433433
async with self._device_list_id_gen.get_next() as stream_id:
@@ -1322,7 +1322,7 @@ def _update_remote_device_list_cache_txn(
13221322

13231323
async def add_device_change_to_streams(
13241324
self, user_id: str, device_ids: Collection[str], hosts: List[str]
1325-
):
1325+
) -> int:
13261326
"""Persist that a user's devices have been updated, and which hosts
13271327
(if any) should be poked.
13281328
"""

synapse/storage/databases/main/presence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def __init__(
9292
prefilled_cache=presence_cache_prefill,
9393
)
9494

95-
async def update_presence(self, presence_states):
95+
async def update_presence(self, presence_states) -> Tuple[int, int]:
9696
assert self._can_persist_presence
9797

9898
stream_ordering_manager = self._presence_id_gen.get_next_mult(

0 commit comments

Comments
 (0)