From 31077a97900691b1104767e41c427d662ecedf7d Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 8 Apr 2021 14:17:29 -0700 Subject: [PATCH] Update ordered collection classes to use CollectionItem --- bookwyrm/models/activitypub_mixin.py | 22 ++++++++++------------ bookwyrm/models/list.py | 3 +-- bookwyrm/models/shelf.py | 5 ++--- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/bookwyrm/models/activitypub_mixin.py b/bookwyrm/models/activitypub_mixin.py index 8dce42e4..9b104456 100644 --- a/bookwyrm/models/activitypub_mixin.py +++ b/bookwyrm/models/activitypub_mixin.py @@ -64,6 +64,7 @@ class ActivitypubMixin: ) if hasattr(self, "property_fields"): self.activity_fields += [ + # pylint: disable=cell-var-from-loop PropertyField(lambda a, o: set_activity_from_property_field(a, o, f)) for f in self.property_fields ] @@ -356,8 +357,7 @@ class OrderedCollectionMixin(OrderedCollectionPageMixin): class CollectionItemMixin(ActivitypubMixin): """ for items that are part of an (Ordered)Collection """ - activity_serializer = activitypub.Add - object_field = collection_field = None + activity_serializer = activitypub.CollectionItem def save(self, *args, broadcast=True, **kwargs): """ broadcast updated """ @@ -370,35 +370,33 @@ class CollectionItemMixin(ActivitypubMixin): return # adding an obj to the collection - activity = self.to_add_activity() + activity = self.to_add_activity(self.user) self.broadcast(activity, self.user) def delete(self, *args, **kwargs): """ broadcast a remove activity """ - activity = self.to_remove_activity() + activity = self.to_remove_activity(self.user) super().delete(*args, **kwargs) if self.user.local: self.broadcast(activity, self.user) - def to_add_activity(self): + def to_add_activity(self, user): """ AP for shelving a book""" - object_field = getattr(self, self.object_field) collection_field = getattr(self, self.collection_field) return activitypub.Add( id=self.get_remote_id(), - actor=self.user.remote_id, - object=object_field, + actor=user.remote_id, + object=self.to_activity_dataclass(), target=collection_field.remote_id, ).serialize() - def to_remove_activity(self): + def to_remove_activity(self, user): """ AP for un-shelving a book""" - object_field = getattr(self, self.object_field) collection_field = getattr(self, self.collection_field) return activitypub.Remove( id=self.get_remote_id(), - actor=self.user.remote_id, - object=object_field, + actor=user.remote_id, + object=self.to_activity_dataclass(), target=collection_field.remote_id, ).serialize() diff --git a/bookwyrm/models/list.py b/bookwyrm/models/list.py index 880c4122..b90eafe3 100644 --- a/bookwyrm/models/list.py +++ b/bookwyrm/models/list.py @@ -72,8 +72,7 @@ class ListItem(CollectionItemMixin, BookWyrmModel): order = fields.IntegerField(blank=True, null=True) endorsement = models.ManyToManyField("User", related_name="endorsers") - activity_serializer = activitypub.Add - object_field = "book" + activity_serializer = activitypub.ListItem collection_field = "book_list" def save(self, *args, **kwargs): diff --git a/bookwyrm/models/shelf.py b/bookwyrm/models/shelf.py index 3209da5d..8a7095d0 100644 --- a/bookwyrm/models/shelf.py +++ b/bookwyrm/models/shelf.py @@ -32,6 +32,7 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel): ) activity_serializer = activitypub.Shelf + collection_field = "shelf" def save(self, *args, **kwargs): """ set the identifier """ @@ -75,9 +76,7 @@ class ShelfBook(CollectionItemMixin, BookWyrmModel): "User", on_delete=models.PROTECT, activitypub_field="actor" ) - activity_serializer = activitypub.Add - object_field = "book" - collection_field = "shelf" + activity_serializer = activitypub.ShelfItem def save(self, *args, **kwargs): if not self.user: