AP: don't create "receiving" user in inbox, don't set actor to g.user in send

more iffy kinda scary changes. gulp. important though, baby step toward killing g.user! #690
pull/691/head
Ryan Barrett 2023-10-19 16:15:40 -07:00
rodzic fe3a9b693c
commit 3b88b0f315
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
3 zmienionych plików z 16 dodań i 44 usunięć

Wyświetl plik

@ -198,13 +198,9 @@ class ActivityPub(User, Protocol):
return False
orig_as2 = orig_obj.as_as2() if orig_obj else None
activity = obj.as2 or postprocess_as2(as2.from_as1(obj.as1),
orig_obj=orig_as2)
activity = obj.as2 or postprocess_as2(obj.as_as2(), orig_obj=orig_as2)
if g.user:
logger.debug(f'Overwriting actor with g.user {g.user.ap_actor()}')
activity['actor'] = g.user.ap_actor()
elif not activity.get('actor'):
if not activity.get('actor'):
logger.warning('Outgoing AP activity has no actor!')
return signed_post(url, log_data=log_data, data=activity).ok
@ -666,8 +662,10 @@ def postprocess_as2(activity, orig_obj=None, wrap=True):
if not name.startswith('#'):
tag['name'] = f'#{name}'
activity['object'] = postprocess_as2(activity.get('object'), orig_obj=orig_obj,
wrap=type in ('Create', 'Update', 'Delete'))
activity['object'] = postprocess_as2(
activity.get('object'),
orig_obj=orig_obj,
wrap=wrap and type in ('Create', 'Update', 'Delete'))
return util.trim_nulls(activity)
@ -824,31 +822,6 @@ def inbox(protocol=None, id=None):
actor_id = actor.get('id')
logger.info(f'Got {type} from {actor_id}: {json_dumps(activity, indent=2)}')
# load receiving user
obj_id = as1.get_object(redirect_unwrap(activity)).get('id')
to_proto = PROTOCOLS[protocol] if protocol else Protocol.for_request(fed='web')
if not to_proto and type == 'Follow':
to_proto = Protocol.for_id(obj_id)
if to_proto:
logger.info(f'To protocol {to_proto.LABEL}')
to_user_id = None
if id:
to_user_id = id
else:
to_key = to_proto.key_for(obj_id)
if to_key:
to_user_id = to_key.id()
if to_user_id:
g.user = to_proto.get_or_create(to_user_id, direct=False)
logger.info(f'Setting g.user to {g.user.key}')
if not g.user.direct and actor_id:
# this is a deliberate interaction with an indirect receiving user;
# create a local AP User for the sending user
actor_obj = ActivityPub.load(actor_id)
ActivityPub.get_or_create(actor_id, direct=True, obj=actor_obj)
authed_as = ActivityPub.verify_signature(activity)
# check that this activity is public. only do this for creates, not likes,

Wyświetl plik

@ -643,7 +643,6 @@ class Protocol:
if actor_obj and actor_obj.as1:
obj.our_as1 = {**obj.as1, 'actor': actor_obj.as1}
# fetch object if necessary so we can render it in feeds
if (obj.type == 'share'
and inner_obj_as1.keys() == set(['id'])
@ -753,7 +752,15 @@ class Protocol:
'actor': to_id,
'object': obj.as1,
})
# TODO: ugly, brittle. dangerous. remove once postprocess_as2 no
# longer depends on g.user!
# https://github.com/snarfed/bridgy-fed/issues/690
orig_g_user = g.user
g.user = to_user
sent = from_cls.send(accept, from_target)
g.user = orig_g_user
if sent:
accept.populate(
delivered=[Target(protocol=from_cls.LABEL, uri=from_target)],

Wyświetl plik

@ -440,8 +440,7 @@ class ActivityPubTest(TestCase):
}
self._test_inbox_reply(reply, mock_head, mock_get, mock_post)
self.assert_user(ActivityPub, 'https://mas.to/actor',
obj_as2=LIKE_ACTOR, direct=True)
self.assert_user(ActivityPub, 'https://mas.to/actor', obj_as2=LIKE_ACTOR)
def test_inbox_activity_without_id(self, *_):
note = copy.deepcopy(NOTE)
@ -449,12 +448,6 @@ class ActivityPubTest(TestCase):
resp = self.post('/ap/sharedInbox', json=note)
self.assertEqual(400, resp.status_code)
def test_inbox_no_matching_protocol(self, mock_head, mock_get, mock_post):
# TODO: remove
mock_get.return_value = self.as2_resp(ACTOR)
resp = self.post('/foo.json/inbox', json=NOTE)
self.assertEqual(400, resp.status_code)
def test_inbox_reply_object(self, mock_head, mock_get, mock_post):
self._test_inbox_reply(REPLY_OBJECT, mock_head, mock_get, mock_post)
@ -809,8 +802,7 @@ class ActivityPubTest(TestCase):
mock_get.return_value = self.as2_resp(LIKE_ACTOR)
self.test_inbox_like()
self.assert_user(ActivityPub, 'https://mas.to/actor',
obj_as2=LIKE_ACTOR, direct=True)
self.assert_user(ActivityPub, 'https://mas.to/actor', obj_as2=LIKE_ACTOR)
def test_inbox_like_no_object_error(self, *_):
Fake.fetchable = {'fake:user': {'id': 'fake:user'}}