mirror of
https://git.sr.ht/~tsileo/microblog.pub
synced 2024-12-22 13:14:28 +00:00
Improve mention parsing
This commit is contained in:
parent
9b75020c91
commit
c3eb44add7
1 changed files with 29 additions and 17 deletions
|
@ -1,6 +1,7 @@
|
||||||
import re
|
import re
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
from mistletoe import Document # type: ignore
|
from mistletoe import Document # type: ignore
|
||||||
from mistletoe.html_renderer import HTMLRenderer # type: ignore
|
from mistletoe.html_renderer import HTMLRenderer # type: ignore
|
||||||
from mistletoe.span_token import SpanToken # type: ignore
|
from mistletoe.span_token import SpanToken # type: ignore
|
||||||
|
@ -78,13 +79,17 @@ class CustomRenderer(HTMLRenderer):
|
||||||
|
|
||||||
def render_mention(self, token: Mention) -> str:
|
def render_mention(self, token: Mention) -> str:
|
||||||
mention = token.target
|
mention = token.target
|
||||||
|
suffix = ""
|
||||||
|
if mention.endswith("."):
|
||||||
|
mention = mention[:-1]
|
||||||
|
suffix = "."
|
||||||
actor = self.mentioned_actors.get(mention)
|
actor = self.mentioned_actors.get(mention)
|
||||||
if not actor:
|
if not actor:
|
||||||
return mention
|
return mention
|
||||||
|
|
||||||
self.tags.append(dict(type="Mention", href=actor.ap_id, name=mention))
|
self.tags.append(dict(type="Mention", href=actor.ap_id, name=mention))
|
||||||
|
|
||||||
link = f'<span class="h-card"><a href="{actor.url}" class="u-url mention">{actor.handle}</a></span>' # noqa: E501
|
link = f'<span class="h-card"><a href="{actor.url}" class="u-url mention">{actor.handle}</a></span>{suffix}' # noqa: E501
|
||||||
return link
|
return link
|
||||||
|
|
||||||
def render_hashtag(self, token: Hashtag) -> str:
|
def render_hashtag(self, token: Hashtag) -> str:
|
||||||
|
@ -118,6 +123,11 @@ async def _prefetch_mentioned_actors(
|
||||||
if mention in actors:
|
if mention in actors:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# XXX: the regex catches stuff like `@toto@example.com.`
|
||||||
|
if mention.endswith("."):
|
||||||
|
mention = mention[:-1]
|
||||||
|
|
||||||
|
try:
|
||||||
_, username, domain = mention.split("@")
|
_, username, domain = mention.split("@")
|
||||||
actor = (
|
actor = (
|
||||||
await db_session.execute(
|
await db_session.execute(
|
||||||
|
@ -135,6 +145,8 @@ async def _prefetch_mentioned_actors(
|
||||||
actor = await fetch_actor(db_session, actor_url)
|
actor = await fetch_actor(db_session, actor_url)
|
||||||
|
|
||||||
actors[mention] = actor
|
actors[mention] = actor
|
||||||
|
except Exception:
|
||||||
|
logger.exception(f"Failed to prefetch {mention}")
|
||||||
|
|
||||||
return actors
|
return actors
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue