Deduplicate notifications in notification model

This commit is contained in:
Mouse Reeve 2021-02-10 16:21:29 -08:00
parent d9e65aa363
commit e0cfb009e4
5 changed files with 16 additions and 12 deletions

View file

@ -26,11 +26,6 @@ class Favorite(ActivityMixin, BookWyrmModel):
if self.status.user.local and self.status.user != self.user: if self.status.user.local and self.status.user != self.user:
notification_model = apps.get_model( notification_model = apps.get_model(
'bookwyrm.Notification', require_ready=True) '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( notification_model.objects.create(
user=self.status.user, user=self.status.user,
notification_type='FAVORITE', notification_type='FAVORITE',

View file

@ -64,7 +64,6 @@ class ImportJob(models.Model):
) )
class ImportItem(models.Model): class ImportItem(models.Model):
''' a single line of a csv being imported ''' ''' a single line of a csv being imported '''
job = models.ForeignKey( job = models.ForeignKey(

View file

@ -25,6 +25,21 @@ class Notification(BookWyrmModel):
notification_type = models.CharField( notification_type = models.CharField(
max_length=255, choices=NotificationType.choices) max_length=255, choices=NotificationType.choices)
def save(self, *args, **kwargs):
''' save, but don't make dupes '''
# there's probably a better way to do this
if self.__class__.objects.filter(
user=self.user,
related_book=self.related_book,
related_user=self.related_user,
related_status=self.related_status,
related_import=self.related_import,
related_list_item=self.related_list_item,
notification_type=self.notification_type,
).exists():
return
super().save(*args, **kwargs)
class Meta: class Meta:
''' checks if notifcation is in enum list for valid types ''' ''' checks if notifcation is in enum list for valid types '''
constraints = [ constraints = [

View file

@ -276,11 +276,6 @@ class Boost(ActivityMixin, Status):
notification_model = apps.get_model( notification_model = apps.get_model(
'bookwyrm.Notification', require_ready=True) '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( notification_model.objects.create(
user=self.boosted_status.user, user=self.boosted_status.user,
related_status=self.boosted_status, related_status=self.boosted_status,

View file

@ -30,7 +30,7 @@ class NotificationViews(TestCase):
def test_clear_notifications(self): def test_clear_notifications(self):
''' erase notifications ''' ''' erase notifications '''
models.Notification.objects.create( models.Notification.objects.create(
user=self.local_user, notification_type='MENTION') user=self.local_user, notification_type='FAVORITE')
models.Notification.objects.create( models.Notification.objects.create(
user=self.local_user, notification_type='MENTION', read=True) user=self.local_user, notification_type='MENTION', read=True)
self.assertEqual(models.Notification.objects.count(), 2) self.assertEqual(models.Notification.objects.count(), 2)