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

Add type hints to event_push_actions. #11594

Merged
merged 7 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/11594.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add missing type hints to storage classes.
4 changes: 3 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ exclude = (?x)
|synapse/storage/databases/main/cache.py
|synapse/storage/databases/main/devices.py
|synapse/storage/databases/main/event_federation.py
|synapse/storage/databases/main/event_push_actions.py
|synapse/storage/databases/main/events_bg_updates.py
|synapse/storage/databases/main/group_server.py
|synapse/storage/databases/main/metrics.py
Expand Down Expand Up @@ -200,6 +199,9 @@ disallow_untyped_defs = True
[mypy-synapse.storage.databases.main.end_to_end_keys]
disallow_untyped_defs = True

[mypy-synapse.storage.databases.main.event_push_actions]
disallow_untyped_defs = True

[mypy-synapse.storage.databases.main.events_worker]
disallow_untyped_defs = True

Expand Down
12 changes: 6 additions & 6 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from synapse.logging.context import current_context
from synapse.logging.opentracing import SynapseTags, log_kv, set_tag, start_active_span
from synapse.push.clientformat import format_push_rules_for_user
from synapse.storage.databases.main.event_push_actions import NotifCounts
from synapse.storage.roommember import MemberSummary
from synapse.storage.state import StateFilter
from synapse.types import (
Expand Down Expand Up @@ -1041,18 +1042,17 @@ async def compute_state_delta(

async def unread_notifs_for_room_id(
self, room_id: str, sync_config: SyncConfig
) -> Dict[str, int]:
) -> NotifCounts:
with Measure(self.clock, "unread_notifs_for_room_id"):
last_unread_event_id = await self.store.get_last_receipt_event_id_for_user(
user_id=sync_config.user.to_string(),
room_id=room_id,
receipt_type=ReceiptTypes.READ,
)

notifs = await self.store.get_unread_event_push_actions_by_room_for_user(
return await self.store.get_unread_event_push_actions_by_room_for_user(
room_id, sync_config.user.to_string(), last_unread_event_id
)
return notifs

async def generate_sync_result(
self,
Expand Down Expand Up @@ -2174,10 +2174,10 @@ async def _generate_room_entry(
if room_sync or always_include:
notifs = await self.unread_notifs_for_room_id(room_id, sync_config)

unread_notifications["notification_count"] = notifs["notify_count"]
unread_notifications["highlight_count"] = notifs["highlight_count"]
unread_notifications["notification_count"] = notifs.notify_count
unread_notifications["highlight_count"] = notifs.highlight_count

room_sync.unread_count = notifs["unread_count"]
room_sync.unread_count = notifs.unread_count

sync_result_builder.joined.append(room_sync)

Expand Down
18 changes: 9 additions & 9 deletions synapse/push/emailpusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ async def _unsafe_process(self) -> None:
return

for push_action in unprocessed:
received_at = push_action["received_ts"]
received_at = push_action.received_ts
if received_at is None:
received_at = 0
notif_ready_at = received_at + DELAY_BEFORE_MAIL_MS

room_ready_at = self.room_ready_to_notify_at(push_action["room_id"])
room_ready_at = self.room_ready_to_notify_at(push_action.room_id)

should_notify_at = max(notif_ready_at, room_ready_at)

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

reason: EmailReason = {
"room_id": push_action["room_id"],
"room_id": push_action.room_id,
"now": self.clock.time_msec(),
"received_at": received_at,
"delay_before_mail_ms": DELAY_BEFORE_MAIL_MS,
"last_sent_ts": self.get_room_last_sent_ts(push_action["room_id"]),
"throttle_ms": self.get_room_throttle_ms(push_action["room_id"]),
"last_sent_ts": self.get_room_last_sent_ts(push_action.room_id),
"throttle_ms": self.get_room_throttle_ms(push_action.room_id),
}

await self.send_notification(unprocessed, reason)

await self.save_last_stream_ordering_and_success(
max(ea["stream_ordering"] for ea in unprocessed)
max(ea.stream_ordering for ea in unprocessed)
)

# we update the throttle on all the possible unprocessed push actions
for ea in unprocessed:
await self.sent_notif_update_throttle(ea["room_id"], ea)
await self.sent_notif_update_throttle(ea.room_id, ea)
break
else:
if soonest_due_at is None or should_notify_at < soonest_due_at:
Expand Down Expand Up @@ -284,10 +284,10 @@ async def sent_notif_update_throttle(
# THROTTLE_RESET_AFTER_MS after the previous one that triggered a
# notif, we release the throttle. Otherwise, the throttle is increased.
time_of_previous_notifs = await self.store.get_time_of_last_push_action_before(
notified_push_action["stream_ordering"]
notified_push_action.stream_ordering
)

time_of_this_notifs = notified_push_action["received_ts"]
time_of_this_notifs = notified_push_action.received_ts

if time_of_previous_notifs is not None and time_of_this_notifs is not None:
gap = time_of_this_notifs - time_of_previous_notifs
Expand Down
12 changes: 6 additions & 6 deletions synapse/push/httppusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ async def _unsafe_process(self) -> None:
"http-push",
tags={
"authenticated_entity": self.user_id,
"event_id": push_action["event_id"],
"event_id": push_action.event_id,
"app_id": self.app_id,
"app_display_name": self.app_display_name,
},
Expand All @@ -209,7 +209,7 @@ async def _unsafe_process(self) -> None:
if processed:
http_push_processed_counter.inc()
self.backoff_delay = HttpPusher.INITIAL_BACKOFF_SEC
self.last_stream_ordering = push_action["stream_ordering"]
self.last_stream_ordering = push_action.stream_ordering
pusher_still_exists = (
await self.store.update_pusher_last_stream_ordering_and_success(
self.app_id,
Expand Down Expand Up @@ -252,7 +252,7 @@ async def _unsafe_process(self) -> None:
self.pushkey,
)
self.backoff_delay = HttpPusher.INITIAL_BACKOFF_SEC
self.last_stream_ordering = push_action["stream_ordering"]
self.last_stream_ordering = push_action.stream_ordering
await self.store.update_pusher_last_stream_ordering(
self.app_id,
self.pushkey,
Expand All @@ -275,17 +275,17 @@ async def _unsafe_process(self) -> None:
break

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

tweaks = push_rule_evaluator.tweaks_for_actions(push_action["actions"])
tweaks = push_rule_evaluator.tweaks_for_actions(push_action.actions)
badge = await push_tools.get_badge_count(
self.hs.get_datastore(),
self.user_id,
group_by_room=self._group_unread_count_by_room,
)

event = await self.store.get_event(push_action["event_id"], allow_none=True)
event = await self.store.get_event(push_action.event_id, allow_none=True)
if event is None:
return True # It's been redacted
rejected = await self.dispatch_push(event, tweaks, badge)
Expand Down
40 changes: 19 additions & 21 deletions synapse/push/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,13 @@ async def send_notification_mail(
reason: The notification that was ready and is the cause of an email
being sent.
"""
rooms_in_order = deduped_ordered_list([pa["room_id"] for pa in push_actions])
rooms_in_order = deduped_ordered_list([pa.room_id for pa in push_actions])

notif_events = await self.store.get_events(
[pa["event_id"] for pa in push_actions]
)
notif_events = await self.store.get_events([pa.event_id for pa in push_actions])

notifs_by_room: Dict[str, List[EmailPushAction]] = {}
for pa in push_actions:
notifs_by_room.setdefault(pa["room_id"], []).append(pa)
notifs_by_room.setdefault(pa.room_id, []).append(pa)

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

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

rooms: List[RoomVars] = []

Expand Down Expand Up @@ -356,7 +354,7 @@ async def _get_room_vars(
# Check if one of the notifs is an invite event for the user.
is_invite = False
for n in notifs:
ev = notif_events[n["event_id"]]
ev = notif_events[n.event_id]
if ev.type == EventTypes.Member and ev.state_key == user_id:
if ev.content.get("membership") == Membership.INVITE:
is_invite = True
Expand All @@ -376,7 +374,7 @@ async def _get_room_vars(
if not is_invite:
for n in notifs:
notifvars = await self._get_notif_vars(
n, user_id, notif_events[n["event_id"]], room_state_ids
n, user_id, notif_events[n.event_id], room_state_ids
)

# merge overlapping notifs together.
Expand Down Expand Up @@ -444,15 +442,15 @@ async def _get_notif_vars(
"""

results = await self.store.get_events_around(
notif["room_id"],
notif["event_id"],
notif.room_id,
notif.event_id,
before_limit=CONTEXT_BEFORE,
after_limit=CONTEXT_AFTER,
)

ret: NotifVars = {
"link": self._make_notif_link(notif),
"ts": notif["received_ts"],
"ts": notif.received_ts,
"messages": [],
}

Expand Down Expand Up @@ -516,7 +514,7 @@ async def _get_message_vars(

ret: MessageVars = {
"event_type": event.type,
"is_historical": event.event_id != notif["event_id"],
"is_historical": event.event_id != notif.event_id,
"id": event.event_id,
"ts": event.origin_server_ts,
"sender_name": sender_name,
Expand Down Expand Up @@ -610,7 +608,7 @@ async def _make_summary_text_single_room(
# See if one of the notifs is an invite event for the user
invite_event = None
for n in notifs:
ev = notif_events[n["event_id"]]
ev = notif_events[n.event_id]
if ev.type == EventTypes.Member and ev.state_key == user_id:
if ev.content.get("membership") == Membership.INVITE:
invite_event = ev
Expand Down Expand Up @@ -659,7 +657,7 @@ async def _make_summary_text_single_room(
if len(notifs) == 1:
# There is just the one notification, so give some detail
sender_name = None
event = notif_events[notifs[0]["event_id"]]
event = notif_events[notifs[0].event_id]
if ("m.room.member", event.sender) in room_state_ids:
state_event_id = room_state_ids[("m.room.member", event.sender)]
state_event = await self.store.get_event(state_event_id)
Expand Down Expand Up @@ -753,9 +751,9 @@ async def _make_summary_text_from_member_events(
# are already in descending received_ts.
sender_ids = {}
for n in notifs:
sender = notif_events[n["event_id"]].sender
sender = notif_events[n.event_id].sender
if sender not in sender_ids:
sender_ids[sender] = n["event_id"]
sender_ids[sender] = n.event_id

# Get the actual member events (in order to calculate a pretty name for
# the room).
Expand Down Expand Up @@ -830,17 +828,17 @@ def _make_notif_link(self, notif: EmailPushAction) -> str:
if self.hs.config.email.email_riot_base_url:
return "%s/#/room/%s/%s" % (
self.hs.config.email.email_riot_base_url,
notif["room_id"],
notif["event_id"],
notif.room_id,
notif.event_id,
)
elif self.app_name == "Vector":
# need /beta for Universal Links to work on iOS
return "https://vector.im/beta/#/room/%s/%s" % (
notif["room_id"],
notif["event_id"],
notif.room_id,
notif.event_id,
)
else:
return "https://matrix.to/#/%s/%s" % (notif["room_id"], notif["event_id"])
return "https://matrix.to/#/%s/%s" % (notif.room_id, notif.event_id)

def _make_unsubscribe_link(
self, user_id: str, app_id: str, email_address: str
Expand Down
4 changes: 2 additions & 2 deletions synapse/push/push_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ async def get_badge_count(store: DataStore, user_id: str, group_by_room: bool) -
room_id, user_id, last_unread_event_id
)
)
if notifs["notify_count"] == 0:
if notifs.notify_count == 0:
continue

if group_by_room:
# return one badge count per conversation
badge += 1
else:
# increment the badge count by the number of unread messages in the room
badge += notifs["notify_count"]
badge += notifs.notify_count
return badge


Expand Down
20 changes: 10 additions & 10 deletions synapse/rest/client/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
user_id, ReceiptTypes.READ
)

notif_event_ids = [pa["event_id"] for pa in push_actions]
notif_event_ids = [pa.event_id for pa in push_actions]
notif_events = await self.store.get_events(notif_event_ids)

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

for pa in push_actions:
returned_pa = {
"room_id": pa["room_id"],
"profile_tag": pa["profile_tag"],
"actions": pa["actions"],
"ts": pa["received_ts"],
"room_id": pa.room_id,
"profile_tag": pa.profile_tag,
"actions": pa.actions,
"ts": pa.received_ts,
"event": (
await self._event_serializer.serialize_event(
notif_events[pa["event_id"]],
notif_events[pa.event_id],
self.clock.time_msec(),
event_format=format_event_for_client_v2_without_room_id,
)
),
}

if pa["room_id"] not in receipts_by_room:
if pa.room_id not in receipts_by_room:
returned_pa["read"] = False
else:
receipt = receipts_by_room[pa["room_id"]]
receipt = receipts_by_room[pa.room_id]

returned_pa["read"] = (
receipt["topological_ordering"],
receipt["stream_ordering"],
) >= (pa["topological_ordering"], pa["stream_ordering"])
) >= (pa.topological_ordering, pa.stream_ordering)
returned_push_actions.append(returned_pa)
next_token = str(pa["stream_ordering"])
next_token = str(pa.stream_ordering)

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

Expand Down
Loading