From 37d8b5d4fc9112b84f10101b46731ae6d720e1d1 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Fri, 3 Nov 2023 11:00:34 -0700 Subject: [PATCH] ids.translate_user_id: for Web, normalize home page URLs to domains --- ids.py | 6 ++++++ tests/test_ids.py | 3 +++ 2 files changed, 9 insertions(+) diff --git a/ids.py b/ids.py index 7613c96..2994c55 100644 --- a/ids.py +++ b/ids.py @@ -4,6 +4,7 @@ https://fed.brid.gy/docs#translate """ import logging import re +from urllib.parse import urlparse from common import subdomain_wrap, SUPERDOMAIN import models @@ -25,6 +26,11 @@ def translate_user_id(*, id, from_proto, to_proto): assert id and from_proto and to_proto assert from_proto.owns_id(id) is not False + parsed = urlparse(id) + if from_proto.LABEL == 'web' and parsed.path.strip('/') == '': + # home page; replace with domain + id = parsed.netloc + if from_proto == to_proto: return id diff --git a/tests/test_ids.py b/tests/test_ids.py index f9cdf24..47851ae 100644 --- a/tests/test_ids.py +++ b/tests/test_ids.py @@ -30,9 +30,12 @@ class IdsTest(TestCase): (Fake, 'fake:user', Fake, 'fake:user'), (Fake, 'fake:user', Web, 'fake:user'), (Web, 'user.com', ActivityPub, 'https://fed.brid.gy/user.com'), + (Web, 'https://user.com/', ActivityPub, 'https://fed.brid.gy/user.com'), (Web, 'user.com', ATProto, 'did:plc:123'), + (Web, 'https://user.com', ATProto, 'did:plc:123'), (Web, 'user.com', Fake, 'fake:u:user.com'), (Web, 'user.com', Web, 'user.com'), + (Web, 'https://user.com/', Web, 'user.com'), ]: with self.subTest(from_=from_.LABEL, to=to.LABEL): self.assertEqual(expected, translate_user_id(