From 74d39c3e24629fb39bcc94907a461bc822f1ff98 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 10 Feb 2021 14:31:39 -0800 Subject: [PATCH] Move fav notifications to model --- bookwyrm/incoming.py | 7 ------- bookwyrm/models/favorite.py | 30 ++++++++++++++++++++++++++++++ bookwyrm/views/interaction.py | 16 ---------------- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/bookwyrm/incoming.py b/bookwyrm/incoming.py index ca53d919..09cda8d9 100644 --- a/bookwyrm/incoming.py +++ b/bookwyrm/incoming.py @@ -288,13 +288,6 @@ def handle_favorite(activity): if fav.user.local: return - status_builder.create_notification( - fav.status.user, - 'FAVORITE', - related_user=fav.user, - related_status=fav.status, - ) - @app.task def handle_unfavorite(activity): diff --git a/bookwyrm/models/favorite.py b/bookwyrm/models/favorite.py index 7d630cf5..3a7249f2 100644 --- a/bookwyrm/models/favorite.py +++ b/bookwyrm/models/favorite.py @@ -1,4 +1,5 @@ ''' like/fav/star a status ''' +from django.apps import apps from django.db import models from django.utils import timezone @@ -22,6 +23,35 @@ class Favorite(ActivityMixin, BookWyrmModel): self.user.save(broadcast=False) super().save(*args, **kwargs) + if self.status.user.local and self.status.user != self.user: + notification_model = apps.get_model( + 'bookwyrm.Notification', require_ready=True) + if notification_model.objects.filter( + user=self.status.user, related_user=self.user, + related_status=self.status, notification_type='FAVORITE' + ).exists(): + return + notification_model.objects.create( + user=self.status.user, + notification_type='FAVORITE', + related_user=self.user, + related_status=self.status + ) + + def delete(self, *args, **kwargs): + ''' delete and delete notifications ''' + # check for notification + if self.status.user.local: + notification_model = apps.get_model( + 'bookwyrm.Notification', require_ready=True) + notification = notification_model.objects.filter( + user=self.status.user, related_user=self.user, + related_status=self.status, notification_type='FAVORITE' + ).first() + if notification: + notification.delete() + super().delete(*args, **kwargs) + class Meta: ''' can't fav things twice ''' unique_together = ('user', 'status') diff --git a/bookwyrm/views/interaction.py b/bookwyrm/views/interaction.py index ebee4719..88813e60 100644 --- a/bookwyrm/views/interaction.py +++ b/bookwyrm/views/interaction.py @@ -26,13 +26,6 @@ class Favorite(View): # you already fav'ed that return HttpResponseBadRequest() - if status.user.local: - create_notification( - status.user, - 'FAVORITE', - related_user=request.user, - related_status=status - ) return redirect(request.headers.get('Referer', '/')) @@ -52,15 +45,6 @@ class Unfavorite(View): return HttpResponseNotFound() favorite.delete() - - # check for notification - if status.user.local: - notification = models.Notification.objects.filter( - user=status.user, related_user=request.user, - related_status=status, notification_type='FAVORITE' - ).first() - if notification: - notification.delete() return redirect(request.headers.get('Referer', '/'))