From 5defc1a87b152cf54c31a7b34744cc91ddc3ed7f Mon Sep 17 00:00:00 2001 From: Joe Groocock Date: Sat, 21 Sep 2024 00:21:14 +0100 Subject: [PATCH] Use MSC2530 event format for inline message Signed-off-by: Joe Groocock Signed-off-by: Joe Groocock --- base-config.yaml | 8 +++-- maubot.yaml | 2 +- xkcd.py | 89 +++++++++++++++++++++++++++++++++--------------- 3 files changed, 68 insertions(+), 31 deletions(-) diff --git a/base-config.yaml b/base-config.yaml index 67026cb..d07b6d4 100644 --- a/base-config.yaml +++ b/base-config.yaml @@ -1,5 +1,9 @@ -# Whether or not to use inline images for sending comics. -inline: false +# Send format of xkcd message events. Defaults to 'split'. +# Mode 'split' sends the image and alt text as separate events. Mode 'inline' +# sends a text message with the image embedded as a HTML tag (works best +# with browser-based clients). Mode 'caption' sends the image with the alt text +# as an MSC2530 caption (requires client support). +post_mode: split # The interval to poll xkcd for new comics in. poll_interval: 900 # The time to sleep between send requests when broadcasting a new xkcd comic. diff --git a/maubot.yaml b/maubot.yaml index fba326a..98b87ff 100644 --- a/maubot.yaml +++ b/maubot.yaml @@ -1,4 +1,4 @@ -maubot: 0.1.0 +maubot: 0.5.0 id: xyz.maubot.xkcd version: 1.2.0 license: AGPL-3.0-or-later diff --git a/xkcd.py b/xkcd.py index 2f2dcee..f4c0e53 100644 --- a/xkcd.py +++ b/xkcd.py @@ -121,7 +121,10 @@ def __init__(self, room_id: RoomID, requested_by: UserID) -> None: class Config(BaseProxyConfig): def do_update(self, helper: ConfigUpdateHelper) -> None: - helper.copy("inline") + if "inline" in self: + helper.base["post_mode"] = "inline" if self["inline"] else "split" + else: + helper.copy("post_mode") helper.copy("poll_interval") helper.copy("spam_sleep") helper.copy("allow_reindex") @@ -234,33 +237,63 @@ async def _send_xkcd(self, room_id: RoomID, xkcd: XKCDInfo) -> None: info = await self._get_media_info(xkcd.img) if not info: return - if self.config["inline"]: - content = TextMessageEventContent( - msgtype=MessageType.TEXT, format=Format.HTML, - external_url=f"https://xkcd.com/{xkcd.num}", - body=f"{xkcd.num}: **{xkcd.title}**\n" - f"{xkcd.img}\n{xkcd.alt}", - formatted_body=f"{xkcd.num}: {xkcd.safe_title}
" - f"") - content["license"] = "CC-BY-NC-2.5" - content["license_url"] = "https://xkcd.com/license.html" - await self.client.send_message(room_id, content) - else: - await self.client.send_text(room_id, text=f"{xkcd.num}: **{xkcd.title}**", - html=f"{xkcd.num}: {xkcd.safe_title}") - content = MediaMessageEventContent(url=info.mxc_uri, body=info.file_name, - msgtype=MessageType.IMAGE, - external_url=f"https://xkcd.com/{xkcd.num}", - info=ImageInfo( - mimetype=info.mime_type, - size=info.size, - width=info.width, - height=info.height, - ),) - content["license"] = "CC-BY-NC-2.5" - content["license_url"] = "https://xkcd.com/license.html" - await self.client.send_message(room_id, content) - await self.client.send_text(room_id, text=xkcd.alt) + match self.config["post_mode"]: + case "split": + await self.client.send_text( + room_id, + text=f"{xkcd.num}: **{xkcd.title}**", + html=f"{xkcd.num}: {xkcd.safe_title}", + ) + content = MediaMessageEventContent( + url=info.mxc_uri, + body=info.file_name, + msgtype=MessageType.IMAGE, + external_url=f"https://xkcd.com/{xkcd.num}", + info=ImageInfo( + mimetype=info.mime_type, + size=info.size, + width=info.width, + height=info.height, + ), + ) + content["license"] = "CC-BY-NC-2.5" + content["license_url"] = "https://xkcd.com/license.html" + await self.client.send_message(room_id, content) + await self.client.send_text(room_id, text=xkcd.alt) + + case "inline": + content = TextMessageEventContent( + msgtype=MessageType.TEXT, + format=Format.HTML, + external_url=f"https://xkcd.com/{xkcd.num}", + body=f"{xkcd.num}: **{xkcd.title}**\n" f"{xkcd.img}\n{xkcd.alt}", + formatted_body=f"{xkcd.num}: {xkcd.safe_title}
" + f"", + ) + content["license"] = "CC-BY-NC-2.5" + content["license_url"] = "https://xkcd.com/license.html" + await self.client.send_message(room_id, content) + + case "caption": + content = MediaMessageEventContent( + msgtype=MessageType.IMAGE, + format=Format.HTML, + external_url=f"https://xkcd.com/{xkcd.num}", + url=info.mxc_uri, + filename=info.file_name, + body=f"{xkcd.num}: **{xkcd.title}**\n{xkcd.alt}", + formatted_body=f"{xkcd.num}: {xkcd.safe_title}" + f"
{xkcd.alt}", + info=ImageInfo( + mimetype=info.mime_type, + size=info.size, + width=info.width, + height=info.height, + ), + ) + content["license"] = "CC-BY-NC-2.5" + content["license_url"] = "https://xkcd.com/license.html" + await self.client.send_message(room_id, content) async def broadcast(self, xkcd: XKCDInfo) -> None: self.log.debug(f"Broadcasting xkcd {xkcd.num}")