Merge branch 'main' into domain-block
This commit is contained in:
@ -387,49 +387,59 @@ 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
|
||||
|
||||
@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 """
|
||||
created = not bool(self.id)
|
||||
# first off, we want to save normally no matter what
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
# these shouldn't be edited, only created and deleted
|
||||
if not broadcast or not created or not self.user.local:
|
||||
# list items can be updateda, normally you would only broadcast on created
|
||||
if not broadcast or not self.user.local:
|
||||
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):
|
||||
def delete(self, *args, broadcast=True, **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:
|
||||
if self.user.local and broadcast:
|
||||
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,
|
||||
id="{:s}#add".format(collection_field.remote_id),
|
||||
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,
|
||||
id="{:s}#remove".format(collection_field.remote_id),
|
||||
actor=user.remote_id,
|
||||
object=self.to_activity_dataclass(),
|
||||
target=collection_field.remote_id,
|
||||
).serialize()
|
||||
|
||||
|
@ -59,11 +59,9 @@ class ListItem(CollectionItemMixin, BookWyrmModel):
|
||||
""" ok """
|
||||
|
||||
book = fields.ForeignKey(
|
||||
"Edition", on_delete=models.PROTECT, activitypub_field="object"
|
||||
)
|
||||
book_list = fields.ForeignKey(
|
||||
"List", on_delete=models.CASCADE, activitypub_field="target"
|
||||
"Edition", on_delete=models.PROTECT, activitypub_field="book"
|
||||
)
|
||||
book_list = models.ForeignKey("List", on_delete=models.CASCADE)
|
||||
user = fields.ForeignKey(
|
||||
"User", on_delete=models.PROTECT, activitypub_field="actor"
|
||||
)
|
||||
@ -72,8 +70,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):
|
||||
|
@ -66,17 +66,14 @@ class ShelfBook(CollectionItemMixin, BookWyrmModel):
|
||||
""" many to many join table for books and shelves """
|
||||
|
||||
book = fields.ForeignKey(
|
||||
"Edition", on_delete=models.PROTECT, activitypub_field="object"
|
||||
)
|
||||
shelf = fields.ForeignKey(
|
||||
"Shelf", on_delete=models.PROTECT, activitypub_field="target"
|
||||
"Edition", on_delete=models.PROTECT, activitypub_field="book"
|
||||
)
|
||||
shelf = models.ForeignKey("Shelf", on_delete=models.PROTECT)
|
||||
user = fields.ForeignKey(
|
||||
"User", on_delete=models.PROTECT, activitypub_field="actor"
|
||||
)
|
||||
|
||||
activity_serializer = activitypub.Add
|
||||
object_field = "book"
|
||||
activity_serializer = activitypub.ShelfItem
|
||||
collection_field = "shelf"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
|
Reference in New Issue
Block a user