Merge pull request #2398 from bookwyrm-social/html-in-activitypub

Render markdown to html when using activitypub
This commit is contained in:
Mouse Reeve 2022-12-11 13:43:36 -08:00 committed by GitHub
commit bb5564461c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 13 deletions

View file

@ -13,6 +13,7 @@ from django.forms import ClearableFileInput, ImageField as DjangoImageField
from django.utils import timezone from django.utils import timezone
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.utils.encoding import filepath_to_uri from django.utils.encoding import filepath_to_uri
from markdown import markdown
from bookwyrm import activitypub from bookwyrm import activitypub
from bookwyrm.connectors import get_image from bookwyrm.connectors import get_image
@ -499,6 +500,9 @@ class HtmlField(ActivitypubFieldMixin, models.TextField):
return None return None
return clean(value) return clean(value)
def field_to_activity(self, value):
return markdown(value) if value else value
class ArrayField(ActivitypubFieldMixin, DjangoArrayField): class ArrayField(ActivitypubFieldMixin, DjangoArrayField):
"""activitypub-aware array field""" """activitypub-aware array field"""

View file

@ -383,16 +383,16 @@ class ActivitypubMixins(TestCase):
self.assertEqual(page_1.partOf, "http://fish.com/") self.assertEqual(page_1.partOf, "http://fish.com/")
self.assertEqual(page_1.id, "http://fish.com/?page=1") self.assertEqual(page_1.id, "http://fish.com/?page=1")
self.assertEqual(page_1.next, "http://fish.com/?page=2") self.assertEqual(page_1.next, "http://fish.com/?page=2")
self.assertEqual(page_1.orderedItems[0]["content"], "test status 29") self.assertEqual(page_1.orderedItems[0]["content"], "<p>test status 29</p>")
self.assertEqual(page_1.orderedItems[1]["content"], "test status 28") self.assertEqual(page_1.orderedItems[1]["content"], "<p>test status 28</p>")
page_2 = to_ordered_collection_page( page_2 = to_ordered_collection_page(
models.Status.objects.all(), "http://fish.com/", page=2 models.Status.objects.all(), "http://fish.com/", page=2
) )
self.assertEqual(page_2.partOf, "http://fish.com/") self.assertEqual(page_2.partOf, "http://fish.com/")
self.assertEqual(page_2.id, "http://fish.com/?page=2") self.assertEqual(page_2.id, "http://fish.com/?page=2")
self.assertEqual(page_2.orderedItems[0]["content"], "test status 14") self.assertEqual(page_2.orderedItems[0]["content"], "<p>test status 14</p>")
self.assertEqual(page_2.orderedItems[-1]["content"], "test status 0") self.assertEqual(page_2.orderedItems[-1]["content"], "<p>test status 0</p>")
def test_to_ordered_collection(self, *_): def test_to_ordered_collection(self, *_):
"""convert a queryset into an ordered collection object""" """convert a queryset into an ordered collection object"""
@ -420,8 +420,8 @@ class ActivitypubMixins(TestCase):
) )
self.assertEqual(page_2.partOf, "http://fish.com/") self.assertEqual(page_2.partOf, "http://fish.com/")
self.assertEqual(page_2.id, "http://fish.com/?page=2") self.assertEqual(page_2.id, "http://fish.com/?page=2")
self.assertEqual(page_2.orderedItems[0]["content"], "test status 14") self.assertEqual(page_2.orderedItems[0]["content"], "<p>test status 14</p>")
self.assertEqual(page_2.orderedItems[-1]["content"], "test status 0") self.assertEqual(page_2.orderedItems[-1]["content"], "<p>test status 0</p>")
def test_broadcast_task(self, *_): def test_broadcast_task(self, *_):
"""Should be calling asyncio""" """Should be calling asyncio"""

View file

