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

Commit 83be72d

Browse files
Add StreamKeyType class and replace string literals with constants (#12567)
1 parent 3ce15cc commit 83be72d

File tree

19 files changed

+125
-80
lines changed

19 files changed

+125
-80
lines changed

changelog.d/12567.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace string literal instances of stream key types with typed constants.

synapse/handlers/account_data.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
ReplicationUserAccountDataRestServlet,
2424
)
2525
from synapse.streams import EventSource
26-
from synapse.types import JsonDict, UserID
26+
from synapse.types import JsonDict, StreamKeyType, UserID
2727

2828
if TYPE_CHECKING:
2929
from synapse.server import HomeServer
@@ -105,7 +105,7 @@ async def add_account_data_to_room(
105105
)
106106

107107
self._notifier.on_new_event(
108-
"account_data_key", max_stream_id, users=[user_id]
108+
StreamKeyType.ACCOUNT_DATA, max_stream_id, users=[user_id]
109109
)
110110

111111
await self._notify_modules(user_id, room_id, account_data_type, content)
@@ -141,7 +141,7 @@ async def add_account_data_for_user(
141141
)
142142

143143
self._notifier.on_new_event(
144-
"account_data_key", max_stream_id, users=[user_id]
144+
StreamKeyType.ACCOUNT_DATA, max_stream_id, users=[user_id]
145145
)
146146

147147
await self._notify_modules(user_id, None, account_data_type, content)
@@ -176,7 +176,7 @@ async def add_tag_to_room(
176176
)
177177

178178
self._notifier.on_new_event(
179-
"account_data_key", max_stream_id, users=[user_id]
179+
StreamKeyType.ACCOUNT_DATA, max_stream_id, users=[user_id]
180180
)
181181
return max_stream_id
182182
else:
@@ -201,7 +201,7 @@ async def remove_tag_from_room(self, user_id: str, room_id: str, tag: str) -> in
201201
)
202202

203203
self._notifier.on_new_event(
204-
"account_data_key", max_stream_id, users=[user_id]
204+
StreamKeyType.ACCOUNT_DATA, max_stream_id, users=[user_id]
205205
)
206206
return max_stream_id
207207
else:

