Boost notifications

This commit is contained in:
Mouse Reeve 2021-02-10 16:00:02 -08:00
parent 6f748a6a24
commit ac57db5375
3 changed files with 37 additions and 27 deletions

View file

@ -304,19 +304,11 @@ def handle_unfavorite(activity):
def handle_boost(activity): def handle_boost(activity):
''' someone gave us a boost! ''' ''' someone gave us a boost! '''
try: try:
boost = activitypub.Boost(**activity).to_model(models.Boost) activitypub.Boost(**activity).to_model(models.Boost)
except activitypub.ActivitySerializerError: except activitypub.ActivitySerializerError:
# this probably just means we tried to boost an unknown status # this probably just means we tried to boost an unknown status
return return
if not boost.user.local:
status_builder.create_notification(
boost.boosted_status.user,
'BOOST',
related_user=boost.user,
related_status=boost.boosted_status,
)
@app.task @app.task
def handle_unboost(activity): def handle_unboost(activity):

View file

@ -59,6 +59,10 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
notification_model = apps.get_model( notification_model = apps.get_model(
'bookwyrm.Notification', require_ready=True) 'bookwyrm.Notification', require_ready=True)
if self.deleted:
notification_model.objects.filter(related_status=self).delete()
if self.reply_parent and self.reply_parent.user != self.user and \ if self.reply_parent and self.reply_parent.user != self.user and \
self.reply_parent.user.local: self.reply_parent.user.local:
notification_model.objects.create( notification_model.objects.create(
@ -264,6 +268,38 @@ class Boost(ActivityMixin, Status):
) )
activity_serializer = activitypub.Boost activity_serializer = activitypub.Boost
def save(self, *args, **kwargs):
''' save and notify '''
super().save(*args, **kwargs)
if not self.boosted_status.user.local:
return
notification_model = apps.get_model(
'bookwyrm.Notification', require_ready=True)
if notification_model.objects.filter(
user=self.boosted_status.user,
related_status=self.boosted_status,
related_user=self.user, notification_type='BOOST').exists():
return
notification_model.objects.create(
user=self.boosted_status.user,
related_status=self.boosted_status,
related_user=self.user,
notification_type='BOOST',
)
def delete(self, *args, **kwargs):
''' delete and un-notify '''
notification_model = apps.get_model(
'bookwyrm.Notification', require_ready=True)
notification_model.objects.filter(
user=self.boosted_status.user,
related_status=self.boosted_status,
related_user=self.user,
notification_type='BOOST',
).delete()
super().delete(*args, **kwargs)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
''' the user field is "actor" here instead of "attributedTo" ''' ''' the user field is "actor" here instead of "attributedTo" '''

View file

@ -7,7 +7,6 @@ from django.utils.decorators import method_decorator
from django.views import View from django.views import View
from bookwyrm import models from bookwyrm import models
from bookwyrm.status import create_notification
# pylint: disable= no-self-use # pylint: disable= no-self-use
@ -68,14 +67,6 @@ class Boost(View):
privacy=status.privacy, privacy=status.privacy,
user=request.user, user=request.user,
) )
if status.user.local:
create_notification(
status.user,
'BOOST',
related_user=request.user,
related_status=status
)
return redirect(request.headers.get('Referer', '/')) return redirect(request.headers.get('Referer', '/'))
@ -90,13 +81,4 @@ class Unboost(View):
).first() ).first()
boost.delete() boost.delete()
# delete related notification
if status.user.local:
notification = models.Notification.objects.filter(
user=status.user, related_user=request.user,
related_status=status, notification_type='BOOST'
).first()
if notification:
notification.delete()
return redirect(request.headers.get('Referer', '/')) return redirect(request.headers.get('Referer', '/'))