@ -132,7 +132,7 @@ class Status(TestCase):
activity = status.to_activity() activity = status.to_activity()
self.assertEqual(activity["id"], status.remote_id) self.assertEqual(activity["id"], status.remote_id)
self.assertEqual(activity["type"], "Note") self.assertEqual(activity["type"], "Note")
self.assertEqual(activity["content"], "test content") self.assertEqual(activity["content"], "<p>test content</p>")
self.assertEqual(activity["sensitive"], False) self.assertEqual(activity["sensitive"], False)
def test_status_to_activity_tombstone(self, *_): def test_status_to_activity_tombstone(self, *_):
@ -156,7 +156,7 @@ class Status(TestCase):
activity = status.to_activity(pure=True) activity = status.to_activity(pure=True)
self.assertEqual(activity["id"], status.remote_id) self.assertEqual(activity["id"], status.remote_id)
self.assertEqual(activity["type"], "Note") self.assertEqual(activity["type"], "Note")
self.assertEqual(activity["content"], "test content") self.assertEqual(activity["content"], "<p>test content</p>")
self.assertEqual(activity["sensitive"], False) self.assertEqual(activity["sensitive"], False)
self.assertEqual(activity["attachment"], []) self.assertEqual(activity["attachment"], [])
@ -170,7 +170,7 @@ class Status(TestCase):
activity = status.to_activity() activity = status.to_activity()
self.assertEqual(activity["id"], status.remote_id) self.assertEqual(activity["id"], status.remote_id)
self.assertEqual(activity["type"], "GeneratedNote") self.assertEqual(activity["type"], "GeneratedNote")
self.assertEqual(activity["content"], "test content") self.assertEqual(activity["content"], "<p>test content</p>")
self.assertEqual(activity["sensitive"], False) self.assertEqual(activity["sensitive"], False)
self.assertEqual(len(activity["tag"]), 2) self.assertEqual(len(activity["tag"]), 2)
@ -208,7 +208,7 @@ class Status(TestCase):
activity = status.to_activity() activity = status.to_activity()
self.assertEqual(activity["id"], status.remote_id) self.assertEqual(activity["id"], status.remote_id)
self.assertEqual(activity["type"], "Comment") self.assertEqual(activity["type"], "Comment")
self.assertEqual(activity["content"], "test content") self.assertEqual(activity["content"], "<p>test content</p>")
self.assertEqual(activity["inReplyToBook"], self.book.remote_id) self.assertEqual(activity["inReplyToBook"], self.book.remote_id)
def test_comment_to_pure_activity(self, *_): def test_comment_to_pure_activity(self, *_):
@ -243,8 +243,8 @@ class Status(TestCase):
activity = status.to_activity() activity = status.to_activity()
self.assertEqual(activity["id"], status.remote_id) self.assertEqual(activity["id"], status.remote_id)
self.assertEqual(activity["type"], "Quotation") self.assertEqual(activity["type"], "Quotation")
self.assertEqual(activity["quote"], "a sickening sense") self.assertEqual(activity["quote"], "<p>a sickening sense</p>")
self.assertEqual(activity["content"], "test content") self.assertEqual(activity["content"], "<p>test content</p>")
self.assertEqual(activity["inReplyToBook"], self.book.remote_id) self.assertEqual(activity["inReplyToBook"], self.book.remote_id)
def test_quotation_to_pure_activity(self, *_): def test_quotation_to_pure_activity(self, *_):
@ -285,7 +285,7 @@ class Status(TestCase):
self.assertEqual(activity["type"], "Review") self.assertEqual(activity["type"], "Review")
self.assertEqual(activity["rating"], 3) self.assertEqual(activity["rating"], 3)
self.assertEqual(activity["name"], "Review name") self.assertEqual(activity["name"], "Review name")
self.assertEqual(activity["content"], "test content") self.assertEqual(activity["content"], "<p>test content</p>")
self.assertEqual(activity["inReplyToBook"], self.book.remote_id) self.assertEqual(activity["inReplyToBook"], self.book.remote_id)
def test_review_to_pure_activity(self, *_): def test_review_to_pure_activity(self, *_):