microblog.pub/app/source.py

99 lines
3 KiB
Python
Raw Normal View History

2022-06-22 18:11:22 +00:00
import re
2022-08-24 18:12:10 +00:00
import typing
2022-06-22 18:11:22 +00:00
from markdown import markdown
2022-06-29 06:56:39 +00:00
from sqlalchemy import select
2022-06-22 18:11:22 +00:00
from app import webfinger
from app.config import BASE_URL
2022-06-29 18:43:17 +00:00
from app.database import AsyncSession
2022-06-27 18:55:44 +00:00
from app.utils import emoji
2022-06-22 18:11:22 +00:00
2022-08-24 18:12:10 +00:00
if typing.TYPE_CHECKING:
from app.actor import Actor
2022-06-22 18:11:22 +00:00
def _set_a_attrs(attrs, new=False):
attrs[(None, "target")] = "_blank"
attrs[(None, "class")] = "external"
attrs[(None, "rel")] = "noopener"
attrs[(None, "title")] = attrs[(None, "href")]
return attrs
_HASHTAG_REGEX = re.compile(r"(#[\d\w]+)")
_MENTION_REGEX = re.compile(r"@[\d\w_.+-]+@[\d\w-]+\.[\d\w\-.]+")
2022-08-24 18:12:10 +00:00
def hashtagify(content: str) -> tuple[str, list[dict[str, str]]]:
2022-06-22 18:11:22 +00:00
tags = []
hashtags = re.findall(_HASHTAG_REGEX, content)
hashtags = sorted(set(hashtags), reverse=True) # unique tags, longest first
for hashtag in hashtags:
tag = hashtag[1:]
link = f'<a href="{BASE_URL}/t/{tag}" class="mention hashtag" rel="tag">#<span>{tag}</span></a>' # noqa: E501
tags.append(dict(href=f"{BASE_URL}/t/{tag}", name=hashtag, type="Hashtag"))
content = content.replace(hashtag, link)
return content, tags
2022-06-29 18:43:17 +00:00
async def _mentionify(
db_session: AsyncSession,
2022-06-26 17:00:29 +00:00
content: str,
2022-08-24 18:12:10 +00:00
) -> tuple[str, list[dict[str, str]], list["Actor"]]:
from app import models
from app.actor import fetch_actor
2022-06-22 18:11:22 +00:00
tags = []
2022-06-26 17:00:29 +00:00
mentioned_actors = []
2022-06-22 18:11:22 +00:00
for mention in re.findall(_MENTION_REGEX, content):
_, username, domain = mention.split("@")
2022-06-29 18:43:17 +00:00
actor = (
await db_session.execute(
2022-08-31 17:44:40 +00:00
select(models.Actor).where(
models.Actor.handle == mention,
models.Actor.is_deleted.is_(False),
)
2022-06-29 18:43:17 +00:00
)
2022-06-29 06:56:39 +00:00
).scalar_one_or_none()
2022-06-22 18:11:22 +00:00
if not actor:
2022-06-29 22:28:07 +00:00
actor_url = await webfinger.get_actor_url(mention)
2022-06-22 18:11:22 +00:00
if not actor_url:
# FIXME(ts): raise an error?
continue
2022-06-29 18:43:17 +00:00
actor = await fetch_actor(db_session, actor_url)
2022-06-22 18:11:22 +00:00
2022-06-26 17:00:29 +00:00
mentioned_actors.append(actor)
2022-08-09 20:22:58 +00:00
tags.append(dict(type="Mention", href=actor.ap_id, name=mention))
2022-06-22 18:11:22 +00:00
link = f'<span class="h-card"><a href="{actor.url}" class="u-url mention">{actor.handle}</a></span>' # noqa: E501
2022-06-22 18:11:22 +00:00
content = content.replace(mention, link)
2022-06-26 17:00:29 +00:00
return content, tags, mentioned_actors
2022-06-22 18:11:22 +00:00
2022-06-29 18:43:17 +00:00
async def markdownify(
db_session: AsyncSession,
2022-06-22 18:11:22 +00:00
content: str,
2022-08-24 18:12:10 +00:00
enable_mentionify: bool = True,
enable_hashtagify: bool = True,
) -> tuple[str, list[dict[str, str]], list["Actor"]]:
2022-06-22 18:11:22 +00:00
"""
>>> content, tags = markdownify("Hello")
"""
tags = []
2022-08-24 18:12:10 +00:00
mentioned_actors: list["Actor"] = []
if enable_hashtagify:
content, hashtag_tags = hashtagify(content)
2022-06-22 18:11:22 +00:00
tags.extend(hashtag_tags)
2022-08-24 18:12:10 +00:00
if enable_mentionify:
2022-06-29 18:43:17 +00:00
content, mention_tags, mentioned_actors = await _mentionify(db_session, content)
2022-06-22 18:11:22 +00:00
tags.extend(mention_tags)
2022-06-27 18:55:44 +00:00
# Handle custom emoji
tags.extend(emoji.tags(content))
2022-07-12 20:24:15 +00:00
content = markdown(content, extensions=["mdx_linkify", "fenced_code"])
2022-06-27 18:55:44 +00:00
2022-06-26 17:00:29 +00:00
return content, tags, mentioned_actors