Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MSC2530 event format for inline message #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions base-config.yaml
Original file line number Diff line number Diff line change
@@ -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 <a> 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.
Expand Down
2 changes: 1 addition & 1 deletion maubot.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
89 changes: 61 additions & 28 deletions xkcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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}: <strong>{xkcd.safe_title}</strong><br/>"
f"<img src='{info.mxc_uri}' title='{xkcd.alt}'/>")
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}: <strong>{xkcd.safe_title}</strong>")
frebib marked this conversation as resolved.
Show resolved Hide resolved
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}: <strong>{xkcd.safe_title}</strong>",
)
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}: <strong>{xkcd.safe_title}</strong><br/>"
f"<img src='{info.mxc_uri}' title='{xkcd.alt}'/>",
)
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}: <strong>{xkcd.safe_title}</strong>"
f"<br/><i>{xkcd.alt}</i>",
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}")
Expand Down