Fixes handling incoming unshelve activities

This commit is contained in:
Mouse Reeve 2021-03-13 08:43:36 -08:00
parent 848edd2972
commit e1a8c4d260
3 changed files with 45 additions and 5 deletions

View file

@ -137,7 +137,7 @@ class Add(Verb):
def action(self): def action(self):
""" add obj to collection """ """ add obj to collection """
target = resolve_remote_id(self.target, refresh=False) target = resolve_remote_id(self.target, refresh=False)
# we want to related field that isn't the book, this is janky af sorry # we want to get the related field that isn't the book, this is janky af sorry
model = [t for t in type(target)._meta.related_objects if t.name != "edition"][ model = [t for t in type(target)._meta.related_objects if t.name != "edition"][
0 0
].related_model ].related_model
@ -153,7 +153,11 @@ class Remove(Verb):
def action(self): def action(self):
""" find and remove the activity object """ """ find and remove the activity object """
obj = self.object.to_model(save=False, allow_create=False) target = resolve_remote_id(self.target, refresh=False)
model = [t for t in type(target)._meta.related_objects if t.name != "edition"][
0
].related_model
obj = self.to_model(model=model, save=False, allow_create=False)
obj.delete() obj.delete()

View file

@ -362,14 +362,15 @@ class CollectionItemMixin(ActivitypubMixin):
""" broadcast a remove activity """ """ broadcast a remove activity """
activity = self.to_remove_activity() activity = self.to_remove_activity()
super().delete(*args, **kwargs) super().delete(*args, **kwargs)
self.broadcast(activity, self.user) if self.user.local:
self.broadcast(activity, self.user)
def to_add_activity(self): def to_add_activity(self):
""" AP for shelving a book""" """ AP for shelving a book"""
object_field = getattr(self, self.object_field) object_field = getattr(self, self.object_field)
collection_field = getattr(self, self.collection_field) collection_field = getattr(self, self.collection_field)
return activitypub.Add( return activitypub.Add(
id="%s#add" % self.remote_id, id=self.remote_id,
actor=self.user.remote_id, actor=self.user.remote_id,
object=object_field, object=object_field,
target=collection_field.remote_id, target=collection_field.remote_id,
@ -380,7 +381,7 @@ class CollectionItemMixin(ActivitypubMixin):
object_field = getattr(self, self.object_field) object_field = getattr(self, self.object_field)
collection_field = getattr(self, self.collection_field) collection_field = getattr(self, self.collection_field)
return activitypub.Remove( return activitypub.Remove(
id="%s#remove" % self.remote_id, id=self.remote_id,
actor=self.user.remote_id, actor=self.user.remote_id,
object=object_field, object=object_field,
target=collection_field.remote_id, target=collection_field.remote_id,

View file

@ -591,6 +591,41 @@ class Inbox(TestCase):
views.inbox.activity_task(activity) views.inbox.activity_task(activity)
self.assertEqual(shelf.books.first(), book) self.assertEqual(shelf.books.first(), book)
def test_handle_unshelve_book(self):
""" remove a book from a shelf """
work = models.Work.objects.create(title="work title")
book = models.Edition.objects.create(
title="Test",
remote_id="https://bookwyrm.social/book/37292",
parent_work=work,
)
shelf = models.Shelf.objects.create(user=self.remote_user, name="Test Shelf")
shelf.remote_id = "https://bookwyrm.social/user/mouse/shelf/to-read"
shelf.save()
shelfbook = models.ShelfBook.objects.create(
user=self.remote_user, shelf=shelf, book=book
)
self.assertEqual(shelf.books.first(), book)
self.assertEqual(shelf.books.count(), 1)
activity = {
"id": shelfbook.remote_id,
"type": "Remove",
"actor": "https://example.com/users/rat",
"object": {
"type": "Edition",
"title": "Test Title",
"work": work.remote_id,
"id": "https://bookwyrm.social/book/37292",
},
"target": "https://bookwyrm.social/user/mouse/shelf/to-read",
"@context": "https://www.w3.org/ns/activitystreams",
}
views.inbox.activity_task(activity)
self.assertFalse(shelf.books.exists())
@responses.activate @responses.activate
def test_handle_add_book_to_list(self): def test_handle_add_book_to_list(self):
""" listing a book """ """ listing a book """