Resolve and assign target collection for collection items

This commit is contained in:
Mouse Reeve 2021-04-08 15:40:02 -07:00
parent a7375c4c13
commit ddd05a68cf
2 changed files with 22 additions and 66 deletions

View file

@ -145,6 +145,13 @@ class Add(Verb):
object: CollectionItem
type: str = "Add"
def action(self):
""" figure out the target to assign the item to a collection """
target = resolve_remote_id(self.target)
item = self.object.to_model(save=False)
setattr(item, item.collection_field, target)
item.save()
@dataclass(init=False)
class Remove(Add):
@ -154,7 +161,7 @@ class Remove(Add):
def action(self):
""" find and remove the activity object """
obj = self.object.to_model(model=model, save=False, allow_create=False)
obj = self.object.to_model(save=False, allow_create=False)
obj.delete()

View file

@ -8,7 +8,7 @@ from bookwyrm import models, views
# pylint: disable=too-many-public-methods
class InboxActivities(TestCase):
class InboxAdd(TestCase):
""" inbox tests """
def setUp(self):
@ -32,17 +32,17 @@ class InboxActivities(TestCase):
inbox="https://example.com/users/rat/inbox",
outbox="https://example.com/users/rat/outbox",
)
work = models.Work.objects.create(title="work title")
self.book = models.Edition.objects.create(
title="Test",
remote_id="https://bookwyrm.social/book/37292",
parent_work=work,
)
models.SiteSettings.objects.create()
def test_handle_add_book_to_shelf(self):
""" shelving a book """
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()
@ -52,27 +52,19 @@ class InboxActivities(TestCase):
"type": "Add",
"actor": "https://example.com/users/rat",
"object": {
"type": "Edition",
"title": "Test Title",
"work": work.remote_id,
"id": "https://bookwyrm.social/book/37292",
"type": "ShelfItem",
"book": self.book.remote_id,
"id": "https://bookwyrm.social/listbook/6189",
},
"target": "https://bookwyrm.social/user/mouse/shelf/to-read",
"@context": "https://www.w3.org/ns/activitystreams",
}
views.inbox.activity_task(activity)
self.assertEqual(shelf.books.first(), book)
self.assertEqual(shelf.books.first(), self.book)
@responses.activate
def test_handle_add_book_to_list(self):
""" listing a book """
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,
)
responses.add(
responses.GET,
"https://bookwyrm.social/user/mouse/list/to-read",
@ -97,8 +89,9 @@ class InboxActivities(TestCase):
"type": "Add",
"actor": "https://example.com/users/rat",
"object": {
"actor": self.local_user.remote_id,
"type": "ListItem",
"book": self.edition.remote_id,
"book": self.book.remote_id,
"id": "https://bookwyrm.social/listbook/6189",
},
"target": "https://bookwyrm.social/user/mouse/list/to-read",
@ -109,49 +102,5 @@ class InboxActivities(TestCase):
booklist = models.List.objects.get()
listitem = models.ListItem.objects.get()
self.assertEqual(booklist.name, "Test List")
self.assertEqual(booklist.books.first(), book)
self.assertEqual(booklist.books.first(), self.book)
self.assertEqual(listitem.remote_id, "https://bookwyrm.social/listbook/6189")
@responses.activate
def test_handle_tag_book(self):
""" listing a book """
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,
)
responses.add(
responses.GET,
"https://www.example.com/tag/cool-tag",
json={
"id": "https://1b1a78582461.ngrok.io/tag/tag",
"type": "OrderedCollection",
"totalItems": 0,
"first": "https://1b1a78582461.ngrok.io/tag/tag?page=1",
"last": "https://1b1a78582461.ngrok.io/tag/tag?page=1",
"name": "cool tag",
"@context": "https://www.w3.org/ns/activitystreams",
},
)
activity = {
"id": "https://bookwyrm.social/listbook/6189#add",
"type": "Add",
"actor": "https://example.com/users/rat",
"object": {
"type": "Edition",
"title": "Test Title",
"work": work.remote_id,
"id": "https://bookwyrm.social/book/37292",
},
"target": "https://www.example.com/tag/cool-tag",
"@context": "https://www.w3.org/ns/activitystreams",
}
views.inbox.activity_task(activity)
tag = models.Tag.objects.get()
self.assertFalse(models.List.objects.exists())
self.assertEqual(tag.name, "cool tag")
self.assertEqual(tag.books.first(), book)