webmention: if u-url is missing, default to source url

AS2 id, which is required for ActivityPub, comes from u-url, so we always need one.

fixes #20
pull/27/head
Ryan Barrett 2017-12-12 18:07:12 -08:00
rodzic a67e44f110
commit be2ea6ad28
4 zmienionych plików z 33 dodań i 2 usunięć

Wyświetl plik

@ -92,7 +92,7 @@ handlers:
secure: always
skip_files:
- ^(.*/)?.*\.py[co]
- ^(.*/)?.*\.(c|cc|cpp|h|o|py[co]|so)$
- ^(.*/)?.*/RCS/.*
- ^(.*/)?\.git.*
- ^(.*/)?.*\.bak$

Wyświetl plik

@ -261,10 +261,12 @@ def postprocess_as2(activity, target=None, key=None):
elif obj != target_id:
activity['object'] = target_id
# default id to url
# id is required for most things. default to url if it's not set.
if not activity.get('id'):
activity['id'] = activity.get('url')
assert activity.get('id') or (isinstance(obj, dict) and obj.get('id'))
# cc public and target's author(s) and recipients
# https://www.w3.org/TR/activitystreams-vocabulary/#audienceTargeting
# https://w3c.github.io/activitypub/#delivery

Wyświetl plik

@ -304,6 +304,29 @@ class WebmentionTest(testutil.TestCase):
self.assertEqual(('https://foo.com/inbox',), args)
self.assertEqual(self.as2_create, kwargs['json'])
def test_activitypub_create_default_url_to_wm_source(self, mock_get, mock_post):
"""Source post has no u-url. AS2 id should default to webmention source."""
missing_url = requests_response("""\
<html>
<body class="h-entry">
<a class="u-repost-of p-name" href="http://orig/post">reposted!</a>
<a class="p-author h-card" href="http://orig">Ms. Baz</a>
</body>
</html>
""", content_type=CONTENT_TYPE_HTML)
mock_get.side_effect = [missing_url, self.orig_as2, self.actor]
mock_post.return_value = requests_response('abc xyz', status=203)
got = app.get_response('/webmention', method='POST', body=urllib.urlencode({
'source': 'http://a/repost',
'target': 'https://fed.brid.gy/',
}))
self.assertEquals(203, got.status_int)
args, kwargs = mock_post.call_args
self.assertEqual(('https://foo.com/inbox',), args)
self.assert_equals(self.repost_as2, kwargs['json'])
def test_salmon_reply(self, mock_get, mock_post):
mock_get.side_effect = [self.reply, self.orig_html_atom, self.orig_atom]

Wyświetl plik

@ -74,6 +74,12 @@ class WebmentionHandler(webapp2.RequestHandler):
entry = mf2util.find_first_entry(source_mf2, ['h-entry'])
logging.info('First entry: %s', json.dumps(entry, indent=2))
# make sure it has url, since we use that for AS2 id, which is required
# for ActivityPub.
props = entry.setdefault('properties', {})
if not props.get('url'):
props['url'] = [source_url]
source_obj = microformats2.json_to_object(entry)
logging.info('Converted to AS: %s', json.dumps(source_obj, indent=2))