kopia lustrzana https://github.com/snarfed/bridgy-fed
add Object.as_as2()
rodzic
99bb564209
commit
1821e90f36
|
@ -108,9 +108,7 @@ class ActivityPub(User, Protocol):
|
|||
if obj.type not in as1.ACTOR_TYPES:
|
||||
logger.info(f'{obj.key} type {type} is not an actor')
|
||||
|
||||
actor = obj.as2 or as2.from_as1(obj.as1)
|
||||
if not actor:
|
||||
return None
|
||||
actor = obj.as_as2()
|
||||
|
||||
if shared:
|
||||
shared_inbox = actor.get('endpoints', {}).get('sharedInbox')
|
||||
|
|
10
models.py
10
models.py
|
@ -199,10 +199,8 @@ class User(StringIdModel, metaclass=ProtocolUserMeta):
|
|||
u._obj = keys_to_objs.get(u.obj_key)
|
||||
|
||||
def as2(self):
|
||||
if self.obj and self.obj.as1:
|
||||
return self.obj.as2 or as2.from_as1(self.obj.as1)
|
||||
|
||||
return {}
|
||||
"""Returns this user as an AS2 actor."""
|
||||
return self.obj.as_as2() if self.obj else {}
|
||||
|
||||
@ndb.ComputedProperty
|
||||
def readable_id(self):
|
||||
|
@ -456,6 +454,10 @@ class Object(StringIdModel):
|
|||
logger.warning(f'Wiping out {prop}: {json_dumps(val, indent=2)}')
|
||||
setattr(self, prop, None)
|
||||
|
||||
def as_as2(self):
|
||||
"""Returns this object as an AS2 dict."""
|
||||
return self.as2 or as2.from_as1(self.as1) or {}
|
||||
|
||||
def proxy_url(self):
|
||||
"""Returns the Bridgy Fed proxy URL to render this post as HTML.
|
||||
|
||||
|
|
|
@ -420,8 +420,7 @@ class Protocol:
|
|||
|
||||
# fetch object if necessary so we can render it in feeds
|
||||
if obj.type == 'share' and inner_obj.keys() == set(['id']):
|
||||
inner_obj = obj.as2['object'] = as2.from_as1(
|
||||
from_cls.load(inner_obj_id).as1)
|
||||
inner_obj = obj.as2['object'] = from_cls.load(inner_obj_id).as_as2()
|
||||
|
||||
if obj.type == 'follow':
|
||||
from_cls.accept_follow(obj)
|
||||
|
@ -487,7 +486,7 @@ class Protocol:
|
|||
'id': common.host_url(f'/web/{g.user.key.id()}/followers#accept-{obj.key.id()}'),
|
||||
'type': 'Accept',
|
||||
'actor': followee_actor_url,
|
||||
'object': as2.from_as1(obj.as1),
|
||||
'object': obj.as_as2()
|
||||
}
|
||||
return cls.send(Object(as2=accept), inbox)
|
||||
|
||||
|
|
|
@ -322,10 +322,7 @@ class ActivityPubTest(TestCase):
|
|||
type = got.headers['Content-Type']
|
||||
self.assertTrue(type.startswith(as2.CONTENT_TYPE), type)
|
||||
self.assertEqual({
|
||||
'@context': [
|
||||
'https://www.w3.org/ns/activitystreams',
|
||||
'https://w3id.org/security/v1',
|
||||
],
|
||||
'@context': ['https://w3id.org/security/v1'],
|
||||
'type': 'Person',
|
||||
'id': 'http://bf/fake/user.com/ap',
|
||||
'preferredUsername': 'user.com',
|
||||
|
@ -850,6 +847,9 @@ class ActivityPubTest(TestCase):
|
|||
self.assertEqual(2, len(mock_post.call_args_list))
|
||||
args, kwargs = mock_post.call_args_list[0]
|
||||
self.assertEqual(('http://mas.to/inbox',), args)
|
||||
|
||||
accept_as2 = copy.deepcopy(accept_as2)
|
||||
accept_as2['object']['actor']['@context'] = 'https://www.w3.org/ns/activitystreams'
|
||||
self.assertEqual(accept_as2, json_loads(kwargs['data']))
|
||||
|
||||
# check webmention
|
||||
|
|
|
@ -102,6 +102,18 @@ class UserTest(TestCase):
|
|||
def test_readable_id(self):
|
||||
self.assertIsNone(g.user.readable_id)
|
||||
|
||||
def test_as2(self):
|
||||
self.assertEqual({}, g.user.as2())
|
||||
|
||||
obj = Object(id='foo')
|
||||
g.user.obj_key = obj.key # doesn't exist
|
||||
self.assertEqual({}, g.user.as2())
|
||||
|
||||
del g.user._obj
|
||||
obj.as2 = {'foo': 'bar'}
|
||||
obj.put()
|
||||
self.assertEqual({'foo': 'bar'}, g.user.as2())
|
||||
|
||||
|
||||
class ObjectTest(TestCase):
|
||||
def setUp(self):
|
||||
|
@ -199,6 +211,26 @@ class ObjectTest(TestCase):
|
|||
obj.put()
|
||||
self.assertEqual(['user'], obj.labels)
|
||||
|
||||
def test_as2(self):
|
||||
obj = Object(id='foo')
|
||||
self.assertEqual({}, obj.as_as2())
|
||||
|
||||
obj.our_as1 = {}
|
||||
self.assertEqual({}, obj.as_as2())
|
||||
|
||||
obj.our_as1 = {
|
||||
'objectType': 'person',
|
||||
'foo': 'bar',
|
||||
}
|
||||
self.assertEqual({
|
||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||
'type': 'Person',
|
||||
'foo': 'bar',
|
||||
}, obj.as_as2())
|
||||
|
||||
obj.as2 = {'baz': 'biff'}
|
||||
self.assertEqual({'baz': 'biff'}, obj.as_as2())
|
||||
|
||||
|
||||
class FollowerTest(TestCase):
|
||||
|
||||
|
|
5
web.py
5
web.py
|
@ -709,7 +709,7 @@ def _activitypub_targets(obj):
|
|||
try:
|
||||
# TODO: make this generic across protocols
|
||||
target_stored = activitypub.ActivityPub.load(target)
|
||||
target_obj = target_stored.as2 or as2.from_as1(target_stored.as1)
|
||||
target_obj = target_stored.as_as2()
|
||||
except (HTTPError, BadGateway) as e:
|
||||
resp = getattr(e, 'requests_response', None)
|
||||
if resp and resp.ok:
|
||||
|
@ -734,8 +734,7 @@ def _activitypub_targets(obj):
|
|||
if not inbox_url:
|
||||
# fetch actor as AS object
|
||||
# TODO: make this generic across protocols
|
||||
actor_obj = activitypub.ActivityPub.load(actor)
|
||||
actor = actor_obj.as2 or as2.from_as1(actor_obj.as1)
|
||||
actor = activitypub.ActivityPub.load(actor).as_as2()
|
||||
inbox_url = actor.get('inbox')
|
||||
|
||||
if not inbox_url:
|
||||
|
|
Ładowanie…
Reference in New Issue