mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-20 14:08:09 +00:00
Resolve and assign target collection for collection items
This commit is contained in:
parent
a7375c4c13
commit
ddd05a68cf
2 changed files with 22 additions and 66 deletions
|
@ -145,6 +145,13 @@ class Add(Verb):
|
||||||
object: CollectionItem
|
object: CollectionItem
|
||||||
type: str = "Add"
|
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)
|
@dataclass(init=False)
|
||||||
class Remove(Add):
|
class Remove(Add):
|
||||||
|
@ -154,7 +161,7 @@ class Remove(Add):
|
||||||
|
|
||||||
def action(self):
|
def action(self):
|
||||||
""" find and remove the activity object """
|
""" 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()
|
obj.delete()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ from bookwyrm import models, views
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-many-public-methods
|
# pylint: disable=too-many-public-methods
|
||||||
class InboxActivities(TestCase):
|
class InboxAdd(TestCase):
|
||||||
""" inbox tests """
|
""" inbox tests """
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -32,17 +32,17 @@ class InboxActivities(TestCase):
|
||||||
inbox="https://example.com/users/rat/inbox",
|
inbox="https://example.com/users/rat/inbox",
|
||||||
outbox="https://example.com/users/rat/outbox",
|
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()
|
models.SiteSettings.objects.create()
|
||||||
|
|
||||||
def test_handle_add_book_to_shelf(self):
|
def test_handle_add_book_to_shelf(self):
|
||||||
""" shelving a book """
|
""" 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 = models.Shelf.objects.create(user=self.remote_user, name="Test Shelf")
|
||||||
shelf.remote_id = "https://bookwyrm.social/user/mouse/shelf/to-read"
|
shelf.remote_id = "https://bookwyrm.social/user/mouse/shelf/to-read"
|
||||||
shelf.save()
|
shelf.save()
|
||||||
|
@ -52,27 +52,19 @@ class InboxActivities(TestCase):
|
||||||
"type": "Add",
|
"type": "Add",
|
||||||
"actor": "https://example.com/users/rat",
|
"actor": "https://example.com/users/rat",
|
||||||
"object": {
|
"object": {
|
||||||
"type": "Edition",
|
"type": "ShelfItem",
|
||||||
"title": "Test Title",
|
"book": self.book.remote_id,
|
||||||
"work": work.remote_id,
|
"id": "https://bookwyrm.social/listbook/6189",
|
||||||
"id": "https://bookwyrm.social/book/37292",
|
|
||||||
},
|
},
|
||||||
"target": "https://bookwyrm.social/user/mouse/shelf/to-read",
|
"target": "https://bookwyrm.social/user/mouse/shelf/to-read",
|
||||||
"@context": "https://www.w3.org/ns/activitystreams",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
}
|
}
|
||||||
views.inbox.activity_task(activity)
|
views.inbox.activity_task(activity)
|
||||||
self.assertEqual(shelf.books.first(), book)
|
self.assertEqual(shelf.books.first(), self.book)
|
||||||
|
|
||||||
@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 """
|
||||||
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.add(
|
||||||
responses.GET,
|
responses.GET,
|
||||||
"https://bookwyrm.social/user/mouse/list/to-read",
|
"https://bookwyrm.social/user/mouse/list/to-read",
|
||||||
|
@ -97,8 +89,9 @@ class InboxActivities(TestCase):
|
||||||
"type": "Add",
|
"type": "Add",
|
||||||
"actor": "https://example.com/users/rat",
|
"actor": "https://example.com/users/rat",
|
||||||
"object": {
|
"object": {
|
||||||
|
"actor": self.local_user.remote_id,
|
||||||
"type": "ListItem",
|
"type": "ListItem",
|
||||||
"book": self.edition.remote_id,
|
"book": self.book.remote_id,
|
||||||
"id": "https://bookwyrm.social/listbook/6189",
|
"id": "https://bookwyrm.social/listbook/6189",
|
||||||
},
|
},
|
||||||
"target": "https://bookwyrm.social/user/mouse/list/to-read",
|
"target": "https://bookwyrm.social/user/mouse/list/to-read",
|
||||||
|
@ -109,49 +102,5 @@ class InboxActivities(TestCase):
|
||||||
booklist = models.List.objects.get()
|
booklist = models.List.objects.get()
|
||||||
listitem = models.ListItem.objects.get()
|
listitem = models.ListItem.objects.get()
|
||||||
self.assertEqual(booklist.name, "Test List")
|
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")
|
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)
|
|
||||||
|
|
Loading…
Reference in a new issue