From eb327749e8107927361755a4335293cef79ace30 Mon Sep 17 00:00:00 2001 From: LoaD Accumulator Date: Thu, 15 Jun 2023 17:11:10 +0200 Subject: [PATCH] [ADD] Don't spam rooms with errors. --- callbacks.py | 9 ++------- routes.py | 7 +------ utils.py | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/callbacks.py b/callbacks.py index 05cc87b..3f0c753 100644 --- a/callbacks.py +++ b/callbacks.py @@ -59,17 +59,12 @@ async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None: await utils.send_png(room.room_id, filename) - for tex in re.findall(latex_regex, event.body, re.M): + for tex in re.findall(latex_regex, event.formatted_body, re.M): try: filename = latex.render(event.sender, tex) await utils.send_png(room.room_id, filename) except FileNotFoundError as e: - content = { - "msgtype": "m.text", - "body": f"Couldn't parse LaTeX correctly.\n```{e.args[0]}\n```", - "formatted_body": f"Couldn't parse LaTeX correctly.
{e.args[0]}
", - } - await client.room_send(room.room_id, message_type="m.room.message", content=content) + await utils.send_plain(room.room_id, e.args[0]) except OSError: content = { "msgtype": "m.text", diff --git a/routes.py b/routes.py index eeaf6b5..cc250c3 100644 --- a/routes.py +++ b/routes.py @@ -43,12 +43,7 @@ async def route_lshow(router: Router, client: Client, room: str, event: RoomMess file = lilypond.render("", args[0], version=version) await utils.send_png(room, file) except FileNotFoundError as e: - content = { - "msgtype": "m.text", - "body": f"Couldn't parse LilyPond correctly.\n```{e.args[0]}\n```", - "formatted_body": f"Couldn't parse LilyPond correctly.
{e.args[0]}
", - } - await client.room_send(room, message_type="m.room.message", content=content) + await utils.send_plain(room, e.args[0]) except Exception: # ??? pass diff --git a/utils.py b/utils.py index a3ebb4d..939695d 100644 --- a/utils.py +++ b/utils.py @@ -64,5 +64,27 @@ async def send_png(room: str, filename: str) -> None: "url": resp.content_uri, } file.close() +async def send_plain(room: str, cnt: str) -> None: + import imagesize + import tempfile + import io + + b = bytes(cnt, 'utf8') + size = len(b) + + resp, keys = await client.upload(io.BytesIO(b), content_type="text/plain") + + if not isinstance(resp, UploadResponse): + raise OSError("Couldn't upload file.") + + content = { + "body": "error.log", + "info": { + "size": size, + "mimetype": "text/plain" + }, + "msgtype": "m.file", + "url": resp.content_uri, + } await client.room_send(room, message_type="m.room.message", content=content)