Browse Source

message redaction

master
Hendrik Langer 2 years ago
parent
commit
4f973609c6
  1. 7
      matrix_pygmalion_bot/chatlog.py
  2. 13
      matrix_pygmalion_bot/core.py

7
matrix_pygmalion_bot/chatlog.py

@ -53,6 +53,13 @@ class ChatHistory:
for i in range(num_items): for i in range(num_items):
event_id, item = self.chat_history.popitem() 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): 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) 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 self.chat_history[chat_message.event_id] = chat_message

13
matrix_pygmalion_bot/core.py

@ -1,6 +1,6 @@
import asyncio import asyncio
import nio 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 os, sys
import time import time
@ -265,6 +265,16 @@ class Callbacks(object):
else: else:
logger.error("Error joining room: {}".format(str(result))) 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): class ChatBot(object):
"""Main chatbot""" """Main chatbot"""
@ -338,6 +348,7 @@ class ChatBot(object):
self.callbacks = Callbacks(self.client, self) self.callbacks = Callbacks(self.client, self)
self.client.add_event_callback(self.callbacks.message_cb, RoomMessageText) 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.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)) sync_task = asyncio.create_task(self.watch_for_sync(self.client.synced))
event_loop = asyncio.create_task(self.event_loop()) event_loop = asyncio.create_task(self.event_loop())

Loading…
Cancel
Save