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

Commit b610223

Browse files
authored
Add type hints to event_push_actions. (#11594)
1 parent 2215954 commit b610223

File tree

11 files changed

+225
-154
lines changed

11 files changed

+225
-154
lines changed

changelog.d/11594.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add missing type hints to storage classes.

mypy.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ exclude = (?x)
2828
|synapse/storage/databases/main/cache.py
2929
|synapse/storage/databases/main/devices.py
3030
|synapse/storage/databases/main/event_federation.py
31-
|synapse/storage/databases/main/event_push_actions.py
3231
|synapse/storage/databases/main/events_bg_updates.py
3332
|synapse/storage/databases/main/group_server.py
3433
|synapse/storage/databases/main/metrics.py
@@ -200,6 +199,9 @@ disallow_untyped_defs = True
200199
[mypy-synapse.storage.databases.main.end_to_end_keys]
201200
disallow_untyped_defs = True
202201

202+
[mypy-synapse.storage.databases.main.event_push_actions]
203+
disallow_untyped_defs = True
204+
203205
[mypy-synapse.storage.databases.main.events_worker]
204206
disallow_untyped_defs = True
205207

synapse/handlers/sync.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from synapse.logging.context import current_context
3737
from synapse.logging.opentracing import SynapseTags, log_kv, set_tag, start_active_span
3838
from synapse.push.clientformat import format_push_rules_for_user
39+
from synapse.storage.databases.main.event_push_actions import NotifCounts
3940
from synapse.storage.roommember import MemberSummary
4041
from synapse.storage.state import StateFilter
4142
from synapse.types import (
@@ -1041,18 +1042,17 @@ async def compute_state_delta(
10411042

10421043
async def unread_notifs_for_room_id(
10431044
self, room_id: str, sync_config: SyncConfig
1044-
) -> Dict[str, int]:
1045+
) -> NotifCounts:
10451046
with Measure(self.clock, "unread_notifs_for_room_id"):
10461047
last_unread_event_id = await self.store.get_last_receipt_event_id_for_user(
10471048
user_id=sync_config.user.to_string(),
10481049
room_id=room_id,
10491050
receipt_type=ReceiptTypes.READ,
10501051
)
10511052

1052-
notifs = await self.store.get_unread_event_push_actions_by_room_for_user(
1053+
return await self.store.get_unread_event_push_actions_by_room_for_user(
10531054
room_id, sync_config.user.to_string(), last_unread_event_id
10541055
)
1055-
return notifs
10561056

10571057
async def generate_sync_result(
10581058
self,
@@ -2174,10 +2174,10 @@ async def _generate_room_entry(
21742174
if room_sync or always_include:
21752175
notifs = await self.unread_notifs_for_room_id(room_id, sync_config)
21762176

2177-
unread_notifications["notification_count"] = notifs["notify_count"]
2178-
unread_notifications["highlight_count"] = notifs["highlight_count"]
2177+
unread_notifications["notification_count"] = notifs.notify_count
2178+
unread_notifications["highlight_count"] = notifs.highlight_count
21792179

2180-
room_sync.unread_count = notifs["unread_count"]
2180+
room_sync.unread_count = notifs.unread_count
21812181

21822182
sync_result_builder.joined.append(room_sync)
21832183

synapse/push/emailpusher.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,12 @@ async def _unsafe_process(self) -> None:
177177
return
178178

179179
for push_action in unprocessed:
180-
received_at = push_action["received_ts"]
180+
received_at = push_action.received_ts
181181
if received_at is None:
182182
received_at = 0
183183
notif_ready_at = received_at + DELAY_BEFORE_MAIL_MS
184184

185-
room_ready_at = self.room_ready_to_notify_at(push_action["room_id"])
185+
room_ready_at = self.room_ready_to_notify_at(push_action.room_id)
186186

187187
should_notify_at = max(notif_ready_at, room_ready_at)
188188

@@ -193,23 +193,23 @@ async def _unsafe_process(self) -> None:
193193
# to be delivered.
194194

195195
reason: EmailReason = {
196-
"room_id": push_action["room_id"],
196+
"room_id": push_action.room_id,
197197
"now": self.clock.time_msec(),
198198
"received_at": received_at,
199199
"delay_before_mail_ms": DELAY_BEFORE_MAIL_MS,
200-
"last_sent_ts": self.get_room_last_sent_ts(push_action["room_id"]),
201-
"throttle_ms": self.get_room_throttle_ms(push_action["room_id"]),
200+
"last_sent_ts": self.get_room_last_sent_ts(push_action.room_id),
201+
"throttle_ms": self.get_room_throttle_ms(push_action.room_id),
202202
}
203203

204204
await self.send_notification(unprocessed, reason)
205205

206206
await self.save_last_stream_ordering_and_success(
207-
max(ea["stream_ordering"] for ea in unprocessed)
207+
max(ea.stream_ordering for ea in unprocessed)
208208
)
209209

210210
# we update the throttle on all the possible unprocessed push actions
211211
for ea in unprocessed:
212-
await self.sent_notif_update_throttle(ea["room_id"], ea)
212+
await self.sent_notif_update_throttle(ea.room_id, ea)
213213
break
214214
else:
215215
if soonest_due_at is None or should_notify_at < soonest_due_at:
@@ -284,10 +284,10 @@ async def sent_notif_update_throttle(
284284
# THROTTLE_RESET_AFTER_MS after the previous one that triggered a
285285
# notif, we release the throttle. Otherwise, the throttle is increased.
286286
time_of_previous_notifs = await self.store.get_time_of_last_push_action_before(
287-
notified_push_action["stream_ordering"]
287+
notified_push_action.stream_ordering
288288
)
289289

290-
time_of_this_notifs = notified_push_action["received_ts"]
290+
time_of_this_notifs = notified_push_action.received_ts
291291

292292
if time_of_previous_notifs is not None and time_of_this_notifs is not None:
293293
gap = time_of_this_notifs - time_of_previous_notifs

synapse/push/httppusher.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ async def _unsafe_process(self) -> None:
199199
"http-push",
200200
tags={
201201
"authenticated_entity": self.user_id,
202-
"event_id": push_action["event_id"],
202+
"event_id": push_action.event_id,
203203
"app_id": self.app_id,
204204
"app_display_name": self.app_display_name,
205205
},
@@ -209,7 +209,7 @@ async def _unsafe_process(self) -> None:
209209
if processed:
210210
http_push_processed_counter.inc()
211211
self.backoff_delay = HttpPusher.INITIAL_BACKOFF_SEC
212-
self.last_stream_ordering = push_action["stream_ordering"]
212+
self.last_stream_ordering = push_action.stream_ordering
213213
pusher_still_exists = (
214214
await self.store.update_pusher_last_stream_ordering_and_success(
215215
self.app_id,
@@ -252,7 +252,7 @@ async def _unsafe_process(self) -> None:
252252
self.pushkey,
253253
)
254254
self.backoff_delay = HttpPusher.INITIAL_BACKOFF_SEC
255-
self.last_stream_ordering = push_action["stream_ordering"]
255+
self.last_stream_ordering = push_action.stream_ordering
256256
await self.store.update_pusher_last_stream_ordering(
257257
self.app_id,
258258
self.pushkey,
@@ -275,17 +275,17 @@ async def _unsafe_process(self) -> None:
275275
break
276276

277277
async def _process_one(self, push_action: HttpPushAction) -> bool:
278-
if "notify" not in push_action["actions"]:
278+
if "notify" not in push_action.actions:
279279
return True
280280

281-
tweaks = push_rule_evaluator.tweaks_for_actions(push_action["actions"])
281+
tweaks = push_rule_evaluator.tweaks_for_actions(push_action.actions)
282282
badge = await push_tools.get_badge_count(
283283
self.hs.get_datastore(),
284284
self.user_id,
285285
group_by_room=self._group_unread_count_by_room,
286286
)
287287

288-
event = await self.store.get_event(push_action["event_id"], allow_none=True)
288+
event = await self.store.get_event(push_action.event_id, allow_none=True)
289289
if event is None:
290290
return True # It's been redacted
291291
rejected = await self.dispatch_push(event, tweaks, badge)

synapse/push/mailer.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,13 @@ async def send_notification_mail(
232232
reason: The notification that was ready and is the cause of an email
233233
being sent.
234234
"""
235-
rooms_in_order = deduped_ordered_list([pa["room_id"] for pa in push_actions])
235+
rooms_in_order = deduped_ordered_list([pa.room_id for pa in push_actions])
236236

237-
notif_events = await self.store.get_events(
238-
[pa["event_id"] for pa in push_actions]
239-
)
237+
notif_events = await self.store.get_events([pa.event_id for pa in push_actions])
240238

241239
notifs_by_room: Dict[str, List[EmailPushAction]] = {}
242240
for pa in push_actions:
243-
notifs_by_room.setdefault(pa["room_id"], []).append(pa)
241+
notifs_by_room.setdefault(pa.room_id, []).append(pa)
244242

245243
# collect the current state for all the rooms in which we have
246244
# notifications
@@ -264,7 +262,7 @@ async def _fetch_room_state(room_id: str) -> None:
264262
await concurrently_execute(_fetch_room_state, rooms_in_order, 3)
265263

266264
# actually sort our so-called rooms_in_order list, most recent room first
267-
rooms_in_order.sort(key=lambda r: -(notifs_by_room[r][-1]["received_ts"] or 0))
265+
rooms_in_order.sort(key=lambda r: -(notifs_by_room[r][-1].received_ts or 0))
268266

269267
rooms: List[RoomVars] = []
270268

@@ -356,7 +354,7 @@ async def _get_room_vars(
356354
# Check if one of the notifs is an invite event for the user.
357355
is_invite = False
358356
for n in notifs:
359-
ev = notif_events[n["event_id"]]
357+
ev = notif_events[n.event_id]
360358
if ev.type == EventTypes.Member and ev.state_key == user_id:
361359
if ev.content.get("membership") == Membership.INVITE:
362360
is_invite = True
@@ -376,7 +374,7 @@ async def _get_room_vars(
376374
if not is_invite:
377375
for n in notifs:
378376
notifvars = await self._get_notif_vars(
379-
n, user_id, notif_events[n["event_id"]], room_state_ids
377+
n, user_id, notif_events[n.event_id], room_state_ids
380378
)
381379

382380
# merge overlapping notifs together.
@@ -444,15 +442,15 @@ async def _get_notif_vars(
444442
"""
445443

446444
results = await self.store.get_events_around(
447-
notif["room_id"],
448-
notif["event_id"],
445+
notif.room_id,
446+
notif.event_id,
449447
before_limit=CONTEXT_BEFORE,
450448
after_limit=CONTEXT_AFTER,
451449
)
452450

453451
ret: NotifVars = {
454452
"link": self._make_notif_link(notif),
455-
"ts": notif["received_ts"],
453+
"ts": notif.received_ts,
456454
"messages": [],
457455
}
458456

@@ -516,7 +514,7 @@ async def _get_message_vars(
516514

517515
ret: MessageVars = {
518516
"event_type": event.type,
519-
"is_historical": event.event_id != notif["event_id"],
517+
"is_historical": event.event_id != notif.event_id,
520518
"id": event.event_id,
521519
"ts": event.origin_server_ts,
522520
"sender_name": sender_name,
@@ -610,7 +608,7 @@ async def _make_summary_text_single_room(
610608
# See if one of the notifs is an invite event for the user
611609
invite_event = None
612610
for n in notifs:
613-
ev = notif_events[n["event_id"]]
611+
ev = notif_events[n.event_id]
614612
if ev.type == EventTypes.Member and ev.state_key == user_id:
615613
if ev.content.get("membership") == Membership.INVITE:
616614
invite_event = ev
@@ -659,7 +657,7 @@ async def _make_summary_text_single_room(
659657
if len(notifs) == 1:
660658
# There is just the one notification, so give some detail
661659
sender_name = None
662-
event = notif_events[notifs[0]["event_id"]]
660+
event = notif_events[notifs[0].event_id]
663661
if ("m.room.member", event.sender) in room_state_ids:
664662
state_event_id = room_state_ids[("m.room.member", event.sender)]
665663
state_event = await self.store.get_event(state_event_id)
@@ -753,9 +751,9 @@ async def _make_summary_text_from_member_events(
753751
# are already in descending received_ts.
754752
sender_ids = {}
755753
for n in notifs:
756-
sender = notif_events[n["event_id"]].sender
754+
sender = notif_events[n.event_id].sender
757755
if sender not in sender_ids:
758-
sender_ids[sender] = n["event_id"]
756+
sender_ids[sender] = n.event_id
759757

760758
# Get the actual member events (in order to calculate a pretty name for
761759
# the room).
@@ -830,17 +828,17 @@ def _make_notif_link(self, notif: EmailPushAction) -> str:
830828
if self.hs.config.email.email_riot_base_url:
831829
return "%s/#/room/%s/%s" % (
832830
self.hs.config.email.email_riot_base_url,
833-
notif["room_id"],
834-
notif["event_id"],
831+
notif.room_id,
832+
notif.event_id,
835833
)
836834
elif self.app_name == "Vector":
837835
# need /beta for Universal Links to work on iOS
838836
return "https://vector.im/beta/#/room/%s/%s" % (
839-
notif["room_id"],
840-
notif["event_id"],
837+
notif.room_id,
838+
notif.event_id,
841839
)
842840
else:
843-
return "https://matrix.to/#/%s/%s" % (notif["room_id"], notif["event_id"])
841+
return "https://matrix.to/#/%s/%s" % (notif.room_id, notif.event_id)
844842

845843
def _make_unsubscribe_link(
846844
self, user_id: str, app_id: str, email_address: str

synapse/push/push_tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ async def get_badge_count(store: DataStore, user_id: str, group_by_room: bool) -
3737
room_id, user_id, last_unread_event_id
3838
)
3939
)
40-
if notifs["notify_count"] == 0:
40+
if notifs.notify_count == 0:
4141
continue
4242

4343
if group_by_room:
4444
# return one badge count per conversation
4545
badge += 1
4646
else:
4747
# increment the badge count by the number of unread messages in the room
48-
badge += notifs["notify_count"]
48+
badge += notifs.notify_count
4949
return badge
5050

5151

synapse/rest/client/notifications.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
5858
user_id, ReceiptTypes.READ
5959
)
6060

61-
notif_event_ids = [pa["event_id"] for pa in push_actions]
61+
notif_event_ids = [pa.event_id for pa in push_actions]
6262
notif_events = await self.store.get_events(notif_event_ids)
6363

6464
returned_push_actions = []
@@ -67,30 +67,30 @@ async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
6767

6868
for pa in push_actions:
6969
returned_pa = {
70-
"room_id": pa["room_id"],
71-
"profile_tag": pa["profile_tag"],
72-
"actions": pa["actions"],
73-
"ts": pa["received_ts"],
70+
"room_id": pa.room_id,
71+
"profile_tag": pa.profile_tag,
72+
"actions": pa.actions,
73+
"ts": pa.received_ts,
7474
"event": (
7575
await self._event_serializer.serialize_event(
76-
notif_events[pa["event_id"]],
76+
notif_events[pa.event_id],
7777
self.clock.time_msec(),
7878
event_format=format_event_for_client_v2_without_room_id,
7979
)
8080
),
8181
}
8282

83-
if pa["room_id"] not in receipts_by_room:
83+
if pa.room_id not in receipts_by_room:
8484
returned_pa["read"] = False
8585
else:
86-
receipt = receipts_by_room[pa["room_id"]]
86+
receipt = receipts_by_room[pa.room_id]
8787

8888
returned_pa["read"] = (
8989
receipt["topological_ordering"],
9090
receipt["stream_ordering"],
91-
) >= (pa["topological_ordering"], pa["stream_ordering"])
91+
) >= (pa.topological_ordering, pa.stream_ordering)
9292
returned_push_actions.append(returned_pa)
93-
next_token = str(pa["stream_ordering"])
93+
next_token = str(pa.stream_ordering)
9494

9595
return 200, {"notifications": returned_push_actions, "next_token": next_token}
9696

0 commit comments

Comments
 (0)