From 84606500fa97ba8148db183e0f6490ddb30be610 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Wed, 18 Jan 2023 20:32:23 -0800 Subject: [PATCH] refactoring: unify followers and following AP collection endpoints --- activitypub.py | 45 ++++++++------------------------------------- common.py | 4 ++-- 2 files changed, 10 insertions(+), 39 deletions(-) diff --git a/activitypub.py b/activitypub.py index df6b185..e8c0eca 100644 --- a/activitypub.py +++ b/activitypub.py @@ -46,7 +46,7 @@ def actor(domain): }) -@app.post(f'/inbox') +@app.post('/inbox') @app.post(f'//inbox') def inbox(domain=None): """Handles ActivityPub inbox delivery.""" @@ -238,11 +238,10 @@ def undo_follow(undo_unwrapped): # TODO send webmention with 410 of u-follow -# TODO: unify with following_collection -@app.get(f'//followers') +@app.get(f'//') @flask_util.cached(cache, CACHE_TIME) -def followers_collection(domain): - """ActivityPub Followers collection. +def followers_collection(domain, collection): + """ActivityPub Followers and Following collections. https://www.w3.org/TR/activitypub/#followers https://www.w3.org/TR/activitypub/#collections @@ -251,44 +250,16 @@ def followers_collection(domain): if not User.get_by_id(domain): return f'User {domain} not found', 404 - logger.info(f"Counting {domain}'s followers") + logger.info(f"Counting {domain}'s {collection}") + domain_prop = Follower.dest if collection == 'followers' else Follower.src count = Follower.query( Follower.status == 'active', - Follower.dest == domain, + domain_prop == domain, ).count() ret = { '@context': 'https://www.w3.org/ns/activitystreams', - 'summary': f"{domain}'s followers", - 'type': 'Collection', - 'totalItems': count, - 'items': [], # TODO - } - logger.info(f'Returning {json_dumps(ret, indent=2)}') - return ret - - -@app.get(f'//following') -@flask_util.cached(cache, CACHE_TIME) -def following_collection(domain): - """ActivityPub Following collection. - - https://www.w3.org/TR/activitypub/#following - https://www.w3.org/TR/activitypub/#collections - https://www.w3.org/TR/activitystreams-core/#paging - """ - if not User.get_by_id(domain): - return f'User {domain} not found', 404 - - logger.info(f"Counting {domain}'s following") - count = Follower.query( - Follower.status == 'active', - Follower.src == domain, - ).count() - - ret = { - '@context': 'https://www.w3.org/ns/activitystreams', - 'summary': f"{domain}'s following", + 'summary': f"{domain}'s {collection}", 'type': 'Collection', 'totalItems': count, 'items': [], # TODO diff --git a/common.py b/common.py index 85edbd0..3112a25 100644 --- a/common.py +++ b/common.py @@ -26,8 +26,8 @@ from models import Activity, User logger = logging.getLogger(__name__) -DOMAIN_RE = r'([^/:]+\.[^/:]+)' -ACCT_RE = r'(?:acct:)?([^@]+)@' + DOMAIN_RE +DOMAIN_RE = r'[^/:]+\.[^/:]+' +ACCT_RE = f'(?:acct:)?([^@]+)@({DOMAIN_RE})' TLD_BLOCKLIST = ('7z', 'asp', 'aspx', 'gif', 'html', 'ico', 'jpg', 'jpeg', 'js', 'json', 'php', 'png', 'rar', 'txt', 'yaml', 'yml', 'zip') XML_UTF8 = "\n"