Move fav notifications to model

This commit is contained in:
Mouse Reeve
2021-02-10 14:31:39 -08:00
parent 106d442a0b
commit 74d39c3e24
3 changed files with 30 additions and 23 deletions

View File

@ -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')