aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorben <ben@nagy.contact>2025-05-19 22:15:11 -0700
committerben <ben@nagy.contact>2025-05-19 22:15:11 -0700
commit7de187585ab1780de86870a25bdd62c1558b587f (patch)
tree344ea58b268cbf56f91a778846775240722f3138
parente7efe8c2e4aeb5adb3b967677b2d51ff1bc57f72 (diff)
Corrected redact_room
- Fixed bug where we get into a redaction loop - only redact events from `client.user_id` - keep track of events - skipped events: RoomMemberEvent, RoomCreateEvent, RoomEncryptionEvent, RoomGuestAccessEvent, RoomHistoryVisibilityEvent, RoomJoinRulesEvent, PowerLevelsEvent - updated sleep duration
-rw-r--r--matrix-redact.py77
1 files changed, 58 insertions, 19 deletions
diff --git a/matrix-redact.py b/matrix-redact.py
index e355cca..40f6b61 100644
--- a/matrix-redact.py
+++ b/matrix-redact.py
@@ -18,8 +18,18 @@ from nio import(
RoomInfo,
MessageDirection,
RedactedEvent,
+ RedactionEvent,
RoomMessagesError,
RoomRedactError,
+
+ RoomMemberEvent,
+ RoomCreateEvent,
+ RoomEncryptionEvent,
+ RoomGuestAccessEvent,
+ RoomHistoryVisibilityEvent,
+ RoomJoinRulesEvent,
+ PowerLevelsEvent,
+
responses
)
from typing import Optional
@@ -65,11 +75,11 @@ async def redact_room(client: AsyncClient, room: MatrixRoom) -> None:
start_token = sync_resp.rooms.join[room['room_id']].timeline.prev_batch
current_token = start_token
- # BUG: This loops if messages are already redacted.
- # NOTE: I wonder if we need to continuously sync? So perhaps it is necessary that we modularize fetching room events
-
-# TODO: if response = 429 (ratelimited), should pausae.
+ tracked = set()
events_redacted = 0
+ print(f"start_token: {start_token}")
+ print(f"current_token: {current_token}")
+
while True:
try:
print(f"getting messages in {room['room_id']}")
@@ -91,35 +101,64 @@ async def redact_room(client: AsyncClient, room: MatrixRoom) -> None:
break
for event in resp.chunk:
- if event.sender == client.user_id:
- event_id = event.event_id
+ event_id = event.event_id
+ print(f"Event class: {type(event).__name__}, "
+ f"ID: {event_id}, "
+ f"Sender: {event.sender if hasattr(event, 'sender') else 'Unknown'}")
+
+ if event_id in tracked:
+ print(f"Event {event_id} already processed, skipping.")
+ continue
+
+ tracked.add(event_id)
+
+ if not hasattr(event, 'sender') or event.sender != client.user_id:
+ continue
+
+ if isinstance(event, (RedactionEvent)):
+ print(f"Skipping redaction event {event_id}")
+ continue
+
+ if isinstance(event, (RedactedEvent)):
+ print(f"Skipping already redacted event {event_id}")
+ continue
+
+ print(f"Processing event_id: {event_id} from {event.sender}")
+
+ if isinstance(event, (RoomMemberEvent,
+ RoomCreateEvent,
+ RoomEncryptionEvent,
+ RoomGuestAccessEvent,
+ RoomHistoryVisibilityEvent,
+ RoomJoinRulesEvent,
+ PowerLevelsEvent)):
+ print(f"Skipping room state event {event_id}")
+ continue
+
+
try:
+ print(f"attempting to redact event {event_id}")
redact_resp = await client.room_redact(
room['room_id'],
event_id,
- reason="test test test"
+ reason=""
)
if isinstance(redact_resp, RoomRedactError):
- print(f"Could not redact event {event_id} in {room['room_id']}.")
+ print(f"Could not redact event {event.event_id} in {room['room_id']}: {redact_resp}")
else:
- print(f"Successfuly redacted event {event_id} in {room['room_id']}.")
+ print(f"Successfully redacted event {event.event_id} in {room['room_id']}")
events_redacted += 1
- await asyncio.sleep(5)
+ print(f"current redactions: {events_redacted}")
+
+ await asyncio.sleep(4)
except Exception as e:
- print(f"Error redacting event {event_id}: {e}")
+ print(f"Error redacting event {event.event_id}: {e}")
if events_redacted:
print(f"Total messages redacted: {events_redacted}")
else:
- print("could not redact any messages.")
-
- # NOTE: need to ensure event belongs to correct user ID (ie. client.user_id)
-
- # https://github.com/8go/matrix-commander/blob/master/matrix_commander/matrix_commander.py#L6241
- # https://github.com/8go/matrix-commander/blob/master/matrix_commander/matrix_commander.py#L4314
- # https://github.com/8go/matrix-commander/blob/master/matrix_commander/matrix_commander.py#L4402
- # https://github.com/8go/matrix-commander/blob/master/matrix_commander/matrix_commander.py#L4188f
+ print("Could not redact any messages, room is probably redacted already")
async def main(args) -> None:
print("\n" + "="*80)