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

Commit

Permalink
Use attrs for UserPushActions.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Dec 16, 2021
1 parent 684a25d commit b9ca846
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
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
33 changes: 26 additions & 7 deletions synapse/storage/databases/main/event_push_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import attr

Expand Down Expand Up @@ -57,6 +57,13 @@ class EmailPushAction(HttpPushAction):
received_ts: Optional[int]


@attr.s(slots=True, frozen=True, auto_attribs=True)
class UserPushAction(EmailPushAction):
topological_ordering: int
highlight: bool
profile_tag: str


@attr.s(slots=True, frozen=True, auto_attribs=True)
class NotifCounts:
notify_count: int
Expand Down Expand Up @@ -973,8 +980,10 @@ async def get_push_actions_for_user(
before: Optional[str] = None,
limit: int = 50,
only_highlight: bool = False,
) -> List[Dict[str, Any]]:
def f(txn: LoggingTransaction) -> List[Dict[str, Any]]:
) -> List[UserPushAction]:
def f(
txn: LoggingTransaction,
) -> List[Tuple[str, str, int, int, str, bool, str, int]]:
before_clause = ""
if before:
before_clause = "AND epa.stream_ordering < ?"
Expand All @@ -1001,12 +1010,22 @@ def f(txn: LoggingTransaction) -> List[Dict[str, Any]]:
" LIMIT ?" % (before_clause,)
)
txn.execute(sql, args)
return self.db_pool.cursor_to_dict(txn)
return txn.fetchall() # type: ignore[return-value]

push_actions = await self.db_pool.runInteraction("get_push_actions_for_user", f)
for pa in push_actions:
pa["actions"] = _deserialize_action(pa["actions"], pa["highlight"])
return push_actions
return [
UserPushAction(
event_id=row[0],
room_id=row[1],
stream_ordering=row[2],
actions=_deserialize_action(row[4], row[5]),
received_ts=row[7],
topological_ordering=row[3],
highlight=row[5],
profile_tag=row[6],
)
for row in push_actions
]


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

0 comments on commit b9ca846

Please sign in to comment.