2017-08-13 07:12:16 +00:00
|
|
|
"""Handles requests for ActivityPub endpoints: actors, inbox, etc.
|
|
|
|
"""
|
2018-10-19 13:50:00 +00:00
|
|
|
import datetime
|
2017-08-13 07:12:16 +00:00
|
|
|
import logging
|
2021-07-10 15:07:40 +00:00
|
|
|
import re
|
2017-08-13 07:12:16 +00:00
|
|
|
|
2021-07-10 15:07:40 +00:00
|
|
|
from flask import request
|
2019-12-26 06:20:57 +00:00
|
|
|
from google.cloud import ndb
|
2022-11-21 20:14:37 +00:00
|
|
|
from google.cloud.ndb import OR
|
2022-11-19 02:49:34 +00:00
|
|
|
from granary import as2
|
2021-08-28 14:18:46 +00:00
|
|
|
from oauth_dropins.webutil import flask_util, util
|
2021-08-06 17:29:25 +00:00
|
|
|
from oauth_dropins.webutil.flask_util import error
|
2019-12-25 07:26:58 +00:00
|
|
|
from oauth_dropins.webutil.util import json_dumps, json_loads
|
2017-08-13 07:12:16 +00:00
|
|
|
|
2021-07-10 15:07:40 +00:00
|
|
|
from app import app, cache
|
2017-08-15 06:07:24 +00:00
|
|
|
import common
|
2022-12-26 21:34:50 +00:00
|
|
|
from common import CACHE_TIME, redirect_unwrap, redirect_wrap
|
2022-11-17 02:53:49 +00:00
|
|
|
from models import Activity, Follower, User
|
2017-08-15 06:07:24 +00:00
|
|
|
|
2022-02-12 06:38:56 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-10-17 05:21:13 +00:00
|
|
|
SUPPORTED_TYPES = (
|
2018-10-25 04:44:21 +00:00
|
|
|
'Accept',
|
2017-10-17 05:21:13 +00:00
|
|
|
'Announce',
|
|
|
|
'Article',
|
|
|
|
'Audio',
|
2018-10-15 15:09:36 +00:00
|
|
|
'Create',
|
2020-03-01 04:28:53 +00:00
|
|
|
'Delete',
|
2018-10-19 13:50:00 +00:00
|
|
|
'Follow',
|
2017-10-17 05:21:13 +00:00
|
|
|
'Image',
|
|
|
|
'Like',
|
|
|
|
'Note',
|
2019-08-01 14:32:45 +00:00
|
|
|
'Undo',
|
2017-10-17 05:21:13 +00:00
|
|
|
'Video',
|
|
|
|
)
|
2017-08-15 06:07:24 +00:00
|
|
|
|
2017-10-20 14:13:04 +00:00
|
|
|
|
2021-07-12 21:19:56 +00:00
|
|
|
@app.get(f'/<regex("{common.DOMAIN_RE}"):domain>')
|
2023-01-25 00:29:19 +00:00
|
|
|
@flask_util.cached(cache, CACHE_TIME, http_5xx=True)
|
2021-07-10 15:07:40 +00:00
|
|
|
def actor(domain):
|
2022-11-19 02:49:34 +00:00
|
|
|
"""Fetches a domain's h-card and converts to AS2 actor."""
|
2023-01-24 00:09:25 +00:00
|
|
|
_, _, actor, _ = common.actor(domain)
|
2022-11-19 02:49:34 +00:00
|
|
|
return (actor, {
|
2023-01-07 05:01:33 +00:00
|
|
|
'Content-Type': as2.CONTENT_TYPE,
|
2021-07-10 15:07:40 +00:00
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
})
|
|
|
|
|
2017-08-13 07:12:16 +00:00
|
|
|
|
2023-01-19 04:32:23 +00:00
|
|
|
@app.post('/inbox')
|
2021-07-12 21:19:56 +00:00
|
|
|
@app.post(f'/<regex("{common.DOMAIN_RE}"):domain>/inbox')
|
2022-11-17 15:36:01 +00:00
|
|
|
def inbox(domain=None):
|
2022-11-16 18:09:24 +00:00
|
|
|
"""Handles ActivityPub inbox delivery."""
|
2021-07-10 15:07:40 +00:00
|
|
|
body = request.get_data(as_text=True)
|
2022-02-12 06:38:56 +00:00
|
|
|
logger.info(f'Got: {body}')
|
2021-07-10 15:07:40 +00:00
|
|
|
|
|
|
|
# parse and validate AS2 activity
|
|
|
|
try:
|
|
|
|
activity = request.json
|
|
|
|
assert activity
|
|
|
|
except (TypeError, ValueError, AssertionError):
|
2022-11-24 16:20:04 +00:00
|
|
|
error(f"Couldn't parse body as JSON", exc_info=True)
|
2021-07-10 15:07:40 +00:00
|
|
|
|
2022-11-29 01:25:39 +00:00
|
|
|
type = activity.get('type')
|
|
|
|
actor = activity.get('actor')
|
|
|
|
actor_id = actor.get('id') if isinstance(actor, dict) else actor
|
|
|
|
logger.info(f'Got {type} activity from {actor_id}: {json_dumps(activity, indent=2)}')
|
2022-11-05 20:34:58 +00:00
|
|
|
|
2021-07-10 15:07:40 +00:00
|
|
|
obj = activity.get('object') or {}
|
|
|
|
if isinstance(obj, str):
|
|
|
|
obj = {'id': obj}
|
|
|
|
|
|
|
|
if type == 'Accept': # eg in response to a Follow
|
2021-12-30 03:34:46 +00:00
|
|
|
return '' # noop
|
2022-11-17 02:53:49 +00:00
|
|
|
if type not in SUPPORTED_TYPES:
|
2023-01-24 20:17:24 +00:00
|
|
|
error(f'Sorry, {type} activities are not supported yet.', status=501)
|
2021-07-10 15:07:40 +00:00
|
|
|
|
|
|
|
# TODO: verify signature if there is one
|
|
|
|
|
|
|
|
if type == 'Undo' and obj.get('type') == 'Follow':
|
|
|
|
# skip actor fetch below; we don't need it to undo a follow
|
|
|
|
undo_follow(redirect_unwrap(activity))
|
|
|
|
return ''
|
|
|
|
elif type == 'Delete':
|
2022-11-21 20:14:37 +00:00
|
|
|
# we currently only actually delete followers for Deletes that are sent
|
|
|
|
# to the shared inbox, not individual users' inboxes, to help scaling
|
|
|
|
# background: https://github.com/snarfed/bridgy-fed/issues/284
|
|
|
|
if domain:
|
|
|
|
logger.info('Skipping Delete sent to individual user inbox')
|
|
|
|
return 'OK'
|
|
|
|
|
|
|
|
id = obj.get('id') if isinstance(obj, dict) else obj
|
|
|
|
if not isinstance(id, str):
|
|
|
|
error("Couldn't find id of object to delete")
|
|
|
|
# assume this is an actor
|
|
|
|
# https://github.com/snarfed/bridgy-fed/issues/63
|
|
|
|
logger.info(f'Deactivating Followers with src or dest = {id}')
|
|
|
|
followers = Follower.query(OR(Follower.src == id,
|
|
|
|
Follower.dest == id)
|
|
|
|
).fetch()
|
|
|
|
for f in followers:
|
|
|
|
f.status = 'inactive'
|
|
|
|
ndb.put_multi(followers)
|
|
|
|
return 'OK'
|
2021-07-10 15:07:40 +00:00
|
|
|
|
2022-12-08 17:00:44 +00:00
|
|
|
user = User.get_or_create(domain) if domain else None
|
|
|
|
|
2021-07-10 15:07:40 +00:00
|
|
|
# fetch actor if necessary so we have name, profile photo, etc
|
2022-11-17 02:53:49 +00:00
|
|
|
if actor and isinstance(actor, str):
|
2022-11-24 16:20:04 +00:00
|
|
|
actor = activity['actor'] = common.get_as2(actor, user=user).json()
|
2021-07-10 15:07:40 +00:00
|
|
|
|
|
|
|
activity_unwrapped = redirect_unwrap(activity)
|
|
|
|
if type == 'Follow':
|
2022-11-24 16:20:04 +00:00
|
|
|
return accept_follow(activity, activity_unwrapped, user)
|
2021-07-10 15:07:40 +00:00
|
|
|
|
|
|
|
# send webmentions to each target
|
|
|
|
as1 = as2.to_as1(activity)
|
2022-11-17 02:53:49 +00:00
|
|
|
source_as2 = json_dumps(activity_unwrapped)
|
|
|
|
sent = common.send_webmentions(as1, proxy=True, protocol='activitypub',
|
|
|
|
source_as2=source_as2)
|
|
|
|
|
|
|
|
if not sent and type in ('Create', 'Announce'):
|
2022-08-24 00:37:50 +00:00
|
|
|
# check that this activity is public. only do this check for Creates,
|
|
|
|
# not Like, Follow, or other activity types, since Mastodon doesn't
|
|
|
|
# currently mark those as explicitly public.
|
|
|
|
if not as2.is_public(activity_unwrapped):
|
|
|
|
logging.info('Dropping non-public activity')
|
|
|
|
return ''
|
|
|
|
|
2022-11-17 02:53:49 +00:00
|
|
|
# normal post, deliver to BF followers
|
2022-12-01 05:04:22 +00:00
|
|
|
source = util.get_first(activity, 'url') or activity.get('id')
|
2022-11-17 02:53:49 +00:00
|
|
|
domains = []
|
|
|
|
if actor:
|
|
|
|
actor_id = actor.get('id')
|
|
|
|
if actor_id:
|
|
|
|
logging.info(f'Finding followers of {actor_id}')
|
|
|
|
domains = [f.src for f in
|
|
|
|
Follower.query(Follower.dest == actor_id,
|
|
|
|
projection=[Follower.src]).fetch()]
|
|
|
|
key = Activity(source=source, target='Public', direction='in',
|
|
|
|
protocol='activitypub', domain=domains, status='complete',
|
|
|
|
source_as2=source_as2).put()
|
|
|
|
logging.info(f'Wrote Activity {key} with {len(domains)} follower domains')
|
2021-07-10 15:07:40 +00:00
|
|
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
2022-11-24 16:20:04 +00:00
|
|
|
def accept_follow(follow, follow_unwrapped, user):
|
2021-07-10 15:07:40 +00:00
|
|
|
"""Replies to an AP Follow request with an Accept request.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
follow: dict, AP Follow activity
|
|
|
|
follow_unwrapped: dict, same, except with redirect URLs unwrapped
|
2022-11-24 16:20:04 +00:00
|
|
|
user: :class:`User`
|
2021-07-10 15:07:40 +00:00
|
|
|
"""
|
2022-02-12 06:38:56 +00:00
|
|
|
logger.info('Replying to Follow with Accept')
|
2021-07-10 15:07:40 +00:00
|
|
|
|
|
|
|
followee = follow.get('object')
|
|
|
|
followee_unwrapped = follow_unwrapped.get('object')
|
2023-01-12 20:28:34 +00:00
|
|
|
followee_id = (followee_unwrapped.get('id')
|
|
|
|
if isinstance(followee_unwrapped, dict) else followee_unwrapped)
|
2021-07-10 15:07:40 +00:00
|
|
|
follower = follow.get('actor')
|
2023-01-12 20:28:34 +00:00
|
|
|
if not followee or not followee_id or not follower:
|
2023-01-24 20:17:24 +00:00
|
|
|
error(f'Follow activity requires object and actor. Got: {follow}')
|
2021-07-10 15:07:40 +00:00
|
|
|
|
|
|
|
inbox = follower.get('inbox')
|
|
|
|
follower_id = follower.get('id')
|
|
|
|
if not inbox or not follower_id:
|
2023-01-24 20:17:24 +00:00
|
|
|
error(f'Follow actor requires id and inbox. Got: {follower}')
|
2021-07-10 15:07:40 +00:00
|
|
|
|
2023-01-12 20:28:34 +00:00
|
|
|
# rendered mf2 HTML proxy pages (in render.py) fall back to redirecting to
|
|
|
|
# the follow's AS2 id field, but Mastodon's ids are URLs that don't load in
|
|
|
|
# browsers, eg https://jawns.club/ac33c547-ca6b-4351-80d5-d11a6879a7b0
|
|
|
|
# so, set a synthetic URL based on the follower's profile.
|
|
|
|
# https://github.com/snarfed/bridgy-fed/issues/336
|
|
|
|
follower_url = util.get_url(follower) or follower_id
|
|
|
|
followee_url = util.get_url(followee_unwrapped) or followee_id
|
|
|
|
follow_unwrapped.setdefault('url', f'{follower_url}#followed-{followee_url}')
|
|
|
|
|
2021-07-10 15:07:40 +00:00
|
|
|
# store Follower
|
2023-01-12 20:28:34 +00:00
|
|
|
followee_domain = util.domain_from_link(followee_id, minimize=False)
|
2022-12-06 22:09:44 +00:00
|
|
|
# follow use_instead, if any
|
|
|
|
followee_domain = User.get_or_create(followee_domain).key.id()
|
|
|
|
follower = Follower.get_or_create(dest=followee_domain, src=follower_id,
|
2022-11-30 06:43:04 +00:00
|
|
|
last_follow=json_dumps(follow))
|
|
|
|
follower.status = 'active'
|
|
|
|
follower.put()
|
2021-07-10 15:07:40 +00:00
|
|
|
|
|
|
|
# send AP Accept
|
|
|
|
accept = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
2023-01-24 20:17:24 +00:00
|
|
|
'id': util.tag_uri(common.PRIMARY_DOMAIN,
|
|
|
|
f'accept/{followee_domain}/{follow.get("id")}'),
|
2021-07-10 15:07:40 +00:00
|
|
|
'type': 'Accept',
|
|
|
|
'actor': followee,
|
|
|
|
'object': {
|
|
|
|
'type': 'Follow',
|
|
|
|
'actor': follower_id,
|
|
|
|
'object': followee,
|
2018-10-19 13:50:00 +00:00
|
|
|
}
|
2021-07-10 15:07:40 +00:00
|
|
|
}
|
2022-11-24 16:20:04 +00:00
|
|
|
resp = common.signed_post(inbox, data=accept, user=user)
|
2021-07-10 15:07:40 +00:00
|
|
|
|
|
|
|
# send webmention
|
|
|
|
common.send_webmentions(as2.to_as1(follow), proxy=True, protocol='activitypub',
|
|
|
|
source_as2=json_dumps(follow_unwrapped))
|
|
|
|
|
|
|
|
return resp.text, resp.status_code
|
|
|
|
|
|
|
|
|
|
|
|
@ndb.transactional()
|
|
|
|
def undo_follow(undo_unwrapped):
|
2022-11-08 00:26:33 +00:00
|
|
|
"""Handles an AP Undo Follow request by deactivating the Follower entity.
|
2021-07-10 15:07:40 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
undo_unwrapped: dict, AP Undo activity with redirect URLs unwrapped
|
|
|
|
"""
|
2022-02-12 06:38:56 +00:00
|
|
|
logger.info('Undoing Follow')
|
2021-07-10 15:07:40 +00:00
|
|
|
|
|
|
|
follow = undo_unwrapped.get('object', {})
|
|
|
|
follower = follow.get('actor')
|
|
|
|
followee = follow.get('object')
|
2023-01-10 06:58:30 +00:00
|
|
|
if isinstance(followee, dict):
|
|
|
|
followee = followee.get('id') or util.get_url(followee)
|
2021-07-10 15:07:40 +00:00
|
|
|
if not follower or not followee:
|
2023-01-24 20:17:24 +00:00
|
|
|
error(f'Undo of Follow requires object with actor and object. Got: {follow}')
|
2021-07-10 15:07:40 +00:00
|
|
|
|
|
|
|
# deactivate Follower
|
2022-11-08 00:26:33 +00:00
|
|
|
user_domain = util.domain_from_link(followee, minimize=False)
|
2022-11-12 06:43:33 +00:00
|
|
|
follower_obj = Follower.get_by_id(Follower._id(dest=user_domain, src=follower))
|
2021-07-10 15:07:40 +00:00
|
|
|
if follower_obj:
|
2022-02-12 06:38:56 +00:00
|
|
|
logger.info(f'Marking {follower_obj.key} as inactive')
|
2021-07-10 15:07:40 +00:00
|
|
|
follower_obj.status = 'inactive'
|
|
|
|
follower_obj.put()
|
|
|
|
else:
|
2022-02-12 06:38:56 +00:00
|
|
|
logger.warning(f'No Follower found for {user_domain} {follower}')
|
2021-07-10 15:07:40 +00:00
|
|
|
|
|
|
|
# TODO send webmention with 410 of u-follow
|
2022-11-22 02:46:10 +00:00
|
|
|
|
|
|
|
|
2023-01-19 04:32:23 +00:00
|
|
|
@app.get(f'/<regex("{common.DOMAIN_RE}"):domain>/<any(followers,following):collection>')
|
2022-11-22 02:46:10 +00:00
|
|
|
@flask_util.cached(cache, CACHE_TIME)
|
2023-01-19 15:00:13 +00:00
|
|
|
def follower_collection(domain, collection):
|
2023-01-19 04:32:23 +00:00
|
|
|
"""ActivityPub Followers and Following collections.
|
2022-11-22 02:46:10 +00:00
|
|
|
|
|
|
|
https://www.w3.org/TR/activitypub/#followers
|
|
|
|
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
|
|
|
|
|
2023-01-20 15:02:55 +00:00
|
|
|
# page
|
|
|
|
followers, new_before, new_after = common.fetch_followers(domain, collection)
|
|
|
|
items = []
|
|
|
|
for f in followers:
|
|
|
|
f_as2 = f.to_as2()
|
|
|
|
if f_as2:
|
|
|
|
items.append(f_as2)
|
|
|
|
|
|
|
|
page = {
|
|
|
|
'type': 'CollectionPage',
|
|
|
|
'partOf': request.base_url,
|
|
|
|
'items': items,
|
|
|
|
}
|
|
|
|
if new_before:
|
|
|
|
page['next'] = f'{request.base_url}?before={new_before}'
|
|
|
|
if new_after:
|
|
|
|
page['prev'] = f'{request.base_url}?after={new_after}'
|
|
|
|
|
|
|
|
if 'before' in request.args or 'after' in request.args:
|
|
|
|
page.update({
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'id': request.url,
|
|
|
|
})
|
|
|
|
logger.info(f'Returning {json_dumps(page, indent=2)}')
|
|
|
|
return page, {'Content-Type': as2.CONTENT_TYPE}
|
|
|
|
|
|
|
|
# collection
|
2023-01-19 04:32:23 +00:00
|
|
|
domain_prop = Follower.dest if collection == 'followers' else Follower.src
|
2023-01-20 15:02:55 +00:00
|
|
|
count = Follower.query(
|
2022-11-22 02:46:10 +00:00
|
|
|
Follower.status == 'active',
|
2023-01-19 04:32:23 +00:00
|
|
|
domain_prop == domain,
|
2023-01-20 15:02:55 +00:00
|
|
|
).count()
|
|
|
|
|
|
|
|
collection = {
|
2022-11-22 02:46:10 +00:00
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
2023-01-20 15:02:55 +00:00
|
|
|
'id': request.base_url,
|
2022-11-22 02:46:10 +00:00
|
|
|
'type': 'Collection',
|
2023-01-20 15:02:55 +00:00
|
|
|
'summary': f"{domain}'s {collection}",
|
2022-11-22 02:46:10 +00:00
|
|
|
'totalItems': count,
|
2023-01-20 15:02:55 +00:00
|
|
|
'first': page,
|
2022-11-22 02:46:10 +00:00
|
|
|
}
|
2023-01-20 15:02:55 +00:00
|
|
|
logger.info(f'Returning {json_dumps(collection, indent=2)}')
|
|
|
|
return collection, {'Content-Type': as2.CONTENT_TYPE}
|