From 220f2c1eb4b214887ce43df95c0407f6043f5ea8 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 15 Mar 2021 13:55:48 -0700 Subject: [PATCH 1/7] Fixes image attachments --- bookwyrm/activitypub/image.py | 4 +-- bookwyrm/models/activitypub_mixin.py | 6 +++- bookwyrm/models/attachment.py | 6 +++- bookwyrm/tests/views/test_feed.py | 45 ++++++++++++++++++++++++++-- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/bookwyrm/activitypub/image.py b/bookwyrm/activitypub/image.py index 248e7a4ad..931de977b 100644 --- a/bookwyrm/activitypub/image.py +++ b/bookwyrm/activitypub/image.py @@ -9,5 +9,5 @@ class Image(ActivityObject): url: str name: str = "" - type: str = "Image" - id: str = "" + type: str = "Document" + id: str = None diff --git a/bookwyrm/models/activitypub_mixin.py b/bookwyrm/models/activitypub_mixin.py index 0a3c33a16..23ac16424 100644 --- a/bookwyrm/models/activitypub_mixin.py +++ b/bookwyrm/models/activitypub_mixin.py @@ -445,7 +445,11 @@ def unfurl_related_field(related_field, sort_field=None): unfurl_related_field(i) for i in related_field.order_by(sort_field).all() ] if related_field.reverse_unfurl: - return related_field.field_to_activity() + # if it's a one-to-one (key pair) + if hasattr(related_field, "field_to_activity"): + return related_field.field_to_activity() + # if it's one-to-many (attachments) + return related_field.to_activity() return related_field.remote_id diff --git a/bookwyrm/models/attachment.py b/bookwyrm/models/attachment.py index 0cd2c111f..8d2238a14 100644 --- a/bookwyrm/models/attachment.py +++ b/bookwyrm/models/attachment.py @@ -25,7 +25,11 @@ class Image(Attachment): """ an image attachment """ image = fields.ImageField( - upload_to="status/", null=True, blank=True, activitypub_field="url" + upload_to="status/", + null=True, + blank=True, + activitypub_field="url", + alt_field="caption", ) caption = fields.TextField(null=True, blank=True, activitypub_field="name") diff --git a/bookwyrm/tests/views/test_feed.py b/bookwyrm/tests/views/test_feed.py index c54be0061..426684676 100644 --- a/bookwyrm/tests/views/test_feed.py +++ b/bookwyrm/tests/views/test_feed.py @@ -1,5 +1,10 @@ """ test for app action functionality """ +from io import BytesIO from unittest.mock import patch +import pathlib + +from PIL import Image +from django.core.files.base import ContentFile from django.template.response import TemplateResponse from django.test import TestCase from django.test.client import RequestFactory @@ -9,8 +14,8 @@ from bookwyrm import views from bookwyrm.activitypub import ActivitypubResponse -class FeedMessageViews(TestCase): - """ dms """ +class FeedViews(TestCase): + """ activity feed, statuses, dms """ def setUp(self): """ we need basic test data and mocks """ @@ -59,6 +64,42 @@ class FeedMessageViews(TestCase): self.assertIsInstance(result, ActivitypubResponse) self.assertEqual(result.status_code, 200) + def test_status_page_with_image(self): + """ there are so many views, this just makes sure it LOADS """ + view = views.Status.as_view() + + image_file = pathlib.Path(__file__).parent.joinpath( + "../../static/images/default_avi.jpg" + ) + image = Image.open(image_file) + output = BytesIO() + image.save(output, format=image.format) + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): + status = models.Review.objects.create( + content="hi", + user=self.local_user, + book=self.book, + ) + attachment = models.Image.objects.create( + status=status, caption="alt text here" + ) + attachment.image.save("test.jpg", ContentFile(output.getvalue())) + + request = self.factory.get("") + request.user = self.local_user + with patch("bookwyrm.views.feed.is_api_request") as is_api: + is_api.return_value = False + result = view(request, "mouse", status.id) + self.assertIsInstance(result, TemplateResponse) + result.render() + self.assertEqual(result.status_code, 200) + + with patch("bookwyrm.views.feed.is_api_request") as is_api: + is_api.return_value = True + result = view(request, "mouse", status.id) + self.assertIsInstance(result, ActivitypubResponse) + self.assertEqual(result.status_code, 200) + def test_replies_page(self): """ there are so many views, this just makes sure it LOADS """ view = views.Replies.as_view() From 20bcbb1a3ca8b1895a1fb351d130a37ef23c4254 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 15 Mar 2021 15:44:26 -0700 Subject: [PATCH 5/7] Updates image type to Document in status model tests --- bookwyrm/tests/models/test_status_model.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bookwyrm/tests/models/test_status_model.py b/bookwyrm/tests/models/test_status_model.py index b2ee69b87..1dcf56339 100644 --- a/bookwyrm/tests/models/test_status_model.py +++ b/bookwyrm/tests/models/test_status_model.py @@ -169,7 +169,7 @@ class Status(TestCase): self.assertEqual(activity["type"], "Note") self.assertEqual(activity["sensitive"], False) self.assertIsInstance(activity["attachment"], list) - self.assertEqual(activity["attachment"][0].type, "Image") + self.assertEqual(activity["attachment"][0].type, "Document") self.assertEqual( activity["attachment"][0].url, "https://%s%s" % (settings.DOMAIN, self.book.cover.url), @@ -200,7 +200,7 @@ class Status(TestCase): 'test content

