Add support for tags with optional emoji support

This commit is contained in:
Sophie Tauchert 2023-01-08 17:51:23 +01:00
parent e32b9c4b49
commit 942d105de2
No known key found for this signature in database
GPG Key ID: 52701DE5F5F51125
3 changed files with 53 additions and 10 deletions

View File

@ -12,3 +12,6 @@ database_type: asyncpg
config: true
extra_files:
- base-config.yaml
soft_dependencies:
- "emoji>=2.0"

View File

@ -14,6 +14,7 @@ from mautrix.util.formatter import parse_html
from .config import Config
from .db import DB, Topic, upgrade_table
from .emoji import parse_tags, WHITE_CHECK_MARK
class NtfyBot(Plugin):
@ -87,7 +88,7 @@ class NtfyBot(Plugin):
else:
await self.db.add_subscription(db_topic.id, evt.room_id)
await evt.reply("Subscribed this room to %s/%s", server, topic)
await evt.react("")
await evt.react(WHITE_CHECK_MARK)
if not existing_subscriptions:
await self.subscribe_to_topic(db_topic)
@ -108,7 +109,7 @@ class NtfyBot(Plugin):
return
await self.db.remove_subscription(db_topic.id, evt.room_id)
await evt.reply("Unsubscribed this room from %s/%s", server, topic)
await evt.react("")
await evt.react(WHITE_CHECK_MARK)
async def subscribe_to_topics(self) -> None:
topics = await self.db.get_topics()
@ -170,8 +171,7 @@ class NtfyBot(Plugin):
self.log.exception(
"Failed to send matrix message!", exc_info=exc)
@classmethod
def build_message_content(cls, server: str, message) -> str:
def build_message_content(self, server: str, message) -> str:
topic = message["topic"]
body = message["message"]
title = message.get("title", None)
@ -179,21 +179,35 @@ class NtfyBot(Plugin):
click = message.get("click", None)
attachment = message.get("attachment", None)
if tags:
(emoji, non_emoji) = parse_tags(self.log, tags)
emoji = "".join(emoji) + " "
tags = ", ".join(non_emoji)
else:
emoji = tags = ""
html_content = "<span>Ntfy message in topic <code>%s/%s</code></span><blockquote>" % (
html.escape(server), html.escape(topic))
# build title
if title and click:
html_content += "<h4><a href=\"%s\">%s</a></h4>" % (
html.escape(click), html.escape(title))
html_content += "<h4>%s<a href=\"%s\">%s</a></h4>" % (
emoji, html.escape(click), html.escape(title))
emoji = ""
elif title:
html_content += "<h4>%s</h4>" % html.escape(title)
html_content += "<h4>%s%s</h4>" % (emoji, html.escape(title))
emoji = ""
# build body
if click and not title:
html_content += "<a href=\"%s\">%s</a>" % (html.escape(
click), html.escape(body).replace("\n", "<br />"))
html_content += "%s<a href=\"%s\">%s</a>" % (
emoji, html.escape(click), html.escape(body).replace("\n", "<br />"))
else:
html_content += html.escape(body).replace("\n", "<br />")
html_content += emoji + html.escape(body).replace("\n", "<br />")
# add non-emoji tags
if tags:
html_content += "<br/><small>Tags: <code>%s</code></small>" % html.escape(
tags)
# build attachment
if attachment:

26
ntfy/emoji.py Normal file
View File

@ -0,0 +1,26 @@
from typing import List, Tuple
from mautrix.util.logging import TraceLogger
try:
import emoji
WHITE_CHECK_MARK = emoji.emojize(":white_check_mark:")
except ImportError:
emoji = None
WHITE_CHECK_MARK = ""
def parse_tags(log: TraceLogger, tags: List[str]) -> Tuple[List[str], List[str]]:
if emoji is None:
log.warn("Please install the `emoji` package for emoji support")
return ([], tags)
emojis = []
non_emoji_tags = []
for tag in tags:
emojized = emoji.emojize(f":{tag}:")
if emoji.is_emoji(emojized):
emojis.append(emojized)
else:
non_emoji_tags.append(tag)
return (emojis, non_emoji_tags)