diff --git a/bookwyrm/activitypub/base_activity.py b/bookwyrm/activitypub/base_activity.py index ebc06045d..59e345d17 100644 --- a/bookwyrm/activitypub/base_activity.py +++ b/bookwyrm/activitypub/base_activity.py @@ -186,7 +186,7 @@ class ActivityObject: # add many to many fields, which have to be set post-save for field in instance.many_to_many_fields: - # mention books/users, for example + # mention books/users/hashtags, for example field.set_field_from_activity( instance, self, diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index f78f883f8..6cfe4c10c 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -389,13 +389,22 @@ class TagField(ManyToManyField): if tag_type != self.related_model.activity_serializer.type: # tags can contain multiple types continue - items.append( - activitypub.resolve_remote_id( - link.href, - model=self.related_model, - allow_external_connections=allow_external_connections, + + if tag_type == "Hashtag": + # we already have all data to create hashtags, + # no need to fetch from remote + item = self.related_model.activity_serializer(**link_json) + hashtag = item.to_model(model=self.related_model, save=True) + items.append(hashtag) + else: + # for other tag types we fetch them remotely + items.append( + activitypub.resolve_remote_id( + link.href, + model=self.related_model, + allow_external_connections=allow_external_connections, + ) ) - ) return items diff --git a/bookwyrm/tests/activitypub/test_base_activity.py b/bookwyrm/tests/activitypub/test_base_activity.py index 120cd2c91..df243d0db 100644 --- a/bookwyrm/tests/activitypub/test_base_activity.py +++ b/bookwyrm/tests/activitypub/test_base_activity.py @@ -183,12 +183,21 @@ class BaseActivity(TestCase): "name": "gerald j. books", "href": "http://book.com/book", }, + { + "type": "Hashtag", + "name": "#BookClub", + "href": "http://example.com/tags/BookClub", + }, ], ) update_data.to_model(model=models.Status, instance=status) self.assertEqual(status.mention_users.first(), self.user) self.assertEqual(status.mention_books.first(), book) + hashtag = models.Hashtag.objects.filter(name="#BookClub").first() + self.assertIsNotNone(hashtag) + self.assertEqual(status.mention_hashtags.first(), hashtag) + @responses.activate def test_to_model_one_to_many(self, *_): """these are reversed relationships, where the secondary object