From 8af63a304ff98c2f0034275cc4e3a71ed0dbbb5a Mon Sep 17 00:00:00 2001 From: miruka Date: Fri, 3 Apr 2020 05:50:02 -0400 Subject: [PATCH] Redact local echo: same text as redaction event --- src/backend/matrix_client.py | 41 +++++++++++++++++++++++++++--------- src/backend/nio_callbacks.py | 21 ++++++------------ 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index 4b022869..99d0db7e 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -876,27 +876,48 @@ class MatrixClient(nio.AsyncClient): return (successes, errors) + async def get_redacted_event_content( + self, m_type: str, redacter: str, sender: str, reason: str = "", + ) -> str: + kind = m_type.split(".")[-1].replace("_", " ") + + if kind != "message": + kind = f"{kind} event" + + content = f"%1 removed this {kind}" if redacter == sender else \ + f"%1's {kind} was removed by %2" + + if reason: + content = f"{content}, reason: {reason}" + + return content + + async def room_mass_redact( self, room_id: str, reason: str, *event_ids: str, - ) -> List[nio.responses.RoomRedactResponse]: - """Redact events from a room in parallel. - - Returns a list of sucessful redacts. - """ + ) -> List[nio.RoomRedactResponse]: + """Redact events from a room in parallel.""" model = self.models[self.user_id, room_id, "events"] - gather_list = [] + tasks = [] for event in model._sorted_data: + m_type = event.source.source["type"] + if event.event_id in event_ids: event.is_local_echo = True - event.content = "Removing..." - event.event_type = nio.RedactedEvent - gather_list.append( + + event.content = await self.get_redacted_event_content( + m_type, self.user_id, event.sender_id, reason, + ) + + event.event_type = nio.RedactedEvent + + tasks.append( self.room_redact(room_id, event.event_id, reason), ) - return await asyncio.gather(*gather_list) + return await asyncio.gather(*tasks) async def generate_thumbnail( diff --git a/src/backend/nio_callbacks.py b/src/backend/nio_callbacks.py index 0f5f83ab..9d08679d 100644 --- a/src/backend/nio_callbacks.py +++ b/src/backend/nio_callbacks.py @@ -192,23 +192,16 @@ class NioCallbacks: async def onRedactedEvent(self, room, ev, event_id: str = "") -> None: - kind = ev.source["type"].split(".")[-1].replace("_", " ") - - if kind != "message": - kind = f"{kind} event" - - co = f"%1 removed this {kind}" if ev.redacter == ev.sender else \ - f"%1's {kind} was removed by %2" - - if ev.reason: - co = f"{co}, reason: {ev.reason}" - await self.client.register_nio_event( room, ev, - event_id = event_id, - reason = ev.reason or "", - content = co, + event_id = event_id, + reason = ev.reason or "", + + content = await self.client.get_redacted_event_content( + ev.source["type"], ev.redacter, ev.sender, ev.reason, + ), + redacter_id = ev.redacter or "", redacter_name = (await self.client.get_member_name_avatar(