diff --git a/bookwyrm/activitypub/__init__.py b/bookwyrm/activitypub/__init__.py index 201e8042d..510f1f3f4 100644 --- a/bookwyrm/activitypub/__init__.py +++ b/bookwyrm/activitypub/__init__.py @@ -16,7 +16,7 @@ from .response import ActivitypubResponse from .book import Edition, Work, Author from .verbs import Create, Delete, Undo, Update from .verbs import Follow, Accept, Reject, Block -from .verbs import Add, AddBook, Remove +from .verbs import Add, AddBook, AddListItem, Remove # this creates a list of all the Activity types that we can serialize, # so when an Activity comes in from outside, we can check if it's known diff --git a/bookwyrm/activitypub/base_activity.py b/bookwyrm/activitypub/base_activity.py index 4bbb5e9f3..5f35f1d7e 100644 --- a/bookwyrm/activitypub/base_activity.py +++ b/bookwyrm/activitypub/base_activity.py @@ -65,6 +65,13 @@ class ActivityObject: def to_model(self, model, instance=None, save=True): ''' convert from an activity to a model instance ''' + if self.type != model.activity_serializer.type: + raise ActivitySerializerError( + 'Wrong activity type "%s" for activity of type "%s"' % \ + (model.activity_serializer.type, + self.type) + ) + if not isinstance(self, model.activity_serializer): raise ActivitySerializerError( 'Wrong activity type "%s" for model "%s" (expects "%s")' % \ diff --git a/bookwyrm/activitypub/verbs.py b/bookwyrm/activitypub/verbs.py index 5502ced01..190cd7395 100644 --- a/bookwyrm/activitypub/verbs.py +++ b/bookwyrm/activitypub/verbs.py @@ -70,17 +70,26 @@ class Reject(Verb): @dataclass(init=False) class Add(Verb): '''Add activity ''' - target: ActivityObject + target: str + object: ActivityObject type: str = 'Add' @dataclass(init=False) -class AddBook(Verb): +class AddBook(Add): '''Add activity that's aware of the book obj ''' - target: Edition + object: Edition type: str = 'Add' +@dataclass(init=False) +class AddListItem(AddBook): + '''Add activity that's aware of the book obj ''' + notes: str = None + order: int = 0 + approved: bool = True + + @dataclass(init=False) class Remove(Verb): '''Remove activity ''' diff --git a/bookwyrm/incoming.py b/bookwyrm/incoming.py index a88a748e4..202bf806b 100644 --- a/bookwyrm/incoming.py +++ b/bookwyrm/incoming.py @@ -319,8 +319,19 @@ def handle_add(activity): #this is janky as heck but I haven't thought of a better solution try: activitypub.AddBook(**activity).to_model(models.ShelfBook) + return except activitypub.ActivitySerializerError: - activitypub.AddBook(**activity).to_model(models.Tag) + pass + try: + activitypub.AddListItem(**activity).to_model(models.ListItem) + return + except activitypub.ActivitySerializerError: + pass + try: + activitypub.AddBook(**activity).to_model(models.UserTag) + return + except activitypub.ActivitySerializerError: + pass @app.task diff --git a/bookwyrm/models/list.py b/bookwyrm/models/list.py index db8c1af6e..ef48ed956 100644 --- a/bookwyrm/models/list.py +++ b/bookwyrm/models/list.py @@ -68,7 +68,7 @@ class ListItem(CollectionItemMixin, BookWyrmModel): order = fields.IntegerField(blank=True, null=True) endorsement = models.ManyToManyField('User', related_name='endorsers') - activity_serializer = activitypub.AddBook + activity_serializer = activitypub.AddListItem object_field = 'book' collection_field = 'book_list'