From fc878e75374f0dad6b2a56c45918c57fc407d5c0 Mon Sep 17 00:00:00 2001 From: vSLG Date: Sun, 29 Mar 2020 19:06:13 -0300 Subject: [PATCH] Fix event redaction and remove junk code --- src/backend/matrix_client.py | 74 +------------------ src/backend/models/items.py | 1 - src/backend/models/model.py | 18 ----- src/backend/nio_callbacks.py | 31 +++++++- src/gui/Pages/Chat/Timeline/EventDelegate.qml | 8 +- src/gui/Utils.qml | 2 +- 6 files changed, 38 insertions(+), 96 deletions(-) diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index f9cc5fac..b1a3a551 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -1186,7 +1186,8 @@ class MatrixClient(nio.AsyncClient): async def register_nio_event( - self, room: nio.MatrixRoom, ev: nio.Event, **fields, + self, room: nio.MatrixRoom, ev: nio.Event, event_id: str = None, + **fields, ) -> None: """Register a `nio.Event` as a `Event` object in our model.""" @@ -1208,24 +1209,14 @@ class MatrixClient(nio.AsyncClient): content, inline=True, room_id=room.room_id, ) - age = 0 - with suppress(Exception): - age = ev.source.get("age") - age = age or 0 - # Create Event ModelItem item = Event( - id = ev.event_id, + id = event_id or ev.event_id, event_id = ev.event_id, event_type = type(ev), source = ev, - date = datetime.fromtimestamp( - ( - datetime.fromtimestamp(ev.server_timestamp / 1000) - - datetime.fromtimestamp(age / 1000) - ).total_seconds() - ), + date = datetime.fromtimestamp(ev.server_timestamp / 1000), sender_id = ev.sender, sender_name = sender_name, sender_avatar = sender_avatar, @@ -1253,60 +1244,3 @@ class MatrixClient(nio.AsyncClient): model[item.id] = item await self.set_room_last_event(room.room_id, item) - - - async def register_redact_event( - self, room: nio.MatrixRoom, ev: nio.Event, **fields - ) -> None: - """Register a `nio.events.room_events.RedactionEvent` in our model. - - If .redacts attribute points to an existing model, it should get replaced. - Else register as a new Event. - """ - - model = self.models[self.user_id, room.room_id, "events"] - event = model.get_event_id(ev.redacts) - - if event: - sender_name, sender_avatar = \ - await self.get_member_name_avatar(room.room_id, ev.sender) - - target_id = getattr(ev, "state_key", "") or "" - - target_name, target_avatar = \ - await self.get_member_name_avatar(room.room_id, target_id) \ - if target_id else ("", "") - - content = fields.get("content", "").strip() - - if content and "inline_content" not in fields: - fields["inline_content"] = HTML.filter( - content, inline=True, room_id=room.room_id, - ) - - age = ev.source["unsigned"]["age"] - - item = Event( - id = ev.event_id, - event_id = ev.event_id, - event_type = type(ev), - source = ev, - date = datetime.fromtimestamp( - ( - datetime.fromtimestamp(ev.server_timestamp / 1000) - - datetime.fromtimestamp(age / 1000) - ).total_seconds() - ), - sender_id = ev.sender, - sender_name = sender_name, - sender_avatar = sender_avatar, - target_id = target_id, - target_name = target_name, - target_avatar = target_avatar, - links = Event.parse_links(content), - **fields, - ) - - model[event.id] = item - else: - await self.register_nio_event(room, ev, **fields) diff --git a/src/backend/models/items.py b/src/backend/models/items.py index e865e263..4157b60a 100644 --- a/src/backend/models/items.py +++ b/src/backend/models/items.py @@ -177,7 +177,6 @@ class Event(ModelItem): content: str = "" inline_content: str = "" - redacts: str = "" reason: str = "" links: List[str] = field(default_factory=list) mentions: List[Tuple[str, str]] = field(default_factory=list) diff --git a/src/backend/models/model.py b/src/backend/models/model.py index 22fe5882..ea53f749 100644 --- a/src/backend/models/model.py +++ b/src/backend/models/model.py @@ -129,21 +129,3 @@ class Model(MutableMapping): new = type(self)(sync_id=sync_id) new.update(self) return new - - - def get_event_id(self, event_id: str) -> "Model": - """Get an event from the internal dict by event_id field. - - This function exists because not every event is indexed with its - event_id field. - """ - - event = self._data.get(event_id) - if event and event.event_id == event_id: - return event - - for event in self._data.values(): - if event.event_id == event_id: - return event - - return None diff --git a/src/backend/nio_callbacks.py b/src/backend/nio_callbacks.py index 79bc7bd6..ebb2c4ab 100644 --- a/src/backend/nio_callbacks.py +++ b/src/backend/nio_callbacks.py @@ -162,8 +162,35 @@ class NioCallbacks: async def onRedactionEvent(self, room, ev) -> None: - await self.client.register_redact_event( - room, ev, redacts=ev.redacts, reason=ev.reason + model = self.client.models[self.client.user_id, room.room_id, "events"] + event = None + + for event in model._sorted_data: + if event.event_id == ev.redacts: + break + + if ( + event and + issubclass(event.event_type, nio.events.room_events.RoomMessage) + ): + event.source.source["content"] = {} + event.source.source["unsigned"] = { + "redacted_by": ev.event_id, + "redacted_because": ev.source, + } + + await self.client.register_nio_event( + room, + nio.events.room_events.RedactedEvent.from_dict( + event.source.source, + ), + event_id = event.id, + ) + + + async def onRedactedEvent(self, room, ev) -> None: + await self.client.register_nio_event( + room, ev, reason=ev.reason, ) diff --git a/src/gui/Pages/Chat/Timeline/EventDelegate.qml b/src/gui/Pages/Chat/Timeline/EventDelegate.qml index 0f121e28..244fc04d 100644 --- a/src/gui/Pages/Chat/Timeline/EventDelegate.qml +++ b/src/gui/Pages/Chat/Timeline/EventDelegate.qml @@ -1,5 +1,3 @@ -// SPDX-License-Identifier: LGPL-3.0-or-later - import QtQuick 2.12 import QtQuick.Layouts 1.12 import Clipboard 0.1 @@ -33,7 +31,7 @@ HColumnLayout { readonly property bool smallAvatar: compact readonly property bool collapseAvatar: combine readonly property bool hideAvatar: onRight - readonly property bool isRedacted: model.event_type === "RedactionEvent" + readonly property bool isRedacted: model.event_type === "RedactedEvent" readonly property bool hideNameLine: model.event_type === "RoomMessageEmote" || @@ -229,7 +227,9 @@ HColumnLayout { properties: ({ userId: chat.userId, roomId: chat.roomId, - eventIds: [model.event_id] + eventIds: eventList.selectedCount ? + eventList.getSortedChecked().map(ev => ev.event_id) : + [model.event_id] }) } diff --git a/src/gui/Utils.qml b/src/gui/Utils.qml index 475f5d6e..e3b821fe 100644 --- a/src/gui/Utils.qml +++ b/src/gui/Utils.qml @@ -200,7 +200,7 @@ QtObject { return qsTr(ev.content).arg(sender).arg(target) } - if (type === "RedactionEvent") { + if (type === "RedactedEvent") { return qsTr( "Removed message" + `${ev.reason ? ". Reason: " + ev.reason : ""}` +