diff --git a/bookwyrm/outgoing.py b/bookwyrm/outgoing.py index 69d1a4a8c..2a62fd047 100644 --- a/bookwyrm/outgoing.py +++ b/bookwyrm/outgoing.py @@ -332,12 +332,13 @@ def handle_favorite(user, status): fav_activity = favorite.to_activity() broadcast( user, fav_activity, privacy='direct', direct_recipients=[status.user]) - create_notification( - status.user, - 'FAVORITE', - related_user=user, - related_status=status - ) + if status.user.local: + create_notification( + status.user, + 'FAVORITE', + related_user=user, + related_status=status + ) def handle_unfavorite(user, status): @@ -355,6 +356,15 @@ def handle_unfavorite(user, status): favorite.delete() broadcast(user, fav_activity, direct_recipients=[status.user]) + # check for notification + if status.user.local: + notification = models.Notification.objects.filter( + user=status.user, related_user=user, + related_status=status, notification_type='FAVORITE' + ).first() + if notification: + notification.delete() + def handle_boost(user, status): ''' a user wishes to boost a status ''' @@ -375,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): @@ -393,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 6af78296c..67599673a 100644 --- a/bookwyrm/tests/test_outgoing.py +++ b/bookwyrm/tests/test_outgoing.py @@ -260,6 +260,47 @@ class Outgoing(TestCase): self.assertEqual(self.shelf.books.count(), 0) + def test_handle_reading_status_to_read(self): + ''' posts shelve activities ''' + shelf = self.local_user.shelf_set.get(identifier='to-read') + with patch('bookwyrm.broadcast.broadcast_task.delay'): + outgoing.handle_reading_status( + self.local_user, shelf, self.book, 'public') + status = models.GeneratedNote.objects.get() + self.assertEqual(status.user, self.local_user) + self.assertEqual(status.mention_books.first(), self.book) + self.assertEqual(status.content, 'wants to read') + + def test_handle_reading_status_reading(self): + ''' posts shelve activities ''' + shelf = self.local_user.shelf_set.get(identifier='reading') + with patch('bookwyrm.broadcast.broadcast_task.delay'): + outgoing.handle_reading_status( + self.local_user, shelf, self.book, 'public') + status = models.GeneratedNote.objects.get() + self.assertEqual(status.user, self.local_user) + self.assertEqual(status.mention_books.first(), self.book) + self.assertEqual(status.content, 'started reading') + + def test_handle_reading_status_read(self): + ''' posts shelve activities ''' + shelf = self.local_user.shelf_set.get(identifier='read') + with patch('bookwyrm.broadcast.broadcast_task.delay'): + outgoing.handle_reading_status( + self.local_user, shelf, self.book, 'public') + status = models.GeneratedNote.objects.get() + self.assertEqual(status.user, self.local_user) + self.assertEqual(status.mention_books.first(), self.book) + self.assertEqual(status.content, 'finished reading') + + def test_handle_reading_status_other(self): + ''' posts shelve activities ''' + with patch('bookwyrm.broadcast.broadcast_task.delay'): + outgoing.handle_reading_status( + self.local_user, self.shelf, self.book, 'public') + self.assertFalse(models.GeneratedNote.objects.exists()) + + def test_handle_imported_book(self): ''' goodreads import added a book, this adds related connections ''' shelf = self.local_user.shelf_set.filter(identifier='read').first() @@ -392,6 +433,17 @@ class Outgoing(TestCase): ).exists()) + def test_handle_delete_status(self): + ''' marks a status as deleted ''' + status = models.Status.objects.create( + user=self.local_user, content='hi') + self.assertFalse(status.deleted) + with patch('bookwyrm.broadcast.broadcast_task.delay'): + outgoing.handle_delete_status(self.local_user, status) + status.refresh_from_db() + self.assertTrue(status.deleted) + + def test_handle_status(self): ''' create a status ''' form = forms.CommentForm({ @@ -543,3 +595,111 @@ class Outgoing(TestCase): outgoing.format_links(url), 'openlibrary.org/search' \ '?q=arkady+strugatsky&mode=everything' % url) + + + def test_to_markdown(self): + ''' this is mostly handled in other places, but nonetheless ''' + text = '_hi_ and http://fish.com is rad' + result = outgoing.to_markdown(text) + self.assertEqual( + result, + '

hi and fish.com ' \ + 'is rad

') + + + def test_handle_favorite(self): + ''' create and broadcast faving a status ''' + status = models.Status.objects.create( + user=self.local_user, content='hi') + + with patch('bookwyrm.broadcast.broadcast_task.delay'): + outgoing.handle_favorite(self.remote_user, status) + fav = models.Favorite.objects.get() + self.assertEqual(fav.status, status) + self.assertEqual(fav.user, self.remote_user) + + notification = models.Notification.objects.get() + self.assertEqual(notification.notification_type, 'FAVORITE') + self.assertEqual(notification.user, self.local_user) + self.assertEqual(notification.related_user, self.remote_user) + + + def test_handle_unfavorite(self): + ''' unfav a status ''' + status = models.Status.objects.create( + user=self.local_user, content='hi') + with patch('bookwyrm.broadcast.broadcast_task.delay'): + outgoing.handle_favorite(self.remote_user, status) + + self.assertEqual(models.Favorite.objects.count(), 1) + self.assertEqual(models.Notification.objects.count(), 1) + + with patch('bookwyrm.broadcast.broadcast_task.delay'): + outgoing.handle_unfavorite(self.remote_user, status) + self.assertEqual(models.Favorite.objects.count(), 0) + self.assertEqual(models.Notification.objects.count(), 0) + + + def test_handle_boost(self): + ''' boost a status ''' + 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) + + boost = models.Boost.objects.get() + self.assertEqual(boost.boosted_status, status) + self.assertEqual(boost.user, self.remote_user) + self.assertEqual(boost.privacy, 'public') + + notification = models.Notification.objects.get() + self.assertEqual(notification.notification_type, 'BOOST') + self.assertEqual(notification.user, self.local_user) + self.assertEqual(notification.related_user, self.remote_user) + self.assertEqual(notification.related_status, status) + + def test_handle_boost_unlisted(self): + ''' boost a status ''' + status = models.Status.objects.create( + user=self.local_user, content='hi', privacy='unlisted') + + with patch('bookwyrm.broadcast.broadcast_task.delay'): + outgoing.handle_boost(self.remote_user, status) + + boost = models.Boost.objects.get() + self.assertEqual(boost.privacy, 'unlisted') + + def test_handle_boost_private(self): + ''' boost a status ''' + status = models.Status.objects.create( + user=self.local_user, content='hi', privacy='followers') + + with patch('bookwyrm.broadcast.broadcast_task.delay'): + outgoing.handle_boost(self.remote_user, status) + self.assertFalse(models.Boost.objects.exists()) + + def test_handle_boost_twice(self): + ''' boost a status ''' + 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) + 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 bc94f73cb..3de1dd9d7 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)