Browse Source

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.
Kumi 6 months ago
parent
commit
e3ea1be260
1 changed files with 2 additions and 112 deletions
  1. 2 112
      src/matrix_reportbot/classes/bot.py

+ 2 - 112
src/matrix_reportbot/classes/bot.py

@@ -18,7 +18,6 @@ from nio import (
 
 
 from typing import Optional, List
 from typing import Optional, List
 from configparser import ConfigParser
 from configparser import ConfigParser
-from io import BytesIO
 
 
 import uuid
 import uuid
 import json
 import json
@@ -57,7 +56,7 @@ class ReportBot:
         """
         """
         try:
         try:
             return json.loads(self.config["ReportBot"]["AllowedUsers"])
             return json.loads(self.config["ReportBot"]["AllowedUsers"])
-        except:
+        except Exception:
             return []
             return []
 
 
     @property
     @property
@@ -178,115 +177,6 @@ class ReportBot:
 
 
         return device_id
         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(
     async def send_message(
         self,
         self,
         room: MatrixRoom | str,
         room: MatrixRoom | str,
@@ -456,7 +346,7 @@ class ReportBot:
 
 
                 try:
                 try:
                     known_report = int(report_state["content"]["report"])
                     known_report = int(report_state["content"]["report"])
-                except:
+                except Exception:
                     known_report = 0
                     known_report = 0
 
 
                 self.logger.log(f"Processing reports since: {known_report}", "debug")
                 self.logger.log(f"Processing reports since: {known_report}", "debug")