Include attachment count in fedi previews

This commit is contained in:
Nexus 2024-08-15 11:32:08 +01:00
parent 225862a401
commit 497a8dc2b7

View file

@ -1,5 +1,5 @@
""" """
This module takes misskey links (e.g.) and provides a preview in a reply. This module takes fediverse links (e.g.) and provides a preview in a reply.
""" """
import logging import logging
import niobot import niobot
@ -11,15 +11,15 @@ if typing.TYPE_CHECKING:
from ..main import TortoiseIntegratedBot from ..main import TortoiseIntegratedBot
class MisskeyPreviewModule(niobot.Module): class FediversePreviewModule(niobot.Module):
bot: "TortoiseIntegratedBot" bot: "TortoiseIntegratedBot"
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@niobot.event("message") @niobot.event("message")
async def on_message(self, room: niobot.MatrixRoom, event: niobot.RoomMessage): async def on_message(self, room: niobot.MatrixRoom, event: niobot.RoomMessage):
supported_prefixes = self.bot.cfg.get("misskey_preview", {}) supported_prefixes = self.bot.cfg.get("fediverse_preview")
supported_prefixes = supported_prefixes.get("urls", ["https://fedi.transgender.ing"]) supported_prefixes = supported_prefixes.get("urls", ["https://fedi.transgender.ing"])
ignore = self.bot.cfg.get("misskey_preview", {}).get("ignore", []) ignore = supported_prefixes.get("ignore", [])
if not isinstance(event, niobot.RoomMessageText): if not isinstance(event, niobot.RoomMessageText):
return return
if event.sender in ignore or room.room_id in ignore or event.sender == self.bot.user_id: if event.sender in ignore or room.room_id in ignore or event.sender == self.bot.user_id:
@ -51,7 +51,7 @@ class MisskeyPreviewModule(niobot.Module):
self.log.warning("No text for post %s", post_id) self.log.warning("No text for post %s", post_id)
continue continue
self.log.info("Detected misskey post %s: %r", post_id, text) self.log.info("Detected fediverse post %s: %r", post_id, text)
rendered = await self.bot._markdown_to_html(text) rendered = await self.bot._markdown_to_html(text)
text_body = "<blockquote>%s</blockquote>" % rendered text_body = "<blockquote>%s</blockquote>" % rendered
body = "<a href=\"%s\">@%s:</a><br>%s" % ( body = "<a href=\"%s\">@%s:</a><br>%s" % (
@ -59,7 +59,9 @@ class MisskeyPreviewModule(niobot.Module):
username, username,
text_body, text_body,
) )
self.log.info("Sending misskey post %s", post_id) if data.get("media_attachments"):
body += "<p>{:,} attachments</p>".format(len(data["media_attachments"]))
self.log.info("Sending fediverse post %s", post_id)
await self.bot.send_message( await self.bot.send_message(
room, room,
body, body,