Mention and notify users when creating a status

This commit is contained in:
Mouse Reeve
2020-11-01 10:13:51 -08:00
parent 4684a83e6f
commit 0cf1838276
6 changed files with 83 additions and 12 deletions

View File

@ -1,5 +1,6 @@
''' handles all the activity coming out of the server '''
from datetime import datetime
import re
from django.db import IntegrityError, transaction
from django.http import HttpResponseNotFound, JsonResponse
@ -13,6 +14,7 @@ from bookwyrm.status import create_tag, create_notification
from bookwyrm.status import create_generated_note
from bookwyrm.status import delete_status
from bookwyrm.remote_user import get_or_create_remote_user
from bookwyrm.settings import DOMAIN
@csrf_exempt
@ -211,7 +213,37 @@ def handle_status(user, form):
''' generic handler for statuses '''
status = form.save()
# notify reply parent or (TODO) tagged users
# inspect the text for user tags
text = status.content
matches = re.finditer(
r'\W@[a-zA-Z_\-\.0-9]+(@[a-z-A-Z0-9_\-]+.[a-z]+)?',
text
)
for match in matches:
username = match.group().strip().split('@')[1:]
if len(username) == 1:
# this looks like a local user (@user), fill in the domain
username.append(DOMAIN)
username = '@'.join(username)
try:
mention_user = models.User.objects.get(username=username)
except models.User.DoesNotExist:
# we can ignore users we don't know about
continue
# add them to status mentions fk
status.mention_users.add(mention_user)
# create notification if the mentioned user is local
if mention_user.local:
create_notification(
mention_user,
'MENTION',
related_user=user,
related_status=status
)
status.save()
# notify reply parent or tagged users
if status.reply_parent and status.reply_parent.user.local:
create_notification(
status.reply_parent.user,