diff --git a/bookwyrm/outgoing.py b/bookwyrm/outgoing.py index 39369e70..2a62fd04 100644 --- a/bookwyrm/outgoing.py +++ b/bookwyrm/outgoing.py @@ -358,10 +358,12 @@ def handle_unfavorite(user, status): # check for notification if status.user.local: - models.Notification.objects.filter( + notification = models.Notification.objects.filter( user=status.user, related_user=user, related_status=status, notification_type='FAVORITE' - ).first().delete() + ).first() + if notification: + notification.delete() def handle_boost(user, status): @@ -383,12 +385,13 @@ def handle_boost(user, status): boost_activity = boost.to_activity() broadcast(user, boost_activity) - create_notification( - status.user, - 'BOOST', - related_user=user, - related_status=status - ) + if status.user.local: + create_notification( + status.user, + 'BOOST', + related_user=user, + related_status=status + ) def handle_unboost(user, status): @@ -401,12 +404,11 @@ def handle_unboost(user, status): boost.delete() broadcast(user, activity) - -def handle_update_book_data(user, item): - ''' broadcast the news about our book ''' - broadcast(user, item.to_update_activity(user)) - - -def handle_update_user(user): - ''' broadcast editing a user's profile ''' - broadcast(user, user.to_update_activity(user)) + # delete related notification + if status.user.local: + notification = models.Notification.objects.filter( + user=status.user, related_user=user, + related_status=status, notification_type='BOOST' + ).first() + if notification: + notification.delete() diff --git a/bookwyrm/tests/test_outgoing.py b/bookwyrm/tests/test_outgoing.py index 9e7e6945..67599673 100644 --- a/bookwyrm/tests/test_outgoing.py +++ b/bookwyrm/tests/test_outgoing.py @@ -688,3 +688,18 @@ class Outgoing(TestCase): outgoing.handle_boost(self.remote_user, status) outgoing.handle_boost(self.remote_user, status) 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) diff --git a/bookwyrm/view_actions.py b/bookwyrm/view_actions.py index bc94f73c..3de1dd9d 100644 --- a/bookwyrm/view_actions.py +++ b/bookwyrm/view_actions.py @@ -210,7 +210,7 @@ def edit_profile(request): user.avatar.save(filename, ContentFile(output.getvalue())) user.save() - outgoing.handle_update_user(user) + broadcast(user, user.to_update_activity(user)) return redirect('/user/%s' % request.user.localname) @@ -241,7 +241,7 @@ def edit_book(request, book_id): return TemplateResponse(request, 'edit_book.html', data) 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) @@ -288,7 +288,7 @@ def upload_cover(request, book_id): book.cover = form.files['cover'] 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) @@ -307,7 +307,7 @@ def add_description(request, book_id): book.description = description 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) @@ -328,7 +328,7 @@ def edit_author(request, author_id): return TemplateResponse(request, 'edit_author.html', data) 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)