synapse/handlers/appservice.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
JsonDict,
3939
RoomAlias,
4040
RoomStreamToken,
41+
StreamKeyType,
4142
UserID,
4243
)
4344
from synapse.util.async_helpers import Linearizer
@@ -213,8 +214,8 @@ def notify_interested_services_ephemeral(
213214
Args:
214215
stream_key: The stream the event came from.
215216
216-
`stream_key` can be "typing_key", "receipt_key", "presence_key",
217-
"to_device_key" or "device_list_key". Any other value for `stream_key`
217+
`stream_key` can be StreamKeyType.TYPING, StreamKeyType.RECEIPT, StreamKeyType.PRESENCE,
218+
StreamKeyType.TO_DEVICE or StreamKeyType.DEVICE_LIST. Any other value for `stream_key`
218219
will cause this function to return early.
219220
220221
Ephemeral events will only be pushed to appservices that have opted into
@@ -235,11 +236,11 @@ def notify_interested_services_ephemeral(
235236
# Only the following streams are currently supported.
236237
# FIXME: We should use constants for these values.
237238
if stream_key not in (
238-
"typing_key",
239-
"receipt_key",
240-
"presence_key",
241-
"to_device_key",
242-
"device_list_key",
239+
StreamKeyType.TYPING,
240+
StreamKeyType.RECEIPT,
241+
StreamKeyType.PRESENCE,
242+
StreamKeyType.TO_DEVICE,
243+
StreamKeyType.DEVICE_LIST,
243244
):
244245
return
245246

@@ -258,14 +259,14 @@ def notify_interested_services_ephemeral(
258259

259260
# Ignore to-device messages if the feature flag is not enabled
260261
if (
261-
stream_key == "to_device_key"
262+
stream_key == StreamKeyType.TO_DEVICE
262263
and not self._msc2409_to_device_messages_enabled
263264
):
264265
return
265266

266267
# Ignore device lists if the feature flag is not enabled
267268
if (
268-
stream_key == "device_list_key"
269+
stream_key == StreamKeyType.DEVICE_LIST
269270
and not self._msc3202_transaction_extensions_enabled
270271
):
271272
return
@@ -283,15 +284,15 @@ def notify_interested_services_ephemeral(
283284
if (
284285
stream_key
285286
in (
286-
"typing_key",
287-
"receipt_key",
288-
"presence_key",
289-
"to_device_key",
287+
StreamKeyType.TYPING,
288+
StreamKeyType.RECEIPT,
289+
StreamKeyType.PRESENCE,
290+
StreamKeyType.TO_DEVICE,
290291
)
291292
and service.supports_ephemeral
292293
)
293294
or (
294-
stream_key == "device_list_key"
295+
stream_key == StreamKeyType.DEVICE_LIST
295296
and service.msc3202_transaction_extensions
296297
)
297298
]
@@ -317,7 +318,7 @@ async def _notify_interested_services_ephemeral(
317318
logger.debug("Checking interested services for %s", stream_key)
318319
with Measure(self.clock, "notify_interested_services_ephemeral"):
319320
for service in services:
320-
if stream_key == "typing_key":
321+
if stream_key == StreamKeyType.TYPING:
321322
# Note that we don't persist the token (via set_appservice_stream_type_pos)
322323
# for typing_key due to performance reasons and due to their highly
323324
# ephemeral nature.
@@ -333,7 +334,7 @@ async def _notify_interested_services_ephemeral(
333334
async with self._ephemeral_events_linearizer.queue(
334335
(service.id, stream_key)
335336
):
336-
if stream_key == "receipt_key":
337+
if stream_key == StreamKeyType.RECEIPT:
337338
events = await self._handle_receipts(service, new_token)
338339
self.scheduler.enqueue_for_appservice(service, ephemeral=events)
339340

@@ -342,7 +343,7 @@ async def _notify_interested_services_ephemeral(
342343
service, "read_receipt", new_token
343344
)
344345

345-
elif stream_key == "presence_key":
346+
elif stream_key == StreamKeyType.PRESENCE:
346347
events = await self._handle_presence(service, users, new_token)
347348
self.scheduler.enqueue_for_appservice(service, ephemeral=events)
348349

@@ -351,7 +352,7 @@ async def _notify_interested_services_ephemeral(
351352
service, "presence", new_token
352353
)
353354

354-
elif stream_key == "to_device_key":
355+
elif stream_key == StreamKeyType.TO_DEVICE:
355356
# Retrieve a list of to-device message events, as well as the
356357
# maximum stream token of the messages we were able to retrieve.
357358
to_device_messages = await self._get_to_device_messages(
@@ -366,7 +367,7 @@ async def _notify_interested_services_ephemeral(
366367
service, "to_device", new_token
367368
)
368369

369-
elif stream_key == "device_list_key":
370+
elif stream_key == StreamKeyType.DEVICE_LIST:
370371
device_list_summary = await self._get_device_list_summary(
371372
service, new_token
372373
)

synapse/handlers/device.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
)
4444
from synapse.types import (
4545
JsonDict,
46+
StreamKeyType,
4647
StreamToken,
4748
UserID,
4849
get_domain_from_id,
@@ -502,7 +503,7 @@ async def notify_device_update(
502503
# specify the user ID too since the user should always get their own device list
503504
# updates, even if they aren't in any rooms.
504505
self.notifier.on_new_event(
505-
"device_list_key", position, users={user_id}, rooms=room_ids
506+
StreamKeyType.DEVICE_LIST, position, users={user_id}, rooms=room_ids
506507
)
507508

508509
# We may need to do some processing asynchronously for local user IDs.
@@ -523,7 +524,9 @@ async def notify_user_signature_update(
523524
from_user_id, user_ids
524525
)
525526

526-
self.notifier.on_new_event("device_list_key", position, users=[from_user_id])
527+
self.notifier.on_new_event(
528+
StreamKeyType.DEVICE_LIST, position, users=[from_user_id]
529+
)
527530

528531
async def user_left_room(self, user: UserID, room_id: str) -> None:
529532
user_id = user.to_string()

synapse/handlers/devicemessage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
set_tag,
2727
)
2828
from synapse.replication.http.devices import ReplicationUserDevicesResyncRestServlet
29-
from synapse.types import JsonDict, Requester, UserID, get_domain_from_id
29+
from synapse.types import JsonDict, Requester, StreamKeyType, UserID, get_domain_from_id
3030
from synapse.util import json_encoder
3131
from synapse.util.stringutils import random_string
3232

@@ -151,7 +151,7 @@ async def on_direct_to_device_edu(self, origin: str, content: JsonDict) -> None:
151151
# Notify listeners that there are new to-device messages to process,
152152
# handing them the latest stream id.
153153
self.notifier.on_new_event(
154-
"to_device_key", last_stream_id, users=local_messages.keys()
154+
StreamKeyType.TO_DEVICE, last_stream_id, users=local_messages.keys()
155155
)
156156

157157
async def _check_for_unknown_devices(
@@ -285,7 +285,7 @@ async def send_device_message(
285285
# Notify listeners that there are new to-device messages to process,
286286
# handing them the latest stream id.
287287
self.notifier.on_new_event(
288-
"to_device_key", last_stream_id, users=local_messages.keys()
288+
StreamKeyType.TO_DEVICE, last_stream_id, users=local_messages.keys()
289289
)
290290

291291
if self.federation_sender:

synapse/handlers/initial_sync.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
Requester,
3131
RoomStreamToken,
3232
StateMap,
33+
StreamKeyType,
3334
StreamToken,
3435
UserID,
3536
)
@@ -220,8 +221,10 @@ async def handle_room(event: RoomsForUser) -> None:
220221
self.storage, user_id, messages
221222
)
222223

223-
start_token = now_token.copy_and_replace("room_key", token)
224-
end_token = now_token.copy_and_replace("room_key", room_end_token)
224+
start_token = now_token.copy_and_replace(StreamKeyType.ROOM, token)
225+
end_token = now_token.copy_and_replace(
226+
StreamKeyType.ROOM, room_end_token
227+
)
225228
time_now = self.clock.time_msec()
226229

227230
d["messages"] = {
@@ -369,8 +372,8 @@ async def _room_initial_sync_parted(
369372
self.storage, user_id, messages, is_peeking=is_peeking
370373
)
371374

372-
start_token = StreamToken.START.copy_and_replace("room_key", token)
373-
end_token = StreamToken.START.copy_and_replace("room_key", stream_token)
375+
start_token = StreamToken.START.copy_and_replace(StreamKeyType.ROOM, token)
376+
end_token = StreamToken.START.copy_and_replace(StreamKeyType.ROOM, stream_token)
374377

375378
time_now = self.clock.time_msec()
376379

@@ -474,7 +477,7 @@ async def get_receipts() -> List[JsonDict]:
474477
self.storage, user_id, messages, is_peeking=is_peeking
475478
)
476479

477-
start_token = now_token.copy_and_replace("room_key", token)
480+
start_token = now_token.copy_and_replace(StreamKeyType.ROOM, token)
478481
end_token = now_token
479482

480483
time_now = self.clock.time_msec()

synapse/handlers/pagination.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from synapse.metrics.background_process_metrics import run_as_background_process
2828
from synapse.storage.state import StateFilter
2929
from synapse.streams.config import PaginationConfig
30-
from synapse.types import JsonDict, Requester
30+
from synapse.types import JsonDict, Requester, StreamKeyType
3131
from synapse.util.async_helpers import ReadWriteLock
3232
from synapse.util.stringutils import random_string
3333
from synapse.visibility import filter_events_for_client
@@ -491,7 +491,7 @@ async def get_messages(
491491

492492
if leave_token.topological < curr_topo:
493493
from_token = from_token.copy_and_replace(
494-
"room_key", leave_token
494+
StreamKeyType.ROOM, leave_token
495495
)
496496

497497
await self.hs.get_federation_handler().maybe_backfill(
@@ -513,7 +513,7 @@ async def get_messages(
513513
event_filter=event_filter,
514514
)
515515

516-
next_token = from_token.copy_and_replace("room_key", next_key)
516+
next_token = from_token.copy_and_replace(StreamKeyType.ROOM, next_key)
517517

518518
if events:
519519
if event_filter:

synapse/handlers/presence.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
from synapse.replication.tcp.streams import PresenceFederationStream, PresenceStream
6767
from synapse.storage.databases.main import DataStore
6868
from synapse.streams import EventSource
69-
from synapse.types import JsonDict, UserID, get_domain_from_id
69+
from synapse.types import JsonDict, StreamKeyType, UserID, get_domain_from_id
7070
from synapse.util.async_helpers import Linearizer
7171
from synapse.util.caches.descriptors import _CacheContext, cached
7272
from synapse.util.metrics import Measure
@@ -522,7 +522,7 @@ async def notify_from_replication(
522522
room_ids_to_states, users_to_states = parties
523523

524524
self.notifier.on_new_event(
525-
"presence_key",
525+
StreamKeyType.PRESENCE,
526526
stream_id,
527527
rooms=room_ids_to_states.keys(),
528528
users=users_to_states.keys(),
@@ -1145,7 +1145,7 @@ async def _persist_and_notify(self, states: List[UserPresenceState]) -> None:
11451145
room_ids_to_states, users_to_states = parties
11461146

11471147
self.notifier.on_new_event(
1148-
"presence_key",
1148+
StreamKeyType.PRESENCE,
11491149
stream_id,
11501150
rooms=room_ids_to_states.keys(),
11511151
users=[UserID.from_string(u) for u in users_to_states],

synapse/handlers/receipts.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
from synapse.api.constants import ReceiptTypes
1818
from synapse.appservice import ApplicationService
1919
from synapse.streams import EventSource
20-
from synapse.types import JsonDict, ReadReceipt, UserID, get_domain_from_id
20+
from synapse.types import (
21+
JsonDict,
22+
ReadReceipt,
23+
StreamKeyType,
24+
UserID,
25+
get_domain_from_id,
26+
)
2127

2228
if TYPE_CHECKING:
2329
from synapse.server import HomeServer
@@ -129,7 +135,9 @@ async def _handle_new_receipts(self, receipts: List[ReadReceipt]) -> bool:
129135

130136
affected_room_ids = list({r.room_id for r in receipts})
131137

132-
self.notifier.on_new_event("receipt_key", max_batch_id, rooms=affected_room_ids)
138+
self.notifier.on_new_event(
139+
StreamKeyType.RECEIPT, max_batch_id, rooms=affected_room_ids
140+
)
133141
# Note that the min here shouldn't be relied upon to be accurate.
134142
await self.hs.get_pusherpool().on_new_receipts(
135143
min_batch_id, max_batch_id, affected_room_ids

synapse/handlers/room.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
RoomID,
7474
RoomStreamToken,
7575
StateMap,
76+
StreamKeyType,
7677
StreamToken,
7778
UserID,
7879
create_requester,
@@ -1292,10 +1293,10 @@ async def filter_evts(events: List[EventBase]) -> List[EventBase]:
12921293
events_after=events_after,
12931294
state=await filter_evts(state_events),
12941295
aggregations=aggregations,
1295-
start=await token.copy_and_replace("room_key", results.start).to_string(
1296-
self.store
1297-
),
1298-
end=await token.copy_and_replace("room_key", results.end).to_string(
1296+
start=await token.copy_and_replace(
1297+
StreamKeyType.ROOM, results.start
1298+
).to_string(self.store),
1299+
end=await token.copy_and_replace(StreamKeyType.ROOM, results.end).to_string(
12991300
self.store
13001301
),
13011302
)

0 commit comments

Comments
 (0)