From 48f89cebc0b580e78999417e9e353880bcfcfcca Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Wed, 22 Nov 2023 19:00:02 -0800 Subject: [PATCH] User.handle_as: handle web users with custom usernames bug fix for b255962, fixes https://console.cloud.google.com/errors/detail/CPiAj72alvDYNQ;time=P30D?project=bridgy-federated --- models.py | 7 +++++-- tests/test_models.py | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/models.py b/models.py index 33ea2f2..6bdb788 100644 --- a/models.py +++ b/models.py @@ -322,8 +322,11 @@ class User(StringIdModel, metaclass=ProtocolUserMeta): if isinstance(to_proto, str): to_proto = PROTOCOLS[to_proto] - return ids.translate_handle(handle=self.handle, from_proto=self.__class__, - to_proto=to_proto) + # override web users to always use domain instead of custom username + handle = self.key.id() if self.LABEL == 'web' else self.handle + + return ids.translate_handle(handle=handle, from_proto=self.__class__, + to_proto=to_proto) def id_as(self, to_proto): """Returns this user's id in a different protocol. diff --git a/tests/test_models.py b/tests/test_models.py index 96807ff..9032705 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -158,6 +158,14 @@ class UserTest(TestCase): self.assertEqual('fake:handle:user', user.handle_as('fake')) self.assertEqual('@fake:handle:user@fa.brid.gy', user.handle_as('ap')) + def test_handle_as_web_custom_username(self, *_): + self.user.obj.our_as1 = { + 'objectType': 'person', + 'url': 'acct:alice@y.z', + } + self.assertEqual('alice', self.user.username()) + self.assertEqual('@y.z@web.brid.gy', self.user.handle_as('ap')) + @patch('requests.get', return_value=requests_response(DID_DOC)) def test_ap_actor(self, mock_get): user = self.make_user('did:plc:abc', cls=ATProto)