Fix event redaction and remove junk code

This commit is contained in:
vSLG 2020-03-29 19:06:13 -03:00 committed by miruka
parent db1afc980e
commit fc878e7537
6 changed files with 38 additions and 96 deletions

View File

@ -1186,7 +1186,8 @@ class MatrixClient(nio.AsyncClient):
async def register_nio_event( 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: ) -> None:
"""Register a `nio.Event` as a `Event` object in our model.""" """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, 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 # Create Event ModelItem
item = Event( item = Event(
id = ev.event_id, id = event_id or ev.event_id,
event_id = ev.event_id, event_id = ev.event_id,
event_type = type(ev), event_type = type(ev),
source = ev, source = ev,
date = datetime.fromtimestamp( date = datetime.fromtimestamp(ev.server_timestamp / 1000),
(
datetime.fromtimestamp(ev.server_timestamp / 1000) -
datetime.fromtimestamp(age / 1000)
).total_seconds()
),
sender_id = ev.sender, sender_id = ev.sender,
sender_name = sender_name, sender_name = sender_name,
sender_avatar = sender_avatar, sender_avatar = sender_avatar,
@ -1253,60 +1244,3 @@ class MatrixClient(nio.AsyncClient):
model[item.id] = item model[item.id] = item
await self.set_room_last_event(room.room_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)

View File

@ -177,7 +177,6 @@ class Event(ModelItem):
content: str = "" content: str = ""
inline_content: str = "" inline_content: str = ""
redacts: str = ""
reason: str = "" reason: str = ""
links: List[str] = field(default_factory=list) links: List[str] = field(default_factory=list)
mentions: List[Tuple[str, str]] = field(default_factory=list) mentions: List[Tuple[str, str]] = field(default_factory=list)

View File

@ -129,21 +129,3 @@ class Model(MutableMapping):
new = type(self)(sync_id=sync_id) new = type(self)(sync_id=sync_id)
new.update(self) new.update(self)
return new 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

View File

@ -162,8 +162,35 @@ class NioCallbacks:
async def onRedactionEvent(self, room, ev) -> None: async def onRedactionEvent(self, room, ev) -> None:
await self.client.register_redact_event( model = self.client.models[self.client.user_id, room.room_id, "events"]
room, ev, redacts=ev.redacts, reason=ev.reason 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,
) )

View File

@ -1,5 +1,3 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick 2.12 import QtQuick 2.12
import QtQuick.Layouts 1.12 import QtQuick.Layouts 1.12
import Clipboard 0.1 import Clipboard 0.1
@ -33,7 +31,7 @@ HColumnLayout {
readonly property bool smallAvatar: compact readonly property bool smallAvatar: compact
readonly property bool collapseAvatar: combine readonly property bool collapseAvatar: combine
readonly property bool hideAvatar: onRight 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: readonly property bool hideNameLine:
model.event_type === "RoomMessageEmote" || model.event_type === "RoomMessageEmote" ||
@ -229,7 +227,9 @@ HColumnLayout {
properties: ({ properties: ({
userId: chat.userId, userId: chat.userId,
roomId: chat.roomId, roomId: chat.roomId,
eventIds: [model.event_id] eventIds: eventList.selectedCount ?
eventList.getSortedChecked().map(ev => ev.event_id) :
[model.event_id]
}) })
} }

View File

@ -200,7 +200,7 @@ QtObject {
return qsTr(ev.content).arg(sender).arg(target) return qsTr(ev.content).arg(sender).arg(target)
} }
if (type === "RedactionEvent") { if (type === "RedactedEvent") {
return qsTr( return qsTr(
"<i>Removed message" + "<i>Removed message" +
`${ev.reason ? ". Reason: " + ev.reason : ""}` + `${ev.reason ? ". Reason: " + ev.reason : ""}` +