Add redact_all_messages.py
This commit is contained in:
parent
e1a0a25c3a
commit
467ec88560
66
redact_all_messages.py
Normal file
66
redact_all_messages.py
Normal file
@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
import requests
|
||||
import sys
|
||||
from pprint import pformat
|
||||
|
||||
|
||||
# configure your session here
|
||||
homeserver = "matrix.thisisjoes.site"
|
||||
access_token = "syt_M4dpAzM_iQdfTcDDoiGhlOTDnz1r_5HfPcV"
|
||||
|
||||
room_id = sys.argv[1]
|
||||
|
||||
|
||||
def get_all_encrypted_messages(homeserver, room_id, access_token):
|
||||
messages = []
|
||||
next_batch = None
|
||||
while True:
|
||||
print("Reading messages...")
|
||||
params = {
|
||||
"dir": "b",
|
||||
"limit": 2,
|
||||
"filter": '{"types": ["m.room.encrypted"]}'
|
||||
}
|
||||
if next_batch:
|
||||
params["from"] = next_batch
|
||||
url = f"https://{homeserver}/_matrix/client/v3/rooms/{room_id}/messages"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {access_token}"
|
||||
}
|
||||
response = requests.get(url, params=params, headers=headers)
|
||||
response.raise_for_status()
|
||||
chunk = response.json()["chunk"]
|
||||
messages.extend(chunk)
|
||||
next_batch = response.json().get("end")
|
||||
if not next_batch:
|
||||
break
|
||||
return messages
|
||||
|
||||
|
||||
def redact_message(message):
|
||||
transaction_id = 0
|
||||
try:
|
||||
transaction_id = message['unsigned']['transaction_id'] + '1'
|
||||
except:
|
||||
pass
|
||||
event_id = message['event_id']
|
||||
h = homeserver
|
||||
r = room_id
|
||||
e = event_id
|
||||
t = transaction_id
|
||||
url = f"https://{h}/_matrix/client/r0/rooms/{r}/redact/{e}/{t}"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {access_token}"
|
||||
}
|
||||
response = requests.put(url, json={"reason":""}, headers=headers)
|
||||
print(pformat(response))
|
||||
response.raise_for_status()
|
||||
|
||||
|
||||
all_messages = get_all_encrypted_messages(homeserver, room_id, access_token)
|
||||
for message in all_messages:
|
||||
message_count = 0
|
||||
if message['content']:
|
||||
message_count += 1
|
||||
print("Message count:", message_count)
|
||||
redact_message(message)
|
Loading…
Reference in New Issue
Block a user