kopia lustrzana https://github.com/snarfed/bridgy-fed
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! #690pull/691/head
rodzic
fe3a9b693c
commit
3b88b0f315
|
@ -198,13 +198,9 @@ class ActivityPub(User, Protocol):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
orig_as2 = orig_obj.as_as2() if orig_obj else None
|
orig_as2 = orig_obj.as_as2() if orig_obj else None
|
||||||
activity = obj.as2 or postprocess_as2(as2.from_as1(obj.as1),
|
activity = obj.as2 or postprocess_as2(obj.as_as2(), orig_obj=orig_as2)
|
||||||
orig_obj=orig_as2)
|
|
||||||
|
|
||||||
if g.user:
|
if not activity.get('actor'):
|
||||||
logger.debug(f'Overwriting actor with g.user {g.user.ap_actor()}')
|
|
||||||
activity['actor'] = g.user.ap_actor()
|
|
||||||
elif not activity.get('actor'):
|
|
||||||
logger.warning('Outgoing AP activity has no actor!')
|
logger.warning('Outgoing AP activity has no actor!')
|
||||||
|
|
||||||
return signed_post(url, log_data=log_data, data=activity).ok
|
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('#'):
|
if not name.startswith('#'):
|
||||||
tag['name'] = f'#{name}'
|
tag['name'] = f'#{name}'
|
||||||
|
|
||||||
activity['object'] = postprocess_as2(activity.get('object'), orig_obj=orig_obj,
|
activity['object'] = postprocess_as2(
|
||||||
wrap=type in ('Create', 'Update', 'Delete'))
|
activity.get('object'),
|
||||||
|
orig_obj=orig_obj,
|
||||||
|
wrap=wrap and type in ('Create', 'Update', 'Delete'))
|
||||||
|
|
||||||
return util.trim_nulls(activity)
|
return util.trim_nulls(activity)
|
||||||
|
|
||||||
|
@ -824,31 +822,6 @@ def inbox(protocol=None, id=None):
|
||||||
actor_id = actor.get('id')
|
actor_id = actor.get('id')
|
||||||
logger.info(f'Got {type} from {actor_id}: {json_dumps(activity, indent=2)}')
|
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)
|
authed_as = ActivityPub.verify_signature(activity)
|
||||||
|
|
||||||
# check that this activity is public. only do this for creates, not likes,
|
# check that this activity is public. only do this for creates, not likes,
|
||||||
|
|
|
@ -643,7 +643,6 @@ class Protocol:
|
||||||
if actor_obj and actor_obj.as1:
|
if actor_obj and actor_obj.as1:
|
||||||
obj.our_as1 = {**obj.as1, 'actor': actor_obj.as1}
|
obj.our_as1 = {**obj.as1, 'actor': actor_obj.as1}
|
||||||
|
|
||||||
|
|
||||||
# fetch object if necessary so we can render it in feeds
|
# fetch object if necessary so we can render it in feeds
|
||||||
if (obj.type == 'share'
|
if (obj.type == 'share'
|
||||||
and inner_obj_as1.keys() == set(['id'])
|
and inner_obj_as1.keys() == set(['id'])
|
||||||
|
@ -753,7 +752,15 @@ class Protocol:
|
||||||
'actor': to_id,
|
'actor': to_id,
|
||||||
'object': obj.as1,
|
'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)
|
sent = from_cls.send(accept, from_target)
|
||||||
|
g.user = orig_g_user
|
||||||
|
|
||||||
if sent:
|
if sent:
|
||||||
accept.populate(
|
accept.populate(
|
||||||
delivered=[Target(protocol=from_cls.LABEL, uri=from_target)],
|
delivered=[Target(protocol=from_cls.LABEL, uri=from_target)],
|
||||||
|
|
|
@ -440,8 +440,7 @@ class ActivityPubTest(TestCase):
|
||||||
}
|
}
|
||||||
self._test_inbox_reply(reply, mock_head, mock_get, mock_post)
|
self._test_inbox_reply(reply, mock_head, mock_get, mock_post)
|
||||||
|
|
||||||
self.assert_user(ActivityPub, 'https://mas.to/actor',
|
self.assert_user(ActivityPub, 'https://mas.to/actor', obj_as2=LIKE_ACTOR)
|
||||||
obj_as2=LIKE_ACTOR, direct=True)
|
|
||||||
|
|
||||||
def test_inbox_activity_without_id(self, *_):
|
def test_inbox_activity_without_id(self, *_):
|
||||||
note = copy.deepcopy(NOTE)
|
note = copy.deepcopy(NOTE)
|
||||||
|
@ -449,12 +448,6 @@ class ActivityPubTest(TestCase):
|
||||||
resp = self.post('/ap/sharedInbox', json=note)
|
resp = self.post('/ap/sharedInbox', json=note)
|
||||||
self.assertEqual(400, resp.status_code)
|
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):
|
def test_inbox_reply_object(self, mock_head, mock_get, mock_post):
|
||||||
self._test_inbox_reply(REPLY_OBJECT, 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)
|
mock_get.return_value = self.as2_resp(LIKE_ACTOR)
|
||||||
|
|
||||||
self.test_inbox_like()
|
self.test_inbox_like()
|
||||||
self.assert_user(ActivityPub, 'https://mas.to/actor',
|
self.assert_user(ActivityPub, 'https://mas.to/actor', obj_as2=LIKE_ACTOR)
|
||||||
obj_as2=LIKE_ACTOR, direct=True)
|
|
||||||
|
|
||||||
def test_inbox_like_no_object_error(self, *_):
|
def test_inbox_like_no_object_error(self, *_):
|
||||||
Fake.fetchable = {'fake:user': {'id': 'fake:user'}}
|
Fake.fetchable = {'fake:user': {'id': 'fake:user'}}
|
||||||
|
|
Ładowanie…
Reference in New Issue