From 6323b0e7005218c35a88f28d3067af8227347f61 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 9 Feb 2022 12:20:11 -0500 Subject: [PATCH 1/2] Use correct model type for federated links --- bookwyrm/activitypub/base_activity.py | 4 ++-- bookwyrm/tests/views/books/test_links.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/bookwyrm/activitypub/base_activity.py b/bookwyrm/activitypub/base_activity.py index 15ca5a938..7d2796eff 100644 --- a/bookwyrm/activitypub/base_activity.py +++ b/bookwyrm/activitypub/base_activity.py @@ -227,7 +227,7 @@ def set_related_field( model_field = getattr(model, related_field_name) if hasattr(model_field, "activitypub_field"): setattr(activity, getattr(model_field, "activitypub_field"), instance.remote_id) - item = activity.to_model() + item = activity.to_model(model=model) # if the related field isn't serialized (attachments on Status), then # we have to set it post-creation @@ -298,6 +298,7 @@ class Link(ActivityObject): mediaType: str = None id: str = None attributedTo: str = None + availability: str = None type: str = "Link" def serialize(self, **kwargs): @@ -305,7 +306,6 @@ class Link(ActivityObject): omit = ("id", "type", "@context") return super().serialize(omit=omit) - @dataclass(init=False) class Mention(Link): """a subtype of Link for mentioning an actor""" diff --git a/bookwyrm/tests/views/books/test_links.py b/bookwyrm/tests/views/books/test_links.py index 2aee5aed9..d98e95dae 100644 --- a/bookwyrm/tests/views/books/test_links.py +++ b/bookwyrm/tests/views/books/test_links.py @@ -80,6 +80,17 @@ class LinkViews(TestCase): activity = json.loads(mock.call_args[1]["args"][1]) self.assertEqual(activity["type"], "Update") self.assertEqual(activity["object"]["type"], "Edition") + self.assertIsInstance(activity["object"]["fileLinks"], list) + self.assertEqual( + activity["object"]["fileLinks"][0]["href"], "https://www.example.com" + ) + self.assertEqual( + activity["object"]["fileLinks"][0]["mediaType"], "HTML" + ) + self.assertEqual( + activity["object"]["fileLinks"][0]["attributedTo"], + self.local_user.remote_id + ) link = models.FileLink.objects.get() self.assertEqual(link.name, "www.example.com") From c06817e9ff8212c7bf5de413a4b4470aeed842f4 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 9 Feb 2022 12:23:01 -0500 Subject: [PATCH 2/2] Python formatting --- bookwyrm/activitypub/base_activity.py | 1 + bookwyrm/tests/models/test_book_model.py | 1 - bookwyrm/tests/views/books/test_links.py | 6 ++---- bookwyrm/tests/views/inbox/test_inbox_update.py | 1 - 4 files changed, 3 insertions(+), 6 deletions(-) diff --git a/bookwyrm/activitypub/base_activity.py b/bookwyrm/activitypub/base_activity.py index 7d2796eff..6bee25f62 100644 --- a/bookwyrm/activitypub/base_activity.py +++ b/bookwyrm/activitypub/base_activity.py @@ -306,6 +306,7 @@ class Link(ActivityObject): omit = ("id", "type", "@context") return super().serialize(omit=omit) + @dataclass(init=False) class Mention(Link): """a subtype of Link for mentioning an actor""" diff --git a/bookwyrm/tests/models/test_book_model.py b/bookwyrm/tests/models/test_book_model.py index d74fa4ca3..d4f59fa58 100644 --- a/bookwyrm/tests/models/test_book_model.py +++ b/bookwyrm/tests/models/test_book_model.py @@ -1,7 +1,6 @@ """ testing models """ from dateutil.parser import parse -from imagekit.models import ImageSpecField from django.test import TestCase from django.utils import timezone diff --git a/bookwyrm/tests/views/books/test_links.py b/bookwyrm/tests/views/books/test_links.py index d98e95dae..3379786b0 100644 --- a/bookwyrm/tests/views/books/test_links.py +++ b/bookwyrm/tests/views/books/test_links.py @@ -84,12 +84,10 @@ class LinkViews(TestCase): self.assertEqual( activity["object"]["fileLinks"][0]["href"], "https://www.example.com" ) - self.assertEqual( - activity["object"]["fileLinks"][0]["mediaType"], "HTML" - ) + self.assertEqual(activity["object"]["fileLinks"][0]["mediaType"], "HTML") self.assertEqual( activity["object"]["fileLinks"][0]["attributedTo"], - self.local_user.remote_id + self.local_user.remote_id, ) link = models.FileLink.objects.get() diff --git a/bookwyrm/tests/views/inbox/test_inbox_update.py b/bookwyrm/tests/views/inbox/test_inbox_update.py index 963742c83..18b2e5d59 100644 --- a/bookwyrm/tests/views/inbox/test_inbox_update.py +++ b/bookwyrm/tests/views/inbox/test_inbox_update.py @@ -6,7 +6,6 @@ from unittest.mock import patch from django.test import TestCase from bookwyrm import models, views -from bookwyrm.activitypub.base_activity import set_related_field # pylint: disable=too-many-public-methods