AP: add HEAD support to followers/ing, outbox collections

for #383
as2-actor-ids
Ryan Barrett 2023-11-23 22:41:52 -08:00
rodzic 579f55d965
commit 33c5c3a4d2
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
2 zmienionych plików z 19 dodań i 2 usunięć

Wyświetl plik

@ -875,7 +875,8 @@ def inbox(protocol=None, id=None):
# source protocol in path; primarily for backcompat # source protocol in path; primarily for backcompat
@app.get(f'/ap/web/<regex("{DOMAIN_RE}"):id>/<any(followers,following):collection>') @app.get(f'/ap/web/<regex("{DOMAIN_RE}"):id>/<any(followers,following):collection>')
# special case Web users without /ap/web/ prefix, for backward compatibility # special case Web users without /ap/web/ prefix, for backward compatibility
@app.get(f'/<regex("{DOMAIN_RE}"):id>/<any(followers,following):collection>') @app.route(f'/<regex("{DOMAIN_RE}"):id>/<any(followers,following):collection>',
methods=['GET', 'HEAD'])
@flask_util.cached(cache, CACHE_TIME) @flask_util.cached(cache, CACHE_TIME)
def follower_collection(id, collection): def follower_collection(id, collection):
"""ActivityPub Followers and Following collections. """ActivityPub Followers and Following collections.
@ -892,6 +893,9 @@ def follower_collection(id, collection):
if not user: if not user:
return f'{protocol} user {id} not found', 404 return f'{protocol} user {id} not found', 404
if request.method == 'HEAD':
return '', {'Content-Type': as2.CONTENT_TYPE}
# page # page
followers, new_before, new_after = Follower.fetch_page(collection, user=user) followers, new_before, new_after = Follower.fetch_page(collection, user=user)
page = { page = {
@ -931,7 +935,7 @@ def follower_collection(id, collection):
# source protocol in path; primarily for backcompat # source protocol in path; primarily for backcompat
@app.get(f'/ap/web/<regex("{DOMAIN_RE}"):id>/outbox') @app.get(f'/ap/web/<regex("{DOMAIN_RE}"):id>/outbox')
# special case Web users without /ap/web/ prefix, for backward compatibility # special case Web users without /ap/web/ prefix, for backward compatibility
@app.get(f'/<regex("{DOMAIN_RE}"):id>/outbox') @app.route(f'/<regex("{DOMAIN_RE}"):id>/outbox', methods=['GET', 'HEAD'])
@flask_util.cached(cache, CACHE_TIME) @flask_util.cached(cache, CACHE_TIME)
def outbox(id): def outbox(id):
"""Serves a user's AP outbox. """Serves a user's AP outbox.
@ -946,6 +950,9 @@ def outbox(id):
if not g.user: if not g.user:
error(f'User {id} not found', status=404) error(f'User {id} not found', status=404)
if request.method == 'HEAD':
return '', {'Content-Type': as2.CONTENT_TYPE}
query = Object.query(Object.users == g.user.key) query = Object.query(Object.users == g.user.key)
objects, new_before, new_after = fetch_objects(query, by=Object.updated, objects, new_before, new_after = fetch_objects(query, by=Object.updated,
user=g.user) user=g.user)

Wyświetl plik

@ -1558,6 +1558,11 @@ class ActivityPubTest(TestCase):
'items': [ACTOR], 'items': [ACTOR],
}, resp.json) }, resp.json)
def test_following_collection_head(self, *_):
resp = self.client.head(f'/user.com/following')
self.assertEqual(200, resp.status_code)
self.assertEqual('', resp.get_data(as_text=True))
def test_outbox_fake_empty(self, *_): def test_outbox_fake_empty(self, *_):
self.make_user('fake:foo', cls=Fake) self.make_user('fake:foo', cls=Fake)
resp = self.client.get(f'/ap/fake:foo/outbox', resp = self.client.get(f'/ap/fake:foo/outbox',
@ -1641,6 +1646,11 @@ class ActivityPubTest(TestCase):
}, },
}, resp.json) }, resp.json)
def test_outbox_web_head(self, *_):
resp = self.client.head(f'/user.com/outbox')
self.assertEqual(200, resp.status_code)
self.assertEqual('', resp.get_data(as_text=True))
class ActivityPubUtilsTest(TestCase): class ActivityPubUtilsTest(TestCase):
def setUp(self): def setUp(self):