Skip to content

Commit

Permalink
AP users: /r/: switch external user to indirect user
Browse files Browse the repository at this point in the history
for #512
  • Loading branch information
snarfed committed Jun 5, 2023
1 parent 9a75d10 commit 51c2773
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 35 deletions.
8 changes: 1 addition & 7 deletions activitypub.py
Expand Up @@ -328,7 +328,7 @@ def postprocess_as2(activity, target=None, wrap=True):
if not activity or isinstance(activity, str):
return activity

assert bool(g.user) ^ bool(g.external_user) # should have one but not both
assert g.user
type = activity.get('type')

# actor objects
Expand Down Expand Up @@ -396,14 +396,10 @@ def postprocess_as2(activity, target=None, wrap=True):
obj['id'] = util.get_first(obj, 'url') or target_id
elif g.user and g.user.is_web_url(id):
obj['id'] = g.user.ap_actor()
elif g.external_user:
obj['id'] = redirect_wrap(g.external_user)

# for Accepts
if g.user and g.user.is_web_url(obj.get('object')):
obj['object'] = g.user.ap_actor()
elif g.external_user and g.external_user == obj.get('object'):
obj['object'] = redirect_wrap(g.external_user)

# id is required for most things. default to url if it's not set.
if not activity.get('id'):
Expand Down Expand Up @@ -506,8 +502,6 @@ def postprocess_as2_actor(actor, wrap=True):
id = actor.get('id')
if g.user and (not id or g.user.is_web_url(id)):
actor['id'] = g.user.ap_actor()
elif g.external_user and (not id or id == g.external_user):
actor['id'] = redirect_wrap(g.external_user)

