aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorben <ben@nagy.contact>2025-05-24 11:27:23 -0700
committerben <ben@nagy.contact>2025-05-24 11:27:23 -0700
commitc48357005abff959f44b8e7627fbb0976515f51f (patch)
tree5be8f685c7292ef41bae680b713fd748d5300086
parent7de187585ab1780de86870a25bdd62c1558b587f (diff)
clean
-rw-r--r--matrix-redact.py85
1 files changed, 28 insertions, 57 deletions
diff --git a/matrix-redact.py b/matrix-redact.py
index 40f6b61..e7de4ea 100644
--- a/matrix-redact.py
+++ b/matrix-redact.py
@@ -65,6 +65,7 @@ async def client_login() -> Optional[AsyncClient]:
print("login was successful, returning client.")
return client
+# TODO: Can we fetch the total number of messages from `client.user_id` first, to show % progress?
async def redact_room(client: AsyncClient, room: MatrixRoom) -> None:
print(f"\nAll events sent by '{client.user_id}' in " +
f"'{room['room_id']}' ({room['display_name']}) will be deleted.")
@@ -77,8 +78,6 @@ async def redact_room(client: AsyncClient, room: MatrixRoom) -> None:
tracked = set()
events_redacted = 0
- print(f"start_token: {start_token}")
- print(f"current_token: {current_token}")
while True:
try:
@@ -100,44 +99,31 @@ async def redact_room(client: AsyncClient, room: MatrixRoom) -> None:
print("no more messages to fetch.")
break
+ tasks = []
for event in resp.chunk:
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.")
+ if (event_id in tracked
+ or not hasattr(event, 'sender')
+ or event.sender != client.user_id
+ or isinstance(event, RedactionEvent)
+ or isinstance(event, RedactedEvent)
+ or isinstance(event, (RoomMemberEvent,
+ RoomCreateEvent,
+ RoomEncryptionEvent,
+ RoomGuestAccessEvent,
+ RoomHistoryVisibilityEvent,
+ RoomJoinRulesEvent,
+ PowerLevelsEvent))):
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}")
+# NOTE:
+ # Fetching room events seems to not have a rate limit (or at least very minimally)
+ # Can we therefore in concurrency, fetch events "ahead" whenever redaction limits are returned?
+
redact_resp = await client.room_redact(
room['room_id'],
event_id,
@@ -150,11 +136,13 @@ async def redact_room(client: AsyncClient, room: MatrixRoom) -> None:
print(f"Successfully redacted event {event.event_id} in {room['room_id']}")
events_redacted += 1
print(f"current redactions: {events_redacted}")
-
- await asyncio.sleep(4)
+ await asyncio.sleep(1)
except Exception as e:
print(f"Error redacting event {event.event_id}: {e}")
+ for event in tracked:
+ print(event)
+
if events_redacted:
print(f"Total messages redacted: {events_redacted}")
else:
@@ -162,35 +150,14 @@ async def redact_room(client: AsyncClient, room: MatrixRoom) -> None:
async def main(args) -> None:
print("\n" + "="*80)
- print("WARNING: THIS PROGRAM PERFORMS A PERMANENT DESTRUCTIVE ACTION IN A PROVIDED MATRIX ROOM.")
- print("""
- This program will (applies only to a provided room and user ID):
- - Delete all user-uploaded media (images, files, etc.)
- - Delete all messages you've sent
- - Delete any other content you've posted
- - Operate *irreversibly* on the room ID you provide
- - Act as the logged-in user on the specified homeserver
-
- THIS ACTION IS DESTRUCTIVE AND CANNOT BE UNDONE.
-
- Make absolutely sure you understand what this program does before proceeding.
- We take zero liability for any misuse of this program.
- """)
+ print("WARNING: THIS PROGRAM PERFORMS A PERMANENT DESTRUCTIVE ACTION IN A PROVIDED MATRIX ROOM.\n")
+ print("Make absolutely sure you understand what this program does before proceeding.\n\n")
confirmation = input("Type 'YES I UNDERSTAND' to continue: ")
if confirmation != "YES I UNDERSTAND":
print("Exiting.")
sys.exit(1)
- x = random.randint(2, 12)
- a = random.randint(2, 12)
- b = a * x
-
- print(f"Solve this: {a} * x = {b}")
- if int(input("Answer: ").strip()) != x:
- print("Incorrect math solution. Exiting.")
- sys.exit(1)
-
client = None
try:
client = await client_login()
@@ -217,4 +184,8 @@ if __name__ == "__main__":
description="matrix-mcnt: Matrix Unread Message Count"
)
+ # TODO:
+ # - delete just from a specific date range
+ # - delete the first a to b messages in DIRECTION
+
asyncio.run(main(parser.parse_args()))