summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activities/models/post.py9
-rw-r--r--setup.cfg2
-rw-r--r--tests/activities/models/test_post.py32
-rw-r--r--tests/conftest.py35
4 files changed, 72 insertions, 6 deletions
diff --git a/activities/models/post.py b/activities/models/post.py
index 6888b3f..7016077 100644
--- a/activities/models/post.py
+++ b/activities/models/post.py
@@ -167,25 +167,24 @@ class Post(StatorModel):
"""
def replacer(match):
- print(match)
precursor = match.group(1)
handle = match.group(2)
# If the handle has no domain, try to match it with a mention
if "@" not in handle.lstrip("@"):
- identity = self.mentions.filter(username=handle.lstrip("@")).first()
+ username = handle.lstrip("@")
+ identity = self.mentions.filter(username=username).first()
if identity:
url = identity.urls.view
else:
- url = None
+ url = f"/@{username}/"
else:
url = f"/{handle}/"
# If we have a URL, link to it, otherwise don't link
if url:
- return f"{precursor}<a href='{url}'>{handle}</a>"
+ return f'{precursor}<a href="{url}">{handle}</a>'
else:
return match.group()
- print(f"replacing on {content!r}")
return mark_safe(self.mention_regex.sub(replacer, content))
@property
diff --git a/setup.cfg b/setup.cfg
index 862d7c3..0895d9e 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -8,7 +8,7 @@ profile = black
multi_line_output = 3
[tool:pytest]
-addopts = --tb=short --ds=takahe.settings.testing
+addopts = --tb=short --ds=takahe.settings.testing --import-mode=importlib
filterwarnings =
ignore:There is no current event loop
diff --git a/tests/activities/models/test_post.py b/tests/activities/models/test_post.py
index 5c7fca2..fb2e7a6 100644
--- a/tests/activities/models/test_post.py
+++ b/tests/activities/models/test_post.py
@@ -29,3 +29,35 @@ def test_fetch_post(httpx_mock: HTTPXMock):
assert post.author.actor_uri == "https://example.com/test-actor"
# Fetch again with a DB hit
assert Post.by_object_uri("https://example.com/test-post").id == post.id
+
+
+@pytest.mark.django_db
+def test_linkify_mentions(identity, remote_identity):
+ """
+ Tests that we can linkify post mentions properly
+ """
+ # Test a short username without a mention (presumed local)
+ post = Post.objects.create(
+ content="<p>Hello @test</p>",
+ author=identity,
+ local=True,
+ )
+ assert post.safe_content == '<p>Hello <a href="/@test/">@test</a></p>'
+ # Test a full username
+ post = Post.objects.create(
+ content="<p>@test@example.com, welcome!</p>",
+ author=identity,
+ local=True,
+ )
+ assert (
+ post.safe_content
+ == '<p><a href="/@test@example.com/">@test@example.com</a>, welcome!</p>'
+ )
+ # Test a short username with a mention resolving to remote
+ post = Post.objects.create(
+ content="<p>Hello @test</p>",
+ author=identity,
+ local=True,
+ )
+ post.mentions.add(remote_identity)
+ assert post.safe_content == '<p>Hello <a href="/@test@remote.test/">@test</a></p>'
diff --git a/tests/conftest.py b/tests/conftest.py
index 79bdf60..24fac9a 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,6 +1,7 @@
import pytest
from core.models import Config
+from users.models import Domain, Identity, User
@pytest.fixture
@@ -57,3 +58,37 @@ def config_system(keypair):
system_actor_public_key=keypair["public_key"],
)
yield Config.system
+
+
+@pytest.fixture
+@pytest.mark.django_db
+def identity():
+ """
+ Creates a basic test identity with a user and domain.
+ """
+ user = User.objects.create(email="test@example.com")
+ domain = Domain.objects.create(domain="example.com", local=True, public=True)
+ return Identity.objects.create(
+ actor_uri="https://example.com/test-actor/",
+ username="test",
+ domain=domain,
+ user=user,
+ name="Test User",
+ local=True,
+ )
+
+
+@pytest.fixture
+@pytest.mark.django_db
+def remote_identity():
+ """
+ Creates a basic remote test identity with a domain.
+ """
+ domain = Domain.objects.create(domain="remote.test", local=False)
+ return Identity.objects.create(
+ actor_uri="https://remote.test/test-actor/",
+ username="test",
+ domain=domain,
+ name="Test Remote User",
+ local=False,
+ )