From e3ea1be260e24f96247d56bfb2053f1753eba820 Mon Sep 17 00:00:00 2001 From: Kumi Date: Sat, 18 May 2024 17:41:40 +0200 Subject: [PATCH] refactor(bot): remove file upload functionality Removed the methods `upload_file`, `send_image`, and `send_file` from the ReportBot class to streamline the bot's functionality, focusing on handling reports rather than managing file uploads. This change simplifies the bot's codebase and aligns with our goal to sharpen its focus on core reporting features. Additionally, updated bare `except:` statements to `except Exception:` for better error handling practices. --- src/matrix_reportbot/classes/bot.py | 114 +--------------------------- 1 file changed, 2 insertions(+), 112 deletions(-) diff --git a/src/matrix_reportbot/classes/bot.py b/src/matrix_reportbot/classes/bot.py index 7a6238a..3acb946 100644 --- a/src/matrix_reportbot/classes/bot.py +++ b/src/matrix_reportbot/classes/bot.py @@ -18,7 +18,6 @@ from nio import ( from typing import Optional, List from configparser import ConfigParser -from io import BytesIO import uuid import json @@ -57,7 +56,7 @@ class ReportBot: """ try: return json.loads(self.config["ReportBot"]["AllowedUsers"]) - except: + except Exception: return [] @property @@ -178,115 +177,6 @@ class ReportBot: return device_id - async def upload_file( - self, - file: bytes, - filename: str = "file", - mime: str = "application/octet-stream", - ) -> str: - """Upload a file to the homeserver. - - Args: - file (bytes): The file to upload. - filename (str, optional): The name of the file. Defaults to "file". - mime (str, optional): The MIME type of the file. Defaults to "application/octet-stream". - - Returns: - str: The MXC URI of the uploaded file. - """ - - bio = BytesIO(file) - bio.seek(0) - - response, _ = await self.matrix_client.upload( - bio, content_type=mime, filename=filename, filesize=len(file) - ) - - return response.content_uri - - async def send_image( - self, room: MatrixRoom, image: bytes, message: Optional[str] = None - ): - """Send an image to a room. - - Args: - room (MatrixRoom|str): The room to send the image to. - image (bytes): The image to send. - message (str, optional): The message to send with the image. Defaults to None. - """ - - if isinstance(room, MatrixRoom): - room = room.room_id - - self.logger.log( - f"Sending image of size {len(image)} bytes to room {room}", "debug" - ) - - bio = BytesIO(image) - img = Image.open(bio) - mime = Image.MIME[img.format] - - (width, height) = img.size - - self.logger.log( - f"Uploading - Image size: {width}x{height} pixels, MIME type: {mime}", - "debug", - ) - - content_uri = await self.upload_file(image, "image", mime) - - self.logger.log("Uploaded image - sending message...", "debug") - - content = { - "body": message or "", - "info": { - "mimetype": mime, - "size": len(image), - "w": width, - "h": height, - }, - "msgtype": "m.image", - "url": content_uri, - } - - status = await self.matrix_client.room_send(room, "m.room.message", content) - - self.logger.log("Sent image", "debug") - - async def send_file( - self, room: MatrixRoom, file: bytes, filename: str, mime: str, msgtype: str - ): - """Send a file to a room. - - Args: - room (MatrixRoom|str): The room to send the file to. - file (bytes): The file to send. - filename (str): The name of the file. - mime (str): The MIME type of the file. - """ - - if isinstance(room, MatrixRoom): - room = room.room_id - - self.logger.log( - f"Sending file of size {len(file)} bytes to room {room}", "debug" - ) - - content_uri = await self.upload_file(file, filename, mime) - - self.logger.log("Uploaded file - sending message...", "debug") - - content = { - "body": filename, - "info": {"mimetype": mime, "size": len(file)}, - "msgtype": msgtype, - "url": content_uri, - } - - status = await self.matrix_client.room_send(room, "m.room.message", content) - - self.logger.log("Sent file", "debug") - async def send_message( self, room: MatrixRoom | str, @@ -456,7 +346,7 @@ class ReportBot: try: known_report = int(report_state["content"]["report"]) - except: + except Exception: known_report = 0 self.logger.log(f"Processing reports since: {known_report}", "debug")