actor.update({
'url': urls if len(urls) > 1 else urls[0],
Expand Down
3 changes: 1 addition & 2 deletions flask_app.py
Expand Up @@ -39,9 +39,8 @@ def init_globals():
"""Set request globals.
* g.user: Current internal user we're operating on behalf of.
* g.external_user: Current external user we're operating on behalf of.
"""
g.user = g.external_user = None
g.user = None

# don't redirect API requests with blank path elements
app.url_map.merge_slashes = False
Expand Down
11 changes: 7 additions & 4 deletions redirect.py
Expand Up @@ -83,17 +83,20 @@ def redir(to):
logger.info(f'Found web user for domain {domain}')
break
else:
if accept_as2:
g.external_user = urllib.parse.urljoin(to, '/')
logging.info(f'No web user for {g.external_user}')
else:
if not accept_as2:
return f'No web user found for any of {domains}', 404

if accept_as2:
# AS2 requested, fetch and convert and serve
obj = Web.load(to, check_backlink=False)
if not obj or obj.deleted:
return f'Object not found: {to}', 404

g.user = Web.get_or_create(util.domain_from_link(to), direct=False)
if g.user.is_web_url(to):
g.user.actor_as2 = as2.from_as1(obj.as1)
g.user.put()

ret, _ = ActivityPub.serve(obj)
logger.info(f'Returning: {json_dumps(ret, indent=2)}')
return ret, {
Expand Down
19 changes: 10 additions & 9 deletions tests/test_activitypub.py
Expand Up @@ -65,6 +65,15 @@
'publicKeyPem': 'populated in setUp()',
},
}
ACTOR_BASE_FULL = {
**ACTOR_BASE,
'name': 'Ms. ☕ Baz',
'attachment': [{
'name': 'Web site',
'type': 'PropertyValue',
'value': '<a rel="me" href="https://user.com/">user.com</a>',
}],
}
REPLY_OBJECT = {
'@context': 'https://www.w3.org/ns/activitystreams',
'type': 'Note',
Expand Down Expand Up @@ -347,15 +356,7 @@ def test_actor_new_user_fetch(self, _, mock_get, __):

got = self.client.get('/user.com')
self.assertEqual(200, got.status_code)
self.assert_equals({
**ACTOR_BASE,
'name': 'Ms. ☕ Baz',
'attachment': [{
'name': 'Web site',
'type': 'PropertyValue',
'value': '<a rel="me" href="https://user.com/">user.com</a>',
}],
}, got.json, ignore=['publicKeyPem'])
self.assert_equals(ACTOR_BASE_FULL, got.json, ignore=['publicKeyPem'])

def test_actor_new_user_fetch_no_mf2(self, _, mock_get, __):
self.user.key.delete()
Expand Down
34 changes: 23 additions & 11 deletions tests/test_redirect.py
Expand Up @@ -13,6 +13,9 @@
from common import redirect_unwrap
from flask_app import app, cache
from models import Object, User
from web import Web

from .test_activitypub import ACTOR_BASE_FULL
from .test_web import (
ACTOR_AS2,
ACTOR_AS2_FULL,
Expand All @@ -27,9 +30,6 @@
}
del REPOST_AS2['cc']

EXTERNAL_REPOST_AS2 = copy.deepcopy(REPOST_AS2)
EXTERNAL_REPOST_AS2['actor']['id'] = 'http://localhost/r/https://user.com/'


class RedirectTest(testutil.TestCase):

Expand Down Expand Up @@ -71,14 +71,16 @@ def test_as2_ld(self):

def test_as2_creates_user(self):
with self.request_context:
Object(id='https://user.com/repost', as2=EXTERNAL_REPOST_AS2).put()
Object(id='https://user.com/repost', as2=REPOST_AS2).put()

self.user.key.delete()

resp = self.client.get('/r/https://user.com/repost',
headers={'Accept': as2.CONTENT_TYPE})
self.assertEqual(200, resp.status_code, resp.get_data(as_text=True))
self.assert_equals(EXTERNAL_REPOST_AS2, resp.json)
self.assert_equals(REPOST_AS2, resp.json)

self.assert_user(Web, 'user.com', direct=False)

@patch('requests.get')
def test_as2_fetch_post(self, mock_get):
Expand Down Expand Up @@ -107,16 +109,26 @@ def test_as2_no_user_fetch_homepage(self, mock_get):
resp = self.client.get('/r/https://user.com/',
headers={'Accept': as2.CONTENT_TYPE})
self.assertEqual(200, resp.status_code, resp.get_data(as_text=True))
self.assert_equals({
**ACTOR_AS2,
'id': 'http://localhost/r/https://user.com/',
'summary': '',

expected = copy.deepcopy(ACTOR_BASE_FULL)
del expected['endpoints']
del expected['followers']
del expected['following']
del expected['inbox']
del expected['outbox']
self.assert_equals(expected, resp.json, ignore=['publicKeyPem'])

self.assert_user(Web, 'user.com', direct=False, actor_as2={
'@context': 'https://www.w3.org/ns/activitystreams',
'type': 'Person',
'url': 'https://user.com/',
'name': 'Ms. ☕ Baz',
'attachment': [{
'name': 'Ms. ☕ Baz',
'type': 'PropertyValue',
'name': 'Ms. ☕ Baz',
'value': '<a rel="me" href="https://user.com/">user.com</a>',
}],
}, resp.json)
})

def test_accept_header_cache_key(self):
app.config['CACHE_TYPE'] = 'SimpleCache'
Expand Down
1 change: 0 additions & 1 deletion tests/test_web.py
Expand Up @@ -69,7 +69,6 @@
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
],
'preferredUsername': 'user.com',
'attachment': [{
'name': 'Web site',
'type': 'PropertyValue',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_webfinger.py
Expand Up @@ -207,7 +207,7 @@ def test_webfinger_missing_user(self):
self.assertEqual(404, got.status_code)

@patch('requests.get')
def test_webfinger_external_user_fetch_create_user(self, mock_get):
def test_webfinger_fetch_create_user(self, mock_get):
self.user.key.delete()
mock_get.return_value = requests_response(ACTOR_HTML)

Expand Down

0 comments on commit 51c2773

Please sign in to comment.