Adds privacy and recipient details to list items
This commit is contained in:
parent
1f930ba821
commit
b1491c2ac6
|
@ -359,6 +359,20 @@ class CollectionItemMixin(ActivitypubMixin):
|
|||
|
||||
activity_serializer = activitypub.CollectionItem
|
||||
|
||||
@property
|
||||
def privacy(self):
|
||||
""" inherit the privacy of the list, or direct if pending """
|
||||
collection_field = getattr(self, self.collection_field)
|
||||
if self.approved:
|
||||
return collection_field.privacy
|
||||
return "direct"
|
||||
|
||||
@property
|
||||
def recipients(self):
|
||||
""" the owner of the list is a direct recipient """
|
||||
collection_field = getattr(self, self.collection_field)
|
||||
return [collection_field.user]
|
||||
|
||||
def save(self, *args, broadcast=True, **kwargs):
|
||||
""" broadcast updated """
|
||||
# first off, we want to save normally no matter what
|
||||
|
|
|
@ -61,9 +61,7 @@ class ListItem(CollectionItemMixin, BookWyrmModel):
|
|||
book = fields.ForeignKey(
|
||||
"Edition", on_delete=models.PROTECT, activitypub_field="book"
|
||||
)
|
||||
book_list = models.ForeignKey(
|
||||
"List", on_delete=models.CASCADE
|
||||
)
|
||||
book_list = models.ForeignKey("List", on_delete=models.CASCADE)
|
||||
user = fields.ForeignKey(
|
||||
"User", on_delete=models.PROTECT, activitypub_field="actor"
|
||||
)
|
||||
|
|
|
@ -68,9 +68,7 @@ class ShelfBook(CollectionItemMixin, BookWyrmModel):
|
|||
book = fields.ForeignKey(
|
||||
"Edition", on_delete=models.PROTECT, activitypub_field="book"
|
||||
)
|
||||
shelf = models.ForeignKey(
|
||||
"Shelf", on_delete=models.PROTECT
|
||||
)
|
||||
shelf = models.ForeignKey("Shelf", on_delete=models.PROTECT)
|
||||
user = fields.ForeignKey(
|
||||
"User", on_delete=models.PROTECT, activitypub_field="actor"
|
||||
)
|
||||
|
|
|
@ -11,45 +11,67 @@ class List(TestCase):
|
|||
|
||||
def setUp(self):
|
||||
""" look, a list """
|
||||
self.user = models.User.objects.create_user(
|
||||
self.local_user = models.User.objects.create_user(
|
||||
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
|
||||
)
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
self.list = models.List.objects.create(name="Test List", user=self.user)
|
||||
work = models.Work.objects.create(title="hello")
|
||||
self.book = models.Edition.objects.create(title="hi", parent_work=work)
|
||||
|
||||
def test_remote_id(self, _):
|
||||
""" shelves use custom remote ids """
|
||||
expected_id = "https://%s/list/%d" % (settings.DOMAIN, self.list.id)
|
||||
self.assertEqual(self.list.get_remote_id(), expected_id)
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
book_list = models.List.objects.create(
|
||||
name="Test List", user=self.local_user
|
||||
)
|
||||
expected_id = "https://%s/list/%d" % (settings.DOMAIN, book_list.id)
|
||||
self.assertEqual(book_list.get_remote_id(), expected_id)
|
||||
|
||||
def test_to_activity(self, _):
|
||||
""" jsonify it """
|
||||
activity_json = self.list.to_activity()
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
book_list = models.List.objects.create(
|
||||
name="Test List", user=self.local_user
|
||||
)
|
||||
activity_json = book_list.to_activity()
|
||||
self.assertIsInstance(activity_json, dict)
|
||||
self.assertEqual(activity_json["id"], self.list.remote_id)
|
||||
self.assertEqual(activity_json["id"], book_list.remote_id)
|
||||
self.assertEqual(activity_json["totalItems"], 0)
|
||||
self.assertEqual(activity_json["type"], "BookList")
|
||||
self.assertEqual(activity_json["name"], "Test List")
|
||||
self.assertEqual(activity_json["owner"], self.user.remote_id)
|
||||
self.assertEqual(activity_json["owner"], self.local_user.remote_id)
|
||||
|
||||
def test_list_item(self, _):
|
||||
""" a list entry """
|
||||
work = models.Work.objects.create(title="hello")
|
||||
book = models.Edition.objects.create(title="hi", parent_work=work)
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
book_list = models.List.objects.create(
|
||||
name="Test List", user=self.local_user, privacy="unlisted"
|
||||
)
|
||||
|
||||
item = models.ListItem.objects.create(
|
||||
book_list=self.list,
|
||||
book=book,
|
||||
user=self.user,
|
||||
book_list=book_list,
|
||||
book=self.book,
|
||||
user=self.local_user,
|
||||
)
|
||||
|
||||
self.assertTrue(item.approved)
|
||||
self.assertEqual(item.privacy, "unlisted")
|
||||
self.assertEqual(item.recipients, [self.local_user])
|
||||
|
||||
add_activity = item.to_add_activity()
|
||||
self.assertEqual(add_activity["actor"], self.user.remote_id)
|
||||
self.assertEqual(add_activity["object"]["id"], book.remote_id)
|
||||
self.assertEqual(add_activity["target"], self.list.remote_id)
|
||||
def test_list_item_pending(self, _):
|
||||
""" a list entry """
|
||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
||||
book_list = models.List.objects.create(
|
||||
name="Test List", user=self.local_user
|
||||
)
|
||||
|
||||
remove_activity = item.to_remove_activity()
|
||||
self.assertEqual(remove_activity["actor"], self.user.remote_id)
|
||||
self.assertEqual(remove_activity["object"]["id"], book.remote_id)
|
||||
self.assertEqual(remove_activity["target"], self.list.remote_id)
|
||||
item = models.ListItem.objects.create(
|
||||
book_list=book_list,
|
||||
book=self.book,
|
||||
user=self.local_user,
|
||||
approved=False
|
||||
)
|
||||
|
||||
self.assertFalse(item.approved)
|
||||
self.assertEqual(item.book_list.privacy, "public")
|
||||
self.assertEqual(item.privacy, "direct")
|
||||
self.assertEqual(item.recipients, [self.local_user])
|
||||
|
|
Loading…
Reference in New Issue