From a6efd51bb294c21db8cc6dda64ae563e88b490e7 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 20 Dec 2020 11:38:13 -0800 Subject: [PATCH] Only parse mardown on outgoing activities --- bookwyrm/models/fields.py | 13 +------------ bookwyrm/outgoing.py | 29 +++++++++++++++++++++-------- bookwyrm/tests/test_incoming.py | 8 ++++---- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 87d220856..6dd3b496b 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -11,11 +11,10 @@ from django.core.files.base import ContentFile from django.db import models from django.utils import timezone from django.utils.translation import gettext_lazy as _ -from markdown import markdown from bookwyrm import activitypub +from bookwyrm.connectors import get_image from bookwyrm.sanitize_html import InputHtmlParser from bookwyrm.settings import DOMAIN -from bookwyrm.connectors import get_image def validate_remote_id(value): @@ -401,16 +400,6 @@ class HtmlField(ActivitypubFieldMixin, models.TextField): sanitizer.feed(value) return sanitizer.get_output() - def to_python(self, value):# pylint: disable=no-self-use - ''' process markdown before save ''' - if not value: - return value - content = markdown(value) - # sanitize resulting html - sanitizer = InputHtmlParser() - sanitizer.feed(content) - return sanitizer.get_output() - class ArrayField(ActivitypubFieldMixin, DjangoArrayField): ''' activitypub-aware array field ''' def field_to_activity(self, value): diff --git a/bookwyrm/outgoing.py b/bookwyrm/outgoing.py index 7ac2d0d67..7daa74de3 100644 --- a/bookwyrm/outgoing.py +++ b/bookwyrm/outgoing.py @@ -4,12 +4,14 @@ import re from django.db import IntegrityError, transaction from django.http import HttpResponseNotFound, JsonResponse from django.views.decorators.csrf import csrf_exempt +from markdown import markdown from requests import HTTPError from bookwyrm import activitypub from bookwyrm import models from bookwyrm.connectors import get_data, ConnectorException from bookwyrm.broadcast import broadcast +from bookwyrm.sanitize_html import InputHtmlParser from bookwyrm.status import create_notification from bookwyrm.status import create_generated_note from bookwyrm.status import delete_status @@ -241,20 +243,17 @@ def handle_status(user, form): related_user=user, related_status=status ) - # add links + # add mentions content = status.content - content = re.sub( - r'([^(href=")])(https?:\/\/([A-Za-z\.\-_\/]+' \ - r'\.[A-Za-z]{2,}[A-Za-z\.\-_\/]+))', - r'\g<1>\g<3>', - content) for (username, url) in matches: content = re.sub( r'%s([^@])' % username, r'%s\g<1>' % (url, username), content) - - status.content = content + if not isinstance(status, models.GeneratedNote): + status.content = to_markown(content) + if hasattr(status, 'quote'): + status.quote = to_markown(status.quote) status.save() # notify reply parent or tagged users @@ -273,6 +272,20 @@ def handle_status(user, form): broadcast(user, remote_activity, software='other') +def to_markown(content): + ''' catch links and convert to markdown ''' + content = re.sub( + r'([^(href=")])(https?:\/\/([A-Za-z\.\-_\/]+' \ + r'\.[A-Za-z]{2,}[A-Za-z\.\-_\/]+))', + r'\g<1>\g<3>', + content) + content = markdown(content) + # sanitize resulting html + sanitizer = InputHtmlParser() + sanitizer.feed(content) + return sanitizer.get_output() + + def handle_tag(user, tag): ''' tag a book ''' broadcast(user, tag.to_add_activity(user)) diff --git a/bookwyrm/tests/test_incoming.py b/bookwyrm/tests/test_incoming.py index e0c376484..2cc4c1f44 100644 --- a/bookwyrm/tests/test_incoming.py +++ b/bookwyrm/tests/test_incoming.py @@ -262,8 +262,8 @@ class Incoming(TestCase): status = models.Quotation.objects.get() self.assertEqual( status.remote_id, 'https://example.com/user/mouse/quotation/13') - self.assertEqual(status.quote, '

quote body

') - self.assertEqual(status.content, '

commentary

') + self.assertEqual(status.quote, 'quote body') + self.assertEqual(status.content, 'commentary') self.assertEqual(status.user, self.local_user) self.assertEqual(models.Status.objects.count(), 2) @@ -284,7 +284,7 @@ class Incoming(TestCase): incoming.handle_create(activity) status = models.Status.objects.last() - self.assertEqual(status.content, '

test content in note

') + self.assertEqual(status.content, 'test content in note') self.assertEqual(status.mention_users.first(), self.local_user) self.assertTrue( models.Notification.objects.filter(user=self.local_user).exists()) @@ -306,7 +306,7 @@ class Incoming(TestCase): incoming.handle_create(activity) status = models.Status.objects.last() - self.assertEqual(status.content, '

test content in note

') + self.assertEqual(status.content, 'test content in note') self.assertEqual(status.reply_parent, self.status) self.assertTrue( models.Notification.objects.filter(user=self.local_user))