Merge pull request #744 from mouse-reeve/images

Fixes image attachments
This commit is contained in:
Mouse Reeve 2021-03-15 16:02:29 -07:00 committed by GitHub
commit 0550918e5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 14 deletions

View file

@ -9,5 +9,5 @@ class Image(ActivityObject):
url: str url: str
name: str = "" name: str = ""
type: str = "Image" type: str = "Document"
id: str = "" id: str = None

View file

@ -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() unfurl_related_field(i) for i in related_field.order_by(sort_field).all()
] ]
if related_field.reverse_unfurl: 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 return related_field.remote_id

View file

@ -25,7 +25,11 @@ class Image(Attachment):
""" an image attachment """ """ an image attachment """
image = fields.ImageField( 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") caption = fields.TextField(null=True, blank=True, activitypub_field="name")

View file

@ -123,7 +123,7 @@ class BaseActivity(TestCase):
summary="", summary="",
publicKey={"id": "hi", "owner": self.user.remote_id, "publicKeyPem": "hi"}, publicKey={"id": "hi", "owner": self.user.remote_id, "publicKeyPem": "hi"},
endpoints={}, endpoints={},
icon={"type": "Image", "url": "http://www.example.com/image.jpg"}, icon={"type": "Document", "url": "http://www.example.com/image.jpg"},
) )
responses.add( responses.add(
@ -194,7 +194,7 @@ class BaseActivity(TestCase):
{ {
"url": "http://www.example.com/image.jpg", "url": "http://www.example.com/image.jpg",
"name": "alt text", "name": "alt text",
"type": "Image", "type": "Document",
} }
], ],
) )
@ -224,7 +224,7 @@ class BaseActivity(TestCase):
data = { data = {
"url": "http://www.example.com/image.jpg", "url": "http://www.example.com/image.jpg",
"name": "alt text", "name": "alt text",
"type": "Image", "type": "Document",
} }
responses.add( responses.add(
responses.GET, responses.GET,

View file

@ -404,7 +404,7 @@ class ActivitypubFields(TestCase):
) )
) )
self.assertEqual(output.name, "alt text") self.assertEqual(output.name, "alt text")
self.assertEqual(output.type, "Image") self.assertEqual(output.type, "Document")
instance = fields.ImageField() instance = fields.ImageField()

View file

@ -169,7 +169,7 @@ class Status(TestCase):
self.assertEqual(activity["type"], "Note") self.assertEqual(activity["type"], "Note")
self.assertEqual(activity["sensitive"], False) self.assertEqual(activity["sensitive"], False)
self.assertIsInstance(activity["attachment"], list) self.assertIsInstance(activity["attachment"], list)
self.assertEqual(activity["attachment"][0].type, "Image") self.assertEqual(activity["attachment"][0].type, "Document")
self.assertEqual( self.assertEqual(
activity["attachment"][0].url, activity["attachment"][0].url,
"https://%s%s" % (settings.DOMAIN, self.book.cover.url), "https://%s%s" % (settings.DOMAIN, self.book.cover.url),
@ -200,7 +200,7 @@ class Status(TestCase):
'test content<p>(comment on <a href="%s">"Test Edition"</a>)</p>' 'test content<p>(comment on <a href="%s">"Test Edition"</a>)</p>'
% self.book.remote_id, % self.book.remote_id,
) )
self.assertEqual(activity["attachment"][0].type, "Image") self.assertEqual(activity["attachment"][0].type, "Document")
self.assertEqual( self.assertEqual(
activity["attachment"][0].url, activity["attachment"][0].url,
"https://%s%s" % (settings.DOMAIN, self.book.cover.url), "https://%s%s" % (settings.DOMAIN, self.book.cover.url),
@ -238,7 +238,7 @@ class Status(TestCase):
'a sickening sense <p>-- <a href="%s">"Test Edition"</a></p>' 'a sickening sense <p>-- <a href="%s">"Test Edition"</a></p>'
"test content" % self.book.remote_id, "test content" % self.book.remote_id,
) )
self.assertEqual(activity["attachment"][0].type, "Image") self.assertEqual(activity["attachment"][0].type, "Document")
self.assertEqual( self.assertEqual(
activity["attachment"][0].url, activity["attachment"][0].url,
"https://%s%s" % (settings.DOMAIN, self.book.cover.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 activity["name"], 'Review of "%s" (3 stars): Review name' % self.book.title
) )
self.assertEqual(activity["content"], "test content") self.assertEqual(activity["content"], "test content")
self.assertEqual(activity["attachment"][0].type, "Image") self.assertEqual(activity["attachment"][0].type, "Document")
self.assertEqual( self.assertEqual(
activity["attachment"][0].url, activity["attachment"][0].url,
"https://%s%s" % (settings.DOMAIN, self.book.cover.url), "https://%s%s" % (settings.DOMAIN, self.book.cover.url),

View file

@ -1,5 +1,10 @@
""" test for app action functionality """ """ test for app action functionality """
from io import BytesIO
from unittest.mock import patch 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.template.response import TemplateResponse
from django.test import TestCase from django.test import TestCase
from django.test.client import RequestFactory from django.test.client import RequestFactory
@ -9,8 +14,8 @@ from bookwyrm import views
from bookwyrm.activitypub import ActivitypubResponse from bookwyrm.activitypub import ActivitypubResponse
class FeedMessageViews(TestCase): class FeedViews(TestCase):
""" dms """ """ activity feed, statuses, dms """
def setUp(self): def setUp(self):
""" we need basic test data and mocks """ """ we need basic test data and mocks """
@ -59,6 +64,42 @@ class FeedMessageViews(TestCase):
self.assertIsInstance(result, ActivitypubResponse) self.assertIsInstance(result, ActivitypubResponse)
self.assertEqual(result.status_code, 200) 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): def test_replies_page(self):
""" there are so many views, this just makes sure it LOADS """ """ there are so many views, this just makes sure it LOADS """
view = views.Replies.as_view() view = views.Replies.as_view()