Moves status notifications into model

This commit is contained in:
Mouse Reeve 2021-02-10 14:13:36 -08:00
parent b22e56333f
commit 13d8ccb016
2 changed files with 27 additions and 21 deletions

View file

@ -52,6 +52,31 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
serialize_reverse_fields = [('attachments', 'attachment', 'id')] serialize_reverse_fields = [('attachments', 'attachment', 'id')]
deserialize_reverse_fields = [('attachments', 'attachment')] deserialize_reverse_fields = [('attachments', 'attachment')]
def save(self, *args, **kwargs):
''' save and notify '''
super().save(*args, **kwargs)
notification_model = apps.get_model(
'bookwyrm.Notification', require_ready=True)
if self.reply_parent and self.reply_parent.user.local:
notification_model.objects.create(
user=self.reply_parent.user,
notification_type='REPLY',
related_user=self.user,
related_status=self,
)
for mention_user in self.mention_users.all():
# avoid double-notifying about this status
if not mention_user.local or notification_model.objects.filter(
related_status=self, related_user=mention_user).exists():
continue
notification_model.objects.create(
user=mention_user,
notification_type='MENTION',
related_user=self.user,
related_status=self,
)
@property @property
def recipients(self): def recipients(self):
''' tagged users who definitely need to get this status in broadcast ''' ''' tagged users who definitely need to get this status in broadcast '''

View file

@ -10,7 +10,7 @@ from markdown import markdown
from bookwyrm import forms, models from bookwyrm import forms, models
from bookwyrm.sanitize_html import InputHtmlParser from bookwyrm.sanitize_html import InputHtmlParser
from bookwyrm.settings import DOMAIN from bookwyrm.settings import DOMAIN
from bookwyrm.status import create_notification, delete_status from bookwyrm.status import delete_status
from bookwyrm.utils import regex from bookwyrm.utils import regex
from .helpers import handle_remote_webfinger from .helpers import handle_remote_webfinger
@ -48,31 +48,12 @@ class CreateStatus(View):
r'<a href="%s">%s</a>\g<1>' % \ r'<a href="%s">%s</a>\g<1>' % \
(mention_user.remote_id, mention_text), (mention_user.remote_id, mention_text),
content) content)
# add reply parent to mentions and notify # add reply parent to mentions
if status.reply_parent: if status.reply_parent:
status.mention_users.add(status.reply_parent.user) status.mention_users.add(status.reply_parent.user)
if status.reply_parent.user.local:
create_notification(
status.reply_parent.user,
'REPLY',
related_user=request.user,
related_status=status
)
# deduplicate mentions # deduplicate mentions
status.mention_users.set(set(status.mention_users.all())) status.mention_users.set(set(status.mention_users.all()))
# create mention notifications
for mention_user in status.mention_users.all():
if status.reply_parent and mention_user == status.reply_parent.user:
continue
if mention_user.local:
create_notification(
mention_user,
'MENTION',
related_user=request.user,
related_status=status
)
# don't apply formatting to generated notes # don't apply formatting to generated notes
if not isinstance(status, models.GeneratedNote): if not isinstance(status, models.GeneratedNote):