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

Commit b9ca846

Browse files
committed
Use attrs for UserPushActions.
1 parent 684a25d commit b9ca846

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

synapse/rest/client/notifications.py

+10-10
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

synapse/storage/databases/main/event_push_actions.py

+26-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
import logging
16-
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
16+
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
1717

1818
import attr
1919

@@ -57,6 +57,13 @@ class EmailPushAction(HttpPushAction):
5757
received_ts: Optional[int]
5858

5959

60+
@attr.s(slots=True, frozen=True, auto_attribs=True)
61+
class UserPushAction(EmailPushAction):
62+
topological_ordering: int
63+
highlight: bool
64+
profile_tag: str
65+
66+
6067
@attr.s(slots=True, frozen=True, auto_attribs=True)
6168
class NotifCounts:
6269
notify_count: int
@@ -973,8 +980,10 @@ async def get_push_actions_for_user(
973980
before: Optional[str] = None,
974981
limit: int = 50,
975982
only_highlight: bool = False,
976-
) -> List[Dict[str, Any]]:
977-
def f(txn: LoggingTransaction) -> List[Dict[str, Any]]:
983+
) -> List[UserPushAction]:
984+
def f(
985+
txn: LoggingTransaction,
986+
) -> List[Tuple[str, str, int, int, str, bool, str, int]]:
978987
before_clause = ""
979988
if before:
980989
before_clause = "AND epa.stream_ordering < ?"
@@ -1001,12 +1010,22 @@ def f(txn: LoggingTransaction) -> List[Dict[str, Any]]:
10011010
" LIMIT ?" % (before_clause,)
10021011
)
10031012
txn.execute(sql, args)
1004-
return self.db_pool.cursor_to_dict(txn)
1013+
return txn.fetchall() # type: ignore[return-value]
10051014

10061015
push_actions = await self.db_pool.runInteraction("get_push_actions_for_user", f)
1007-
for pa in push_actions:
1008-
pa["actions"] = _deserialize_action(pa["actions"], pa["highlight"])
1009-
return push_actions
1016+
return [
1017+
UserPushAction(
1018+
event_id=row[0],
1019+
room_id=row[1],
1020+
stream_ordering=row[2],
1021+
actions=_deserialize_action(row[4], row[5]),
1022+
received_ts=row[7],
1023+
topological_ordering=row[3],
1024+
highlight=row[5],
1025+
profile_tag=row[6],
1026+
)
1027+
for row in push_actions
1028+
]
10101029

10111030

10121031
def _action_has_highlight(actions: List[Union[dict, str]]) -> bool:

0 commit comments

Comments
 (0)