remove boost notification on delete

This commit is contained in:
Mouse Reeve 2021-01-10 19:51:58 -08:00
parent ee96c01cc1
commit 62fd118016
3 changed files with 39 additions and 22 deletions

View file

@ -358,10 +358,12 @@ def handle_unfavorite(user, status):
# check for notification # check for notification
if status.user.local: if status.user.local:
models.Notification.objects.filter( notification = models.Notification.objects.filter(
user=status.user, related_user=user, user=status.user, related_user=user,
related_status=status, notification_type='FAVORITE' related_status=status, notification_type='FAVORITE'
).first().delete() ).first()
if notification:
notification.delete()
def handle_boost(user, status): def handle_boost(user, status):
@ -383,6 +385,7 @@ def handle_boost(user, status):
boost_activity = boost.to_activity() boost_activity = boost.to_activity()
broadcast(user, boost_activity) broadcast(user, boost_activity)
if status.user.local:
create_notification( create_notification(
status.user, status.user,
'BOOST', 'BOOST',
@ -401,12 +404,11 @@ def handle_unboost(user, status):
boost.delete() boost.delete()
broadcast(user, activity) broadcast(user, activity)
# delete related notification
def handle_update_book_data(user, item): if status.user.local:
''' broadcast the news about our book ''' notification = models.Notification.objects.filter(
broadcast(user, item.to_update_activity(user)) user=status.user, related_user=user,
related_status=status, notification_type='BOOST'
).first()
def handle_update_user(user): if notification:
''' broadcast editing a user's profile ''' notification.delete()
broadcast(user, user.to_update_activity(user))

View file

@ -688,3 +688,18 @@ class Outgoing(TestCase):
outgoing.handle_boost(self.remote_user, status) outgoing.handle_boost(self.remote_user, status)
outgoing.handle_boost(self.remote_user, status) outgoing.handle_boost(self.remote_user, status)
self.assertEqual(models.Boost.objects.count(), 1) self.assertEqual(models.Boost.objects.count(), 1)
def test_handle_unboost(self):
''' undo a boost '''
status = models.Status.objects.create(
user=self.local_user, content='hi')
with patch('bookwyrm.broadcast.broadcast_task.delay'):
outgoing.handle_boost(self.remote_user, status)
self.assertEqual(models.Boost.objects.count(), 1)
self.assertEqual(models.Notification.objects.count(), 1)
with patch('bookwyrm.broadcast.broadcast_task.delay'):
outgoing.handle_unboost(self.remote_user, status)
self.assertEqual(models.Boost.objects.count(), 0)
self.assertEqual(models.Notification.objects.count(), 0)

View file

@ -210,7 +210,7 @@ def edit_profile(request):
user.avatar.save(filename, ContentFile(output.getvalue())) user.avatar.save(filename, ContentFile(output.getvalue()))
user.save() user.save()
outgoing.handle_update_user(user) broadcast(user, user.to_update_activity(user))
return redirect('/user/%s' % request.user.localname) return redirect('/user/%s' % request.user.localname)
@ -241,7 +241,7 @@ def edit_book(request, book_id):
return TemplateResponse(request, 'edit_book.html', data) return TemplateResponse(request, 'edit_book.html', data)
book = form.save() book = form.save()
outgoing.handle_update_book_data(request.user, book) broadcast(request.user, book.to_update_activity(request.user))
return redirect('/book/%s' % book.id) return redirect('/book/%s' % book.id)
@ -288,7 +288,7 @@ def upload_cover(request, book_id):
book.cover = form.files['cover'] book.cover = form.files['cover']
book.save() book.save()
outgoing.handle_update_book_data(request.user, book) broadcast(request.user, book.to_update_activity(request.user))
return redirect('/book/%s' % book.id) return redirect('/book/%s' % book.id)
@ -307,7 +307,7 @@ def add_description(request, book_id):
book.description = description book.description = description
book.save() book.save()
outgoing.handle_update_book_data(request.user, book) broadcast(request.user, book.to_update_activity(request.user))
return redirect('/book/%s' % book.id) return redirect('/book/%s' % book.id)
@ -328,7 +328,7 @@ def edit_author(request, author_id):
return TemplateResponse(request, 'edit_author.html', data) return TemplateResponse(request, 'edit_author.html', data)
author = form.save() author = form.save()
outgoing.handle_update_book_data(request.user, author) broadcast(request.user, author.to_update_activity(request.user))
return redirect('/author/%s' % author.id) return redirect('/author/%s' % author.id)