activitypub: point actor ids to bridgy fed, not source web site

...since we want to serve them as AS2.

for tootsuite/mastodon#5500
pull/27/head
Ryan Barrett 2017-10-23 21:49:43 -07:00
rodzic e538f97456
commit 786173d270
4 zmienionych plików z 43 dodań i 14 usunięć

Wyświetl plik

@ -13,6 +13,11 @@ except ValueError as e:
from granary.appengine_config import *
if os.environ.get('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
HOST = 'fed.brid.gy'
HOST_URL = '%s://%s' % (SCHEME, HOST)
# Make requests and urllib3 play nice with App Engine.
# https://github.com/snarfed/bridgy/issues/396
# http://stackoverflow.com/questions/34574740

Wyświetl plik

@ -10,11 +10,12 @@ import urlparse
from bs4 import BeautifulSoup
from granary import as2
from oauth_dropins.webutil import util
from oauth_dropins.webutil import handlers, util
import requests
from webmentiontools import send
from webob import exc
import appengine_config
from models import Response
DOMAIN_RE = r'([^/]+\.[^/]+)'
@ -56,6 +57,8 @@ SUPPORTED_VERBS = (
'update',
)
canonicalize_domain = handlers.redirect('bridgy-federated.appspot.com', 'fed.brid.gy')
def requests_get(url, **kwargs):
return _requests_fn(util.requests_get, url, **kwargs)
@ -218,18 +221,22 @@ def postprocess_as2(activity, target=None, key=None):
key: MagicKey, optional. populated into publicKey field if provided.
"""
type = activity.get('type')
if type == 'Person' and not activity.get('publicKey'):
# underspecified, inferred from this issue and Mastodon's implementation:
# https://github.com/w3c/activitypub/issues/203#issuecomment-297553229
# https://github.com/tootsuite/mastodon/blob/bc2c263504e584e154384ecc2d804aeb1afb1ba3/app/services/activitypub/process_account_service.rb#L77
activity['publicKey'] = {
'publicKeyPem': key.public_pem(),
}
# actor objects
if type == 'Person':
activity.setdefault('preferredUsername', USERNAME)
attr = activity.get('attributedTo')
if attr:
attr[0].setdefault('preferredUsername', USERNAME)
postprocess_as2_actor(activity)
if not activity.get('publicKey'):
# underspecified, inferred from this issue and Mastodon's implementation:
# https://github.com/w3c/activitypub/issues/203#issuecomment-297553229
# https://github.com/tootsuite/mastodon/blob/bc2c263504e584e154384ecc2d804aeb1afb1ba3/app/services/activitypub/process_account_service.rb#L77
activity['publicKey'] = {
'publicKeyPem': key.public_pem(),
}
return activity
for actor in (util.get_list(activity, 'attributedTo') +
util.get_list(activity, 'actor')):
postprocess_as2_actor(actor)
# inReplyTo: singly valued, prefer id over url
target_id = target.get('id') if target else None
@ -269,3 +276,17 @@ def postprocess_as2(activity, target=None, key=None):
}
return util.trim_nulls(activity)
def postprocess_as2_actor(actor):
"""Prepare an AS2 actor object to be served or sent via ActivityPub.
Args:
actor: dict, AS2 actor object
"""
actor.setdefault('preferredUsername', USERNAME)
url = actor.get('url')
if url:
actor['id'] = '%s/%s' % (appengine_config.HOST_URL,
urlparse.urlparse(url).netloc)

Wyświetl plik

@ -27,7 +27,7 @@ class ActivityPubTest(testutil.TestCase):
def test_actor_handler(self, mock_get, _):
mock_get.return_value = requests_response("""
<body>
<a class="h-card" rel="me" href="/about-me">Mrs. Foo</a>
<a class="h-card u-url" rel="me" href="/about-me">Mrs. Foo</a>
</body>
""", url='https://foo.com/')
@ -41,7 +41,7 @@ class ActivityPubTest(testutil.TestCase):
'type' : 'Person',
'name': 'Mrs. ☕ Foo',
'preferredUsername': 'me',
'id': 'https://foo.com/about-me',
'id': 'http://localhost/foo.com',
'url': 'https://foo.com/about-me',
'inbox': 'http://localhost/foo.com/inbox',
'publicKey': {

Wyświetl plik

@ -117,8 +117,10 @@ class WebmentionTest(testutil.TestCase):
'cc': [AS2_PUBLIC_AUDIENCE, 'tag:orig,2017:as2'],
'actor': {
'type': 'Person',
'id': 'http://localhost/orig',
'url': 'http://orig',
'name': 'Ms. ☕ Baz',
'preferredUsername': 'me',
},
}
@ -161,6 +163,7 @@ class WebmentionTest(testutil.TestCase):
],
'attributedTo': [{
'type': 'Person',
'id': 'http://localhost/orig',
'url': 'http://orig',
'preferredUsername': 'me',
'name': 'Ms. ☕ Baz',