diff --git a/exporter/callbacks.py b/exporter/callbacks.py index f4fa216..114b8f6 100644 --- a/exporter/callbacks.py +++ b/exporter/callbacks.py @@ -2,6 +2,7 @@ import copy import json import logging import os +import re import shutil import tempfile import time @@ -15,6 +16,8 @@ from nio import (AsyncClient, InviteMemberEvent, JoinError, MatrixRoom, RoomMess from exporter.export import download_mxc, zip_directory, upload_to_r2, trim_filename, fetch_events +CLEAN_ROOM_NAME_RE = re.compile('[^a-zA-Z0-9]') + class RoomExport: def __init__(self, room_id: str, allowed_again: datetime): @@ -82,7 +85,8 @@ class MatrixBotCallbacks: if event.sender == self.client.user_id: return - self.logger.info(f"Export for {room.room_id} requested by {event.sender}") + requestor = event.sender + self.logger.info(f'Export for "{room.name}" ({room.room_id}) requested by {requestor}') await self.client.room_read_markers(room.room_id, event.event_id, event.event_id) if not self.exports.check_allowed(room.room_id): @@ -103,11 +107,15 @@ class MatrixBotCallbacks: 'm.room.message', content ) - self.logger.info(f"Rejected export in {room.room_id}, {minutes_until_future} minutes remaining.") + self.logger.info(f'Rejected export in {room.room_id}, {minutes_until_future} minutes remaining.') return export_created = int(time.time()) - zipfile_name = f'{room.room_id.replace(":", "_").replace("!", "").replace(".", "")}-{int(time.time())}.zip' + clean_room_name_str = '' + if room.name: + clean_room_name = CLEAN_ROOM_NAME_RE.sub('', room.name) + clean_room_name_str = f'{clean_room_name}-' + zipfile_name = f'{clean_room_name_str}{room.room_id.replace(":", "_").replace("!", "").replace(".", "")}-{int(time.time())}.zip' zipfile_path = None temp_dir = None @@ -144,6 +152,11 @@ class MatrixBotCallbacks: exported_events.append(event.source) exported_data = { + 'room': { + 'id': room.room_id, + 'name': room.name + }, + 'requesting_user': requestor, 'exporting_user': self.client.user_id, 'created': export_created, 'events': sorted(exported_events, key=lambda x: x['origin_server_ts'])