Skip to content

Commit

Permalink
ActivityPub.convert bug fix: don't call postprocess_as2_actor without…
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Dec 14, 2023
1 parent 07177e7 commit b999cfe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
22 changes: 12 additions & 10 deletions activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,18 @@ def convert(cls, obj, orig_obj=None, from_user=None):

# special cases where obj or obj['object'] or obj['object']['object']
# are an actor
if as1.object_type(obj.as1) in as1.ACTOR_TYPES:
return postprocess_as2_actor(converted, user=from_user)
if from_user:
if as1.object_type(obj.as1) in as1.ACTOR_TYPES:
return postprocess_as2_actor(converted, user=from_user)

inner_obj = as1.get_object(obj.as1)
if as1.object_type(inner_obj) in as1.ACTOR_TYPES:
converted['object'] = postprocess_as2_actor(converted['object'],
user=from_user)
inner_obj = as1.get_object(obj.as1)
if as1.object_type(inner_obj) in as1.ACTOR_TYPES:
converted['object'] = postprocess_as2_actor(converted['object'],
user=from_user)

# eg Accept of a Follow
if from_user and from_user.is_web_url(as1.get_object(inner_obj).get('id')):
converted['object']['object'] = from_user.id_as(ActivityPub)
# eg Accept of a Follow
if from_user.is_web_url(as1.get_object(inner_obj).get('id')):
converted['object']['object'] = from_user.id_as(ActivityPub)

# convert!
return postprocess_as2(converted, orig_obj=orig_obj)
Expand Down Expand Up @@ -676,7 +677,7 @@ def postprocess_as2(activity, orig_obj=None, wrap=True):
return util.trim_nulls(activity)


def postprocess_as2_actor(actor, user=None):
def postprocess_as2_actor(actor, user):
"""Prepare an AS2 actor object to be served or sent via ActivityPub.
Modifies actor in place.
Expand All @@ -692,6 +693,7 @@ def postprocess_as2_actor(actor, user=None):
return actor

assert isinstance(actor, dict)
assert user

url = user.web_url()
urls = [u for u in util.get_list(actor, 'url') if u and not u.startswith('acct:')]
Expand Down
35 changes: 35 additions & 0 deletions tests/test_activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,9 @@ def setUp(self):
super().setUp()
self.user = self.make_user('user.com', cls=Web, has_hcard=True, obj_as2=ACTOR)

for obj in ACTOR_BASE, ACTOR_FAKE:
obj['publicKey']['publicKeyPem'] = self.user.public_pem().decode()

def test_put_validates_id(self, *_):
for bad in (
'',
Expand Down Expand Up @@ -2063,6 +2066,38 @@ def test_convert(self):
'to': [as2.PUBLIC_AUDIENCE],
}, ActivityPub.convert(obj))

def test_convert_actor_as2(self):
self.assert_equals(ACTOR, ActivityPub.convert(Object(as2=ACTOR)))

def test_convert_actor_as1_from_user(self):
obj = Object(our_as1={
'objectType': 'person',
'id': 'https://user.com/',
})
self.assert_equals(ACTOR_BASE, ActivityPub.convert(obj, from_user=self.user),
ignore=['endpoints', 'followers', 'following'])

def test_convert_actor_as1_no_from_user(self):
obj = Object(our_as1=ACTOR_AS1)
self.assert_equals(ACTOR, common.unwrap(ActivityPub.convert(obj)),
ignore=['to', 'attachment'])

def test_convert_follow_as1_no_from_user(self):
obj = Object(our_as1=as2.to_as1(FOLLOW))
self.assert_equals(FOLLOW, common.unwrap(ActivityPub.convert(obj)),
ignore=['to'])

def test_convert_profile_update_as1_no_from_user(self):
obj = Object(our_as1={
'objectType': 'activity',
'verb': 'update',
'object': ACTOR_AS1,
})
self.assert_equals({
'type': 'Update',
'object': ACTOR,
}, common.unwrap(ActivityPub.convert(obj)), ignore=['to', 'attachment'])

def test_convert_compact_actor_attributedTo_author(self):
obj = Object(our_as1={
'actor': {'id': 'baj'},
Expand Down

0 comments on commit b999cfe

Please sign in to comment.