(comment on "Test Edition")

' % self.book.remote_id, ) - self.assertEqual(activity["attachment"][0].type, "Image") + self.assertEqual(activity["attachment"][0].type, "Document") self.assertEqual( activity["attachment"][0].url, "https://%s%s" % (settings.DOMAIN, self.book.cover.url), @@ -238,7 +238,7 @@ class Status(TestCase): 'a sickening sense

-- "Test Edition"

' "test content" % self.book.remote_id, ) - self.assertEqual(activity["attachment"][0].type, "Image") + self.assertEqual(activity["attachment"][0].type, "Document") self.assertEqual( activity["attachment"][0].url, "https://%s%s" % (settings.DOMAIN, self.book.cover.url), @@ -278,7 +278,7 @@ class Status(TestCase): activity["name"], 'Review of "%s" (3 stars): Review name' % self.book.title ) self.assertEqual(activity["content"], "test content") - self.assertEqual(activity["attachment"][0].type, "Image") + self.assertEqual(activity["attachment"][0].type, "Document") self.assertEqual( activity["attachment"][0].url, "https://%s%s" % (settings.DOMAIN, self.book.cover.url), From 5a24c97cb5cc521b6883f6c41e765b191f01a574 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 15 Mar 2021 15:45:18 -0700 Subject: [PATCH 6/7] Updates image type in fields tests --- bookwyrm/tests/models/test_fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index 522d16f94..28faf52c3 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -404,7 +404,7 @@ class ActivitypubFields(TestCase): ) ) self.assertEqual(output.name, "alt text") - self.assertEqual(output.type, "Image") + self.assertEqual(output.type, "Document") instance = fields.ImageField() From bb8ce74f5205650e9b2b27d7fe0a1d49d877758e Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 15 Mar 2021 15:57:00 -0700 Subject: [PATCH 7/7] Updates base activity tests for updated Image type --- bookwyrm/tests/activitypub/test_base_activity.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bookwyrm/tests/activitypub/test_base_activity.py b/bookwyrm/tests/activitypub/test_base_activity.py index b3e282619..f3d3decde 100644 --- a/bookwyrm/tests/activitypub/test_base_activity.py +++ b/bookwyrm/tests/activitypub/test_base_activity.py @@ -123,7 +123,7 @@ class BaseActivity(TestCase): summary="", publicKey={"id": "hi", "owner": self.user.remote_id, "publicKeyPem": "hi"}, endpoints={}, - icon={"type": "Image", "url": "http://www.example.com/image.jpg"}, + icon={"type": "Document", "url": "http://www.example.com/image.jpg"}, ) responses.add( @@ -194,7 +194,7 @@ class BaseActivity(TestCase): { "url": "http://www.example.com/image.jpg", "name": "alt text", - "type": "Image", + "type": "Document", } ], ) @@ -224,7 +224,7 @@ class BaseActivity(TestCase): data = { "url": "http://www.example.com/image.jpg", "name": "alt text", - "type": "Image", + "type": "Document", } responses.add( responses.GET,