From aaa8411cb97796d38b226c483a9c330c29e8a175 Mon Sep 17 00:00:00 2001 From: miruka Date: Thu, 19 Dec 2019 16:52:21 -0400 Subject: [PATCH] Room.last_event: be an Event instead of dict --- TODO.md | 2 -- src/backend/matrix_client.py | 8 ++++---- src/backend/models/items.py | 20 +++++++++++++++----- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/TODO.md b/TODO.md index 58331bac..a1348dc4 100644 --- a/TODO.md +++ b/TODO.md @@ -5,7 +5,6 @@ ## Media -- Clean up gui test md - nio ClientTimeout - upload delay at the end? @@ -49,7 +48,6 @@ ## Issues -- last event obj - load_past raise - Room pane slightly overlaps chat at small width - invisible uploaded mxc images? diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index 6d1efc17..77d106a7 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -913,7 +913,7 @@ class MatrixClient(nio.AsyncClient): room = model[room_id] if room.last_event is None: - room.last_event = item.serialized + room.last_event = item if item.is_local_echo: model.sync_now() @@ -924,7 +924,7 @@ class MatrixClient(nio.AsyncClient): # If there were no better events available to show previously prev_is_profile_ev = \ - room.last_event["type_specifier"] == TypeSpecifier.profile_change + room.last_event.type_specifier == TypeSpecifier.profile_change # If this is a profile event, only replace the currently shown one if # it was also a profile event (we had nothing better to show). @@ -933,10 +933,10 @@ class MatrixClient(nio.AsyncClient): # If this event is older than the currently shown one, only replace # it if the previous was a profile event. - if item.date < room.last_event["date"] and not prev_is_profile_ev: + if item.date < room.last_event.date and not prev_is_profile_ev: return - room.last_event = item.serialized + room.last_event = item if item.is_local_echo: model.sync_now() diff --git a/src/backend/models/items.py b/src/backend/models/items.py index a2307b39..f9852c5c 100644 --- a/src/backend/models/items.py +++ b/src/backend/models/items.py @@ -72,8 +72,7 @@ class Room(ModelItem): can_set_join_rules: bool = False can_set_guest_access: bool = False - # Event.serialized - last_event: Optional[Dict[str, Any]] = field(default=None, repr=False) + last_event: Optional["Event"] = field(default=None, repr=False) def __lt__(self, other: "Room") -> bool: """Sort by join state, then descending last event date, then name. @@ -89,7 +88,7 @@ class Room(ModelItem): other.inviter_id, - other.last_event["date"] if other.last_event else + other.last_event.date if other.last_event else datetime.fromtimestamp(0), self.display_name.lower() or self.room_id, @@ -98,7 +97,7 @@ class Room(ModelItem): self.inviter_id, - self.last_event["date"] if self.last_event else + self.last_event.date if self.last_event else datetime.fromtimestamp(0), other.display_name.lower() or other.room_id, @@ -111,11 +110,22 @@ class Room(ModelItem): return " ".join(( self.display_name, self.topic, - re.sub(r"<.*?>", "", self.last_event["inline_content"]) + re.sub(r"<.*?>", "", self.last_event.inline_content) if self.last_event else "", )) + @property + def serialized(self) -> Dict[str, Any]: + dct = super().serialized + + if self.last_event is not None: + dct["last_event"] = self.last_event.serialized + + return dct + + + @dataclass class Member(ModelItem): """A member in a matrix room."""