diff --git a/fedireads/models.py b/fedireads/models.py index 5720d886f..7feecce68 100644 --- a/fedireads/models.py +++ b/fedireads/models.py @@ -111,7 +111,8 @@ class ShelveActivity(Activity): shelf = models.ForeignKey('Shelf', on_delete=models.PROTECT) def save(self, *args, **kwargs): - self.activity_type = 'Add' + if not self.activity_type: + self.activity_type = 'Add' self.fedireads_type = 'Shelve' super().save(*args, **kwargs) diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py index 99c775404..ec9b8ea05 100644 --- a/fedireads/outgoing.py +++ b/fedireads/outgoing.py @@ -108,6 +108,51 @@ def handle_shelve(user, book, shelf): broadcast(user, activity, recipients) +def handle_unshelve(user, book, shelf): + ''' a local user is getting a book put on their shelf ''' + # update the database + row = models.ShelfBook.objects.get(book=book, shelf=shelf) + row.delete() + + # send out the activitypub action + summary = '%s removed %s from %s' % ( + user.username, + book.data['title'], + shelf.name + ) + + uuid = uuid4() + activity = { + '@context': 'https://www.w3.org/ns/activitystreams', + 'id': str(uuid), + 'summary': summary, + 'type': 'Remove', + 'actor': user.actor, + 'object': { + 'type': 'Document', + 'name': book.data['title'], + 'url': book.openlibrary_key + }, + 'target': { + 'type': 'Collection', + 'name': shelf.name, + 'id': shelf.activitypub_id + } + } + recipients = get_recipients(user, 'public') + + models.ShelveActivity( + uuid=uuid, + user=user, + content=activity, + shelf=shelf, + book=book, + activity_type='Remove', + ).save() + + broadcast(user, activity, recipients) + + def handle_review(user, book, name, content, rating): ''' post a review ''' review_uuid = uuid4() diff --git a/fedireads/templates/feed.html b/fedireads/templates/feed.html index 7fdc44e44..51ae43f1c 100644 --- a/fedireads/templates/feed.html +++ b/fedireads/templates/feed.html @@ -11,7 +11,10 @@
- + {% endfor %} {% endif %} @@ -21,7 +24,10 @@ - + {% endfor %} @@ -38,7 +44,7 @@ {{ book.authors.first.data.name }} {% if not book in user_books.all %} - diff --git a/fedireads/views.py b/fedireads/views.py index 2cc9aaddb..271176be1 100644 --- a/fedireads/views.py +++ b/fedireads/views.py @@ -180,12 +180,17 @@ def book_page(request, book_identifier): @csrf_exempt @login_required -def shelve(request, shelf_id, book_id): +def shelve(request, shelf_id, book_id, reshelve=True): ''' put a book on a user's shelf ''' - # TODO: handle "reshelving" book = models.Book.objects.get(id=book_id) - shelf = models.Shelf.objects.get(identifier=shelf_id) - api.handle_shelve(request.user, book, shelf) + desired_shelf = models.Shelf.objects.get(identifier=shelf_id) + if reshelve: + try: + current_shelf = models.Shelf.objects.get(user=request.user, book=book) + api.handle_unshelve(request.user, book, current_shelf) + except models.Shelf.DoesNotExist: + pass + api.handle_shelve(request.user, book, desired_shelf) return redirect('/')