diff --git a/federation/entities/mixins.py b/federation/entities/mixins.py index 506becd..d37fd93 100644 --- a/federation/entities/mixins.py +++ b/federation/entities/mixins.py @@ -10,7 +10,7 @@ from marshmallow import missing from federation.entities.activitypub.enums import ActivityType from federation.entities.utils import get_name_for_profile, get_profile -from federation.utils.text import process_text_links, find_elements, find_tags, MENTION_PATTERN +from federation.utils.text import find_elements, find_tags, MENTION_PATTERN class BaseEntity: diff --git a/federation/tests/entities/activitypub/test_entities.py b/federation/tests/entities/activitypub/test_entities.py index 8a7ba6b..10335d9 100644 --- a/federation/tests/entities/activitypub/test_entities.py +++ b/federation/tests/entities/activitypub/test_entities.py @@ -10,7 +10,6 @@ from federation.entities.activitypub.models import context_manager from federation.entities.activitypub.models import Accept from federation.tests.fixtures.keys import PUBKEY from federation.types import UserType -from federation.utils.text import process_text_links class TestEntitiesConvertToAS2: @@ -65,6 +64,8 @@ class TestEntitiesConvertToAS2: 'published': '2019-04-27T00:00:00', } + # Now handled by the client app + @pytest.mark.skip def test_comment_to_as2__url_in_raw_content(self, activitypubcomment): activitypubcomment.raw_content = 'raw_content http://example.com' activitypubcomment.rendered_content = process_text_links( diff --git a/federation/tests/utils/test_text.py b/federation/tests/utils/test_text.py index 2c73bc0..a442e93 100644 --- a/federation/tests/utils/test_text.py +++ b/federation/tests/utils/test_text.py @@ -1,4 +1,6 @@ -from federation.utils.text import decode_if_bytes, encode_if_text, validate_handle, process_text_links, find_tags +import pytest + +from federation.utils.text import decode_if_bytes, encode_if_text, validate_handle, find_tags def test_decode_if_bytes(): @@ -63,6 +65,8 @@ class TestFindTags: assert tags == {"foobar", "barfoo"} +# TODO: move these tests to the client app +@pytest.mark.skip class TestProcessTextLinks: def test_link_at_start_or_end(self): assert process_text_links('https://example.org example.org\nhttp://example.org') == \ diff --git a/federation/utils/text.py b/federation/utils/text.py index cbe6086..8ce6478 100644 --- a/federation/utils/text.py +++ b/federation/utils/text.py @@ -2,8 +2,6 @@ import re from typing import Set, List from urllib.parse import urlparse -import bleach -from bleach import callbacks from bs4 import BeautifulSoup from bs4.element import NavigableString from commonmark import commonmark @@ -12,6 +10,7 @@ ILLEGAL_TAG_CHARS = "!#$%^&*+.,@£/()=?`'\\{[]}~;:\"’”—\xa0" TAG_PATTERN = re.compile(r'(#[\w]+)', re.UNICODE) # This will match non matching braces. I don't think it's an issue. MENTION_PATTERN = re.compile(r'(@\{?(?:[\w\-. \u263a-\U0001f645]*; *)?[\w]+@[\w\-.]+\.[\w]+}?)', re.UNICODE) +URL_PATTERN = re.compile(r'(https?://[\w_\-.#?&/]+)', re.UNICODE) def decode_if_bytes(text): try: @@ -65,28 +64,6 @@ def get_path_from_url(url: str) -> str: return parsed.path -def process_text_links(text): - """Process links in text, adding some attributes and linkifying textual links.""" - link_callbacks = [callbacks.nofollow, callbacks.target_blank] - - def link_attributes(attrs, new=False): - """Run standard callbacks except for internal links.""" - href_key = (None, "href") - if attrs.get(href_key, "").startswith("/"): - return attrs - - # Run the standard callbacks - for callback in link_callbacks: - attrs = callback(attrs, new) - return attrs - - return bleach.linkify( - text, - callbacks=[link_attributes], - parse_email=False, - skip_tags=["code"], - ) - def test_tag(tag: str) -> bool: """Test a word whether it could be accepted as a tag."""