kopia lustrzana https://github.com/snarfed/bridgy-fed
Protocol.receive: replace copy ids with originals, related bug fixes
rodzic
e486c28919
commit
efdf84a023
|
@ -947,8 +947,8 @@ class Object(StringIdModel):
|
|||
|
||||
# batch lookup matching users
|
||||
ids = util.trim_nulls(
|
||||
[outer_obj.get(f) for f in fields]
|
||||
+ [inner_obj.get(f) for f in fields]
|
||||
[as1.get_object(outer_obj, f).get('id') for f in fields]
|
||||
+ [as1.get_object(inner_obj, f).get('id') for f in fields]
|
||||
+ [inner_obj.get('id')]
|
||||
+ [m.get('url') for m in mention_tags])
|
||||
origs = (User.get_for_copies(ids)
|
||||
|
@ -956,7 +956,7 @@ class Object(StringIdModel):
|
|||
|
||||
replaced = False
|
||||
def replace(obj, field):
|
||||
val = obj.get(field)
|
||||
val = as1.get_object(obj, field).get('id')
|
||||
if val:
|
||||
for orig in origs:
|
||||
target = Target(uri=val, protocol=self.source_protocol)
|
||||
|
|
15
protocol.py
15
protocol.py
|
@ -534,6 +534,9 @@ class Protocol:
|
|||
if authed_as and actor != authed_as:
|
||||
logger.warning(f"actor {actor} isn't authed user {authed_as}")
|
||||
|
||||
# update copy ids to originals
|
||||
obj.replace_copies_with_originals()
|
||||
|
||||
# write Object to datastore
|
||||
orig = obj
|
||||
obj = Object.get_or_create(id, **orig.to_dict(), actor=actor)
|
||||
|
@ -567,7 +570,6 @@ class Protocol:
|
|||
|
||||
# store inner object
|
||||
inner_obj_id = inner_obj_as1.get('id')
|
||||
inner_obj = None
|
||||
if obj.type in ('post', 'update') and inner_obj_as1.keys() > set(['id']):
|
||||
Object.get_or_create(inner_obj_id, our_as1=inner_obj_as1,
|
||||
source_protocol=from_cls.LABEL, actor=actor)
|
||||
|
@ -639,11 +641,13 @@ 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']):
|
||||
if not inner_obj and from_cls.owns_id(inner_obj_id):
|
||||
logger.info('Fetching object so we can render it in feeds')
|
||||
inner_obj = from_cls.load(inner_obj_id)
|
||||
if (obj.type == 'share'
|
||||
and inner_obj_as1.keys() == set(['id'])
|
||||
and from_cls.owns_id(inner_obj_id)):
|
||||
logger.info('Fetching object so we can render it in feeds')
|
||||
inner_obj = from_cls.load(inner_obj_id)
|
||||
if inner_obj and inner_obj.as1:
|
||||
obj.our_as1 = {
|
||||
**obj.as1,
|
||||
|
@ -928,7 +932,6 @@ class Protocol:
|
|||
target_uris |= origs
|
||||
logger.info(f'Added originals: {origs}')
|
||||
|
||||
|
||||
orig_obj = None
|
||||
targets = {} # maps Target to Object or None
|
||||
in_reply_to = as1.get_object(obj.as1).get('inReplyTo')
|
||||
|
|
|
@ -750,7 +750,9 @@ class ObjectTest(TestCase):
|
|||
'id': 'fake:reply',
|
||||
'objectType': 'note',
|
||||
'inReplyTo': 'fake:post',
|
||||
'author': 'fake:alice',
|
||||
'author': {
|
||||
'id': 'fake:alice',
|
||||
},
|
||||
'tags': [{
|
||||
'objectType': 'mention',
|
||||
'url': 'fake:bob',
|
||||
|
|
|
@ -824,19 +824,13 @@ class ProtocolReceiveTest(TestCase):
|
|||
], Fake.sent)
|
||||
|
||||
def test_repost_twitter_blocklisted(self):
|
||||
self._test_repost_blocklisted_error('https://twitter.com/foo')
|
||||
|
||||
def test_repost_bridgy_fed_blocklisted(self):
|
||||
self._test_repost_blocklisted_error('https://fed.brid.gy/foo')
|
||||
|
||||
def _test_repost_blocklisted_error(self, orig_url):
|
||||
"""Reposts of non-fediverse (ie blocklisted) sites aren't yet supported."""
|
||||
repost_as1 = {
|
||||
'id': 'fake:repost',
|
||||
'objectType': 'activity',
|
||||
'verb': 'share',
|
||||
'actor': 'fake:user',
|
||||
'object': orig_url,
|
||||
'object': 'https://twitter.com/foo',
|
||||
}
|
||||
with self.assertRaises(NoContent):
|
||||
Fake.receive_as1(repost_as1)
|
||||
|
@ -1386,12 +1380,7 @@ class ProtocolReceiveTest(TestCase):
|
|||
)
|
||||
self.assertEqual(2, Follower.query().count())
|
||||
|
||||
def test_replace_actor_copies_with_originals(self):
|
||||
Fake.fetchable = {
|
||||
'fake:alice': {},
|
||||
'fake:bob': {},
|
||||
}
|
||||
|
||||
def test_replace_actor_copies_with_originals_follow(self):
|
||||
follow = {
|
||||
'id': 'fake:follow',
|
||||
'objectType': 'activity',
|
||||
|
@ -1401,21 +1390,56 @@ class ProtocolReceiveTest(TestCase):
|
|||
}
|
||||
|
||||
# no matching copy users
|
||||
Fake.receive(Object(id='fake:follow', our_as1=follow, source_protocol='fake'))
|
||||
self.assert_equals(follow, Object.get_by_id('fake:follow').our_as1)
|
||||
obj = Object(id='fake:follow', our_as1=follow, source_protocol='fake')
|
||||
Fake.receive(obj)
|
||||
self.assert_equals(follow, obj.our_as1)
|
||||
|
||||
# matching copy users
|
||||
self.make_user('other:alice', cls=OtherFake,
|
||||
copies=[Target(uri='fake:alice', protocol='fake')])
|
||||
# matching copy user
|
||||
self.make_user('other:bob', cls=OtherFake,
|
||||
copies=[Target(uri='fake:bob', protocol='fake')])
|
||||
Fake.receive(Object(id='fake:follow', our_as1=follow, source_protocol='fake'))
|
||||
|
||||
protocol.seen_ids.clear()
|
||||
obj.new = True
|
||||
OtherFake.fetchable = {
|
||||
'other:bob': {},
|
||||
}
|
||||
|
||||
Fake.receive(obj)
|
||||
self.assert_equals({
|
||||
**follow,
|
||||
'actor': 'other:alice',
|
||||
'object': 'other:bob',
|
||||
'actor': {'id': 'fake:alice'},
|
||||
'object': {'id': 'other:bob'},
|
||||
}, Object.get_by_id('fake:follow').our_as1)
|
||||
|
||||
def test_replace_actor_copies_with_originals_share(self):
|
||||
share = {
|
||||
'objectType': 'activity',
|
||||
'verb': 'share',
|
||||
'object': 'fake:post',
|
||||
}
|
||||
|
||||
# no matching copy object
|
||||
obj = Object(id='fake:share', our_as1=share, source_protocol='fake')
|
||||
with self.assertRaises(NoContent):
|
||||
Fake.receive(obj)
|
||||
self.assert_equals(share, obj.our_as1)
|
||||
|
||||
# matching copy object
|
||||
self.store_object(id='other:post',
|
||||
copies=[Target(uri='fake:post', protocol='fake')])
|
||||
|
||||
protocol.seen_ids.clear()
|
||||
obj.new = True
|
||||
with self.assertRaises(NoContent):
|
||||
Fake.receive(obj)
|
||||
|
||||
self.assert_equals({
|
||||
'id': 'fake:share',
|
||||
'objectType': 'activity',
|
||||
'verb': 'share',
|
||||
'object': {'id': 'other:post'},
|
||||
}, obj.our_as1)
|
||||
|
||||
def test_receive_task_handler(self):
|
||||
note = {
|
||||
'id': 'fake:post',
|
||||
|
|
Ładowanie…
Reference in New Issue