Skip to content

Commit

Permalink
activitypub.postprocess_as2_actor: handle user without handle
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Nov 24, 2023
1 parent dcd7081 commit db2668f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
28 changes: 15 additions & 13 deletions activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,9 @@ def postprocess_as2_actor(actor, wrap=True):
# https://docs.joinmastodon.org/spec/webfinger/#mastodons-requirements-for-webfinger
# https://github.com/snarfed/bridgy-fed/issues/302#issuecomment-1324305460
# https://github.com/snarfed/bridgy-fed/issues/77
actor['preferredUsername'] = g.user.handle_as(ActivityPub).strip('@').split('@')[0]
handle = g.user.handle_as(ActivityPub)
if handle:
actor['preferredUsername'] = handle.strip('@').split('@')[0]

# Override the label for their home page to be "Web site"
for att in util.get_list(actor, 'attachment'):
Expand Down Expand Up @@ -942,15 +944,15 @@ def outbox(id):
objects, before, after = fetch_objects(query, by=Object.updated, user=g.user)

return {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': request.url,
'summary': f"{id}'s outbox",
'type': 'OrderedCollection',
# TODO. needs to handle deleted
# 'totalItems': query.count(),
'first': {
'type': 'CollectionPage',
'partOf': request.base_url,
'items': [ActivityPub.convert(obj) for obj in objects],
},
}, {'Content-Type': as2.CONTENT_TYPE}
'@context': 'https://www.w3.org/ns/activitystreams',
'id': request.url,
'summary': f"{id}'s outbox",
'type': 'OrderedCollection',
# TODO. needs to handle deleted
# 'totalItems': query.count(),
'first': {
'type': 'CollectionPage',
'partOf': request.base_url,
'items': [ActivityPub.convert(obj) for obj in objects],
},
}, {'Content-Type': as2.CONTENT_TYPE}
11 changes: 5 additions & 6 deletions atproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ def _pre_put_hook(self):
@ndb.ComputedProperty
def handle(self):
"""Returns handle if the DID document includes one, otherwise None."""
did_obj = ATProto.load(self.key.id())
if did_obj:
handle, _, _ = parse_at_uri(
util.get_first(did_obj.raw, 'alsoKnownAs', ''))
if handle:
return handle
if did_obj := ATProto.load(self.key.id()):
if aka := util.get_first(did_obj.raw, 'alsoKnownAs', ''):
handle, _, _ = parse_at_uri(aka)
if handle:
return handle

def web_url(self):
return bluesky.Bluesky.user_url(self.handle_or_id())
Expand Down
8 changes: 8 additions & 0 deletions tests/test_activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import activitypub
from activitypub import ActivityPub, default_signature_user, postprocess_as2
from atproto import ATProto
import common
from models import Follower, Object
import protocol
Expand Down Expand Up @@ -428,6 +429,13 @@ def test_actor_handle_new_user(self, _, __, ___):
**ACTOR_FAKE,
}, got.json, ignore=['publicKeyPem'])

def test_actor_no_handle(self, *_):
self.store_object(id='did:plc:user', raw={'foo': 'bar'})
self.make_user('did:plc:user', cls=ATProto)
got = self.client.get('/ap/did:plc:user', base_url='https://atproto.brid.gy/')
self.assertEqual(200, got.status_code)
self.assertNotIn('preferredUsername', got.json)

def test_actor_handle_user_fetch_fails(self, _, __, ___):
got = self.client.get('/ap/fake/fake:handle:nope')
self.assertEqual(404, got.status_code)
Expand Down

0 comments on commit db2668f

Please sign in to comment.