Update ordered collection classes to use CollectionItem

This commit is contained in:
Mouse Reeve 2021-04-08 14:17:29 -07:00
parent 98161b9041
commit 31077a9790
3 changed files with 13 additions and 17 deletions

View file

@ -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()

View file

@ -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):

View file

@ -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: