diff --git a/common.py b/common.py index ddd5b2c..9ec553c 100644 --- a/common.py +++ b/common.py @@ -6,7 +6,7 @@ from pathlib import Path import re import threading import urllib.parse -from urllib.parse import urljoin +from urllib.parse import urljoin, urlparse import cachetools from Crypto.Util import number @@ -195,7 +195,15 @@ def unwrap(val, field=None): Returns: str: unwrapped url """ + if isinstance(val, dict): + # TODO: clean up. https://github.com/snarfed/bridgy-fed/issues/967 + id = val.get('id') + if (id and urlparse(id).path.strip('/') in DOMAINS + ('',) + and util.domain_from_link(id) in DOMAINS): + # protocol bot user, don't touch its URLs + return {**val, 'id': unwrap(id)} + return {f: unwrap(v, field=f) for f, v in val.items()} elif isinstance(val, list): diff --git a/tests/test_models.py b/tests/test_models.py index 448b4ab..034982e 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -628,6 +628,26 @@ class ObjectTest(TestCase): self.assertEqual({'id': 'x', 'foo': 'bar'}, Object(id='x', our_as1={'foo': 'bar'}).as1) + def test_as1_from_as2_protocol_bot_user(self): + self.assert_equals({ + 'objectType': 'application', + 'id': 'fed.brid.gy', + 'url': 'https://fed.brid.gy/', + 'displayName': 'Bridgy Fed', + 'summary': 'Bridging the new social internet', + 'username': 'fed.brid.gy', + 'image': [{ + 'displayName': 'Bridgy Fed', + 'url': 'https://fed.brid.gy/static/bridgy_logo_square.jpg', + }, { + 'url': 'https://fed.brid.gy/static/bridgy_logo.jpg', + }], + 'attachments': [{ + 'displayName': 'Web site', + 'value': 'fed.brid.gy', + }], + }, Web.load('https://fed.brid.gy/').as1) + def test_atom_url_overrides_id(self): obj = { 'objectType': 'note',