mirror of
https://github.com/narrrl/holy-flavor-rater.git
synced 2026-11-03 16:41:27 +01:00
30 lines
713 B
Python
30 lines
713 B
Python
import re
|
|
|
|
from api.models import Notification, Rating, Reply, User
|
|
|
|
_MENTION_RE = re.compile(r"@(\w+)")
|
|
|
|
|
|
def parse_mentions(
|
|
text: str,
|
|
actor: User,
|
|
rating: Rating | None = None,
|
|
reply: Reply | None = None,
|
|
) -> None:
|
|
if not text:
|
|
return
|
|
for username in set(_MENTION_RE.findall(text)):
|
|
try:
|
|
recipient = User.objects.get(username__iexact=username)
|
|
except User.DoesNotExist:
|
|
continue
|
|
if recipient == actor:
|
|
continue
|
|
Notification.objects.get_or_create(
|
|
recipient=recipient,
|
|
actor=actor,
|
|
notification_type="mention",
|
|
rating=rating,
|
|
reply=reply,
|
|
)
|