From 4f973609c6eda09c58f0a1ee0df82c953d91aa0e Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Fri, 14 Apr 2023 02:04:29 +0200 Subject: [PATCH] message redaction --- matrix_pygmalion_bot/chatlog.py | 9 ++++++++- matrix_pygmalion_bot/core.py | 13 ++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/matrix_pygmalion_bot/chatlog.py b/matrix_pygmalion_bot/chatlog.py index 5737fc9..ef44e9a 100644 --- a/matrix_pygmalion_bot/chatlog.py +++ b/matrix_pygmalion_bot/chatlog.py @@ -52,7 +52,14 @@ class ChatHistory: def remove(self, num_items=1): for i in range(num_items): event_id, item = self.chat_history.popitem() - return item + return item + def remove_id(self, event_id): + if event_id in self.chat_history: + del self.chat_history[event_id] + else: + logger.warning("remove_id: could not delete event. event_id not found in chat history") + def exists_id(self, event_id): + return (event_id in self.chat_history) def add(self, event_id, timestamp, user_name, is_own_message, is_command, relates_to_event, message, language="en", english_original_message=None): chat_message = ChatMessage(event_id, timestamp, user_name, is_own_message, is_command, relates_to_event, message, language, english_original_message) self.chat_history[chat_message.event_id] = chat_message diff --git a/matrix_pygmalion_bot/core.py b/matrix_pygmalion_bot/core.py index e8f84ff..7b0d7b9 100644 --- a/matrix_pygmalion_bot/core.py +++ b/matrix_pygmalion_bot/core.py @@ -1,6 +1,6 @@ import asyncio import nio -from nio import (AsyncClient, AsyncClientConfig, MatrixRoom, RoomMessageText, InviteEvent, UploadResponse) +from nio import (AsyncClient, AsyncClientConfig, MatrixRoom, RoomMessageText, InviteEvent, UploadResponse, RedactionEvent) import os, sys import time @@ -265,6 +265,16 @@ class Callbacks(object): else: logger.error("Error joining room: {}".format(str(result))) + async def redaction_cb(self, room: MatrixRoom, event: RedactionEvent) -> None: + logger.info(f"event redacted in room {room.room_id}. event_id: {event.redacts}") + for bot in bots: +# for room in bot.chat_history.chat_rooms.keys(): + if room.display_name in bot.chat_history.chat_rooms: + logger.info("room found") + if bot.chat_history.chat_rooms[room.display_name].exists_id(event.redacts): + logger.info("found it") + bot.chat_history.chat_rooms[room.display_name].remove_id(event.redacts) + class ChatBot(object): """Main chatbot""" @@ -338,6 +348,7 @@ class ChatBot(object): self.callbacks = Callbacks(self.client, self) self.client.add_event_callback(self.callbacks.message_cb, RoomMessageText) self.client.add_event_callback(self.callbacks.invite_cb, InviteEvent) + self.client.add_event_callback(self.callbacks.redaction_cb, RedactionEvent) sync_task = asyncio.create_task(self.watch_for_sync(self.client.synced)) event_loop = asyncio.create_task(self.event_loop())