From ac57db537564eb6a08e2db8f8b519163bdd83b99 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 10 Feb 2021 16:00:02 -0800 Subject: [PATCH] Boost notifications --- bookwyrm/incoming.py | 10 +--------- bookwyrm/models/status.py | 36 +++++++++++++++++++++++++++++++++++ bookwyrm/views/interaction.py | 18 ------------------ 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/bookwyrm/incoming.py b/bookwyrm/incoming.py index 09cda8d9..a7ada52e 100644 --- a/bookwyrm/incoming.py +++ b/bookwyrm/incoming.py @@ -304,19 +304,11 @@ def handle_unfavorite(activity): def handle_boost(activity): ''' someone gave us a boost! ''' try: - boost = activitypub.Boost(**activity).to_model(models.Boost) + activitypub.Boost(**activity).to_model(models.Boost) except activitypub.ActivitySerializerError: # this probably just means we tried to boost an unknown status 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 def handle_unboost(activity): diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index d2768e28..ce4a18bf 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -59,6 +59,10 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel): notification_model = apps.get_model( '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 \ self.reply_parent.user.local: notification_model.objects.create( @@ -264,6 +268,38 @@ class Boost(ActivityMixin, Status): ) 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): ''' the user field is "actor" here instead of "attributedTo" ''' diff --git a/bookwyrm/views/interaction.py b/bookwyrm/views/interaction.py index 88813e60..a7fcc231 100644 --- a/bookwyrm/views/interaction.py +++ b/bookwyrm/views/interaction.py @@ -7,7 +7,6 @@ from django.utils.decorators import method_decorator from django.views import View from bookwyrm import models -from bookwyrm.status import create_notification # pylint: disable= no-self-use @@ -68,14 +67,6 @@ class Boost(View): privacy=status.privacy, 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', '/')) @@ -90,13 +81,4 @@ class Unboost(View): ).first() 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', '/'))