-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add TelegramRenderer for notifications (#1255)
* Add TelegramRenderer * Don't wrap into in a code block * Put PlainRenderer and render_plain back * Put PlainRenderer and render_plain back (posts.py)
- Loading branch information
Showing
17 changed files
with
99 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import mistune | ||
|
||
LIST_BULLET_POINTS = {1: "• ", 2: "◦ ", 3: "▪ "} | ||
|
||
|
||
def get_bullet_point(level: int) -> str: | ||
return LIST_BULLET_POINTS[(level - 1) % len(LIST_BULLET_POINTS) + 1] | ||
|
||
|
||
def indent(level: int) -> str: | ||
return " " * (level - 1) | ||
|
||
|
||
def convert_bulet_to_ordered_list(text, level, start): | ||
current_indent = indent(level) | ||
prefix_to_change = f"{current_indent}{get_bullet_point(level)}" | ||
items = text.split("\n" + prefix_to_change) | ||
items[0] = items[0][len(prefix_to_change):] | ||
return "\n".join(f"{current_indent}{i}. {item}" for i, item in enumerate(items, start or 1)) | ||
|
||
|
||
class TelegramRenderer(mistune.HTMLRenderer): | ||
def image(self, src, alt="", title=None): | ||
if alt: | ||
return f'<a href="{src}">🏞 «{alt}»</a>' | ||
else: | ||
return f'<a href="{src}">🏞🔗</a>' | ||
|
||
def strikethrough(self, text): | ||
return f"<s>{text}</s>" | ||
|
||
def linebreak(self): | ||
return "\n" | ||
|
||
def paragraph(self, text): | ||
return text + "\n\n" | ||
|
||
def heading(self, text, level): | ||
return f"<b>{text}</b>\n\n" | ||
|
||
def newline(self): | ||
return "\n" | ||
|
||
def list(self, text, ordered, level, start=None): | ||
if ordered: | ||
text = convert_bulet_to_ordered_list(text, level, start) | ||
if level > 1: | ||
text = "\n" + text | ||
return text | ||
|
||
def list_item(self, text, level): | ||
return f"{indent(level)}{get_bullet_point(level)}{text}\n" | ||
|
||
def thematic_break(self): | ||
return '---\n' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
notifications/telegram/templates/messages/channel_post_announce.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{% if post.emoji %}{{ post.emoji }} {% endif %}<b>{% if post.prefix and post.prefix != post.emoji %}{{ post.prefix }} {% endif %}<a href="{{ settings.APP_HOST }}{% url "show_post" post.type post.slug %}">{{ post.title }}</a> {% if post.room %} [{{ post.room.title }}]{% endif %}</b> | ||
|
||
{% load posts %}{% render_plain post 350 %} | ||
{% load posts %}{% render_tg post 350 %} | ||
|
||
➜ {{ settings.APP_HOST }}{% url "show_post" post.type post.slug %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
💬 <b>Кто-то упомянул вас в комментарии под постом «<a href="{{ settings.APP_HOST }}{% url "show_post" comment.post.type comment.post.slug %}#comment-{{ comment.id }}">{{ comment.post.title }}</a>»</b> | ||
|
||
{% load posts %}<b>{{ comment.author.full_name }}:</b> {% render_plain comment 3000 %} | ||
{% load posts %}<b>{{ comment.author.full_name }}:</b> {% render_tg comment 3000 %} | ||
|
||
<i>↩ Вы можете ответить просто реплайнув на это сообщение</i> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
notifications/telegram/templates/messages/comment_to_thread.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
💬 <b>Новый реплай к вашему комментарию под постом «<a href="{{ settings.APP_HOST }}{% url "show_post" comment.post.type comment.post.slug %}#comment-{{ comment.id }}">{{ comment.post.title }}</a>»</b> | ||
|
||
{% load posts %}<b>{{ comment.author.full_name }}:</b> {% render_plain comment 3000 %} | ||
{% load posts %}<b>{{ comment.author.full_name }}:</b> {% render_tg comment 3000 %} | ||
|
||
<i>↩ Вы можете ответить просто реплайнув на это сообщение</i> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
💬 <b>Ваш чувак {{ comment.author.full_name }} оставил коммент к посту «<a href="{{ settings.APP_HOST }}{% url "show_post" comment.post.type comment.post.slug %}#comment-{{ comment.id }}">{{ comment.post.title }}</a>»</b> | ||
|
||
{% load posts %}<b>{{ comment.author.full_name }}:</b> {% render_plain comment 3000 %} | ||
{% load posts %}<b>{{ comment.author.full_name }}:</b> {% render_tg comment 3000 %} | ||
|
||
<i>↩ Вы можете ответить просто реплайнув на это сообщение</i> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Ваш чувак {{ post.author.full_name }} написал пост 👇 | ||
|
||
{% if post.emoji %}{{ post.emoji }} {% endif %}<b>{% if post.prefix %}{{ post.prefix }} {% endif %}<a href="{{ settings.APP_HOST }}{% url "show_post" post.type post.slug %}">{{ post.title }}</a> {% if post.room %} [{{ post.room.title }}]{% endif %}</b> | ||
{% load posts %}{% render_plain post 350 %} | ||
{% load posts %}{% render_tg post 350 %} |
2 changes: 1 addition & 1 deletion
2
notifications/telegram/templates/messages/moderator_comment.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
💬 <b>{% if not comment.reply_to_id %}Коммент{% else %}Реплай{% endif %} к «<a href="{{ settings.APP_HOST }}{% url "show_post" comment.post.type comment.post.slug %}#comment-{{ comment.id }}">{{ comment.post.title }}</a>»</b> от <a href="{{ settings.APP_HOST }}{% url "profile" comment.author.slug %}">{{ comment.author.slug }}</a> | ||
|
||
{% load posts %}{% if comment.title %}<b>{{ comment.title }}</b> {% endif %}{% render_plain comment 300 %} | ||
{% load posts %}{% if comment.title %}<b>{{ comment.title }}</b> {% endif %}{% render_tg comment 300 %} |
2 changes: 1 addition & 1 deletion
2
notifications/telegram/templates/messages/moderator_mention.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
🛎 <b>Кто-то позвал модератора в пост «<a href="{{ settings.APP_HOST }}{% url "show_post" comment.post.type comment.post.slug %}#comment-{{ comment.id }}">{{ comment.post.title }}</a>»</b> | ||
|
||
{% load posts %}<b>{{ comment.author.slug }}:</b> {% render_plain comment 3000 %} | ||
{% load posts %}<b>{{ comment.author.slug }}:</b> {% render_tg comment 3000 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
notifications/telegram/templates/messages/moderator_updated_intro.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{% load posts %}👁 <b>Юзер обновил интро:</b> <a href="{{ settings.APP_HOST }}{% url "profile" user.slug %}">{{ user.slug }}</a> | ||
|
||
<pre>{% render_plain intro 1000 %}</pre> | ||
{% render_tg intro 1000 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters