From 17ed24b6f5ec1b8a02274df3f78d357d25a0860d Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Thu, 1 Jun 2023 20:58:42 -0700 Subject: [PATCH] AP users: switch ActivityPub.ap_address() to use as2.address() for #512 --- activitypub.py | 5 +---- pages.py | 2 +- tests/test_activitypub.py | 10 ++++++++-- tests/test_pages.py | 15 ++++++++++----- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/activitypub.py b/activitypub.py index 6d56044..dac7a51 100644 --- a/activitypub.py +++ b/activitypub.py @@ -60,10 +60,7 @@ class ActivityPub(User, Protocol): def ap_address(self): """Returns this user's ActivityPub address, eg '@foo.com@foo.com'.""" - if self.direct: - return f'@{self.username()}@{self.key.id()}' - else: - return f'@{self.key.id()}@{request.host}' + return as2.address(self.actor_as2) or as2.address(self.key.id()) def ap_actor(self, rest=None): """Returns this user's ActivityPub/AS2 actor id URL. diff --git a/pages.py b/pages.py index 04ceb2f..8a9fb87 100644 --- a/pages.py +++ b/pages.py @@ -98,8 +98,8 @@ def followers_or_following(protocol, domain, collection): for f in followers: f.url = f.src if collection == 'followers' else f.dest - f.handle = re.sub(r'^https?://(.+)/(users/|@)(.+)$', r'@\3@\1', f.url) person = f.to_as1() + f.handle = as2.address(as2.from_as1(person) or f.url) or f.url if person and isinstance(person, dict): f.name = person.get('name') or '' f.picture = util.get_url(person, 'icon') or util.get_url(person, 'image') diff --git a/tests/test_activitypub.py b/tests/test_activitypub.py index b65a23e..9855024 100644 --- a/tests/test_activitypub.py +++ b/tests/test_activitypub.py @@ -1544,8 +1544,14 @@ class ActivityPubUtilsTest(TestCase): ignore=['to']) def test_ap_actor(self): - user = self.make_user('http://foo/actor', cls=ActivityPub) - self.assertEqual('http://foo/actor', user.ap_actor()) + user = ActivityPub(actor_as2={**ACTOR, 'preferredUsername': 'me'}) + self.assertEqual('@me@mas.to', user.ap_address()) + + user = ActivityPub(actor_as2=ACTOR) + self.assertEqual('@swentel@mas.to', user.ap_address()) + + user = ActivityPub(id='https://mas.to/users/alice') + self.assertEqual('@alice@mas.to', user.ap_address()) def test_ap_address(self): user = self.make_user('http://foo/actor', cls=ActivityPub) diff --git a/tests/test_pages.py b/tests/test_pages.py index 4694478..2c4162f 100644 --- a/tests/test_pages.py +++ b/tests/test_pages.py @@ -114,15 +114,20 @@ class PagesTest(TestCase): def test_followers(self): self.make_user('bar.com') - Follower.get_or_create('bar.com', 'https://no/stored/follow') - Follower.get_or_create('bar.com', 'https://masto/user', - last_follow=FOLLOW_WITH_ACTOR) + Follower.get_or_create('bar.com', 'https://no.stored/users/follow') + Follower.get_or_create('bar.com', 'https://masto/user', last_follow={ + **FOLLOW_WITH_ACTOR, + 'actor': { + **ACTOR, + 'preferredUsername': 'me', + }, + }) got = self.client.get('/web/bar.com/followers') self.assert_equals(200, got.status_code) body = got.get_data(as_text=True) - self.assertIn('no/stored/follow', body) - self.assertIn('masto/user', body) + self.assertIn('@follow@no.stored', body) + self.assertIn('@me@plus.google.com', body) def test_followers_fake(self): self.make_user('foo.com', cls=Fake)