Move process_text_links back to the client app. Skip related tests. Convert it to BeautifulSoup. Remove unused imports.

ap-processing-improvements
Alain St-Denis 2023-07-16 07:13:56 -04:00
rodzic 0783bf43aa
commit 33366802c4
4 zmienionych plików z 9 dodań i 27 usunięć

Wyświetl plik

@ -10,7 +10,7 @@ from marshmallow import missing
from federation.entities.activitypub.enums import ActivityType from federation.entities.activitypub.enums import ActivityType
from federation.entities.utils import get_name_for_profile, get_profile 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: class BaseEntity:

Wyświetl plik

@ -10,7 +10,6 @@ from federation.entities.activitypub.models import context_manager
from federation.entities.activitypub.models import Accept from federation.entities.activitypub.models import Accept
from federation.tests.fixtures.keys import PUBKEY from federation.tests.fixtures.keys import PUBKEY
from federation.types import UserType from federation.types import UserType
from federation.utils.text import process_text_links
class TestEntitiesConvertToAS2: class TestEntitiesConvertToAS2:
@ -65,6 +64,8 @@ class TestEntitiesConvertToAS2:
'published': '2019-04-27T00:00:00', '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): def test_comment_to_as2__url_in_raw_content(self, activitypubcomment):
activitypubcomment.raw_content = 'raw_content http://example.com' activitypubcomment.raw_content = 'raw_content http://example.com'
activitypubcomment.rendered_content = process_text_links( activitypubcomment.rendered_content = process_text_links(

Wyświetl plik

@ -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(): def test_decode_if_bytes():
@ -63,6 +65,8 @@ class TestFindTags:
assert tags == {"foobar", "barfoo"} assert tags == {"foobar", "barfoo"}
# TODO: move these tests to the client app
@pytest.mark.skip
class TestProcessTextLinks: class TestProcessTextLinks:
def test_link_at_start_or_end(self): def test_link_at_start_or_end(self):
assert process_text_links('https://example.org example.org\nhttp://example.org') == \ assert process_text_links('https://example.org example.org\nhttp://example.org') == \

Wyświetl plik

@ -2,8 +2,6 @@ import re
from typing import Set, List from typing import Set, List
from urllib.parse import urlparse from urllib.parse import urlparse
import bleach
from bleach import callbacks
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from bs4.element import NavigableString from bs4.element import NavigableString
from commonmark import commonmark from commonmark import commonmark
@ -12,6 +10,7 @@ ILLEGAL_TAG_CHARS = "!#$%^&*+.,@£/()=?`'\\{[]}~;:\"’”—\xa0"
TAG_PATTERN = re.compile(r'(#[\w]+)', re.UNICODE) TAG_PATTERN = re.compile(r'(#[\w]+)', re.UNICODE)
# This will match non matching braces. I don't think it's an issue. # 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) 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): def decode_if_bytes(text):
try: try:
@ -65,28 +64,6 @@ def get_path_from_url(url: str) -> str:
return parsed.path 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: def test_tag(tag: str) -> bool:
"""Test a word whether it could be accepted as a tag.""" """Test a word whether it could be accepted as a tag."""