noop: move Protocol.subdomain_url to common.subdomain_wrap

pull/696/head
Ryan Barrett 2023-10-23 15:44:32 -07:00
rodzic 9a87d7261f
commit d12fd99b03
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
8 zmienionych plików z 34 dodań i 28 usunięć

Wyświetl plik

@ -29,6 +29,7 @@ from common import (
host_url,
redirect_unwrap,
redirect_wrap,
subdomain_wrap,
)
from models import Follower, Object, PROTOCOLS, User
from protocol import Protocol
@ -781,7 +782,7 @@ def actor(handle_or_id):
'following': g.user.ap_actor('following'),
'followers': g.user.ap_actor('followers'),
'endpoints': {
'sharedInbox': cls.subdomain_url('/ap/sharedInbox'),
'sharedInbox': subdomain_wrap(cls, '/ap/sharedInbox'),
},
# add this if we ever change the Web actor ids to be /web/[id]
# 'alsoKnownAs': [host_url(id)],

Wyświetl plik

@ -5,6 +5,7 @@ import logging
import re
import threading
import urllib.parse
from urllib.parse import urljoin
import cachetools
from Crypto.Util import number
@ -97,7 +98,7 @@ def host_url(path_query=None):
base = f'https://{PRIMARY_DOMAIN}'
assert base
return urllib.parse.urljoin(base, path_query)
return urljoin(base, path_query)
def error(msg, status=400, exc_info=None, **kwargs):
@ -156,6 +157,21 @@ def redirect_wrap(url):
return host_url('/r/') + url
def subdomain_wrap(proto, path=None):
"""Returns the URL for a given path on this protocol's subdomain.
Eg for the path ``foo/bar`` on ActivityPub, returns
``https://ap.brid.gy/foo/bar``.
Args:
proto (subclass of :class:`protocol.Protocol`)
Returns:
str: URL
"""
return urljoin(f'https://{proto.ABBREV or "fed"}{SUPERDOMAIN}/', path)
def redirect_unwrap(val):
"""Removes our redirect wrapping from a URL, if it's there.

Wyświetl plik

@ -13,7 +13,7 @@ from oauth_dropins.webutil import flask_util, util
from oauth_dropins.webutil.flask_util import error
from activitypub import ActivityPub
from common import CACHE_TIME, LOCAL_DOMAINS, SUPERDOMAIN
from common import CACHE_TIME, LOCAL_DOMAINS, subdomain_wrap, SUPERDOMAIN
from flask_app import app, cache
from models import Object, PROTOCOLS
from protocol import Protocol
@ -109,7 +109,7 @@ def convert(dest, _, src=None):
def render_redirect():
"""Redirect from old /render?id=... endpoint to /convert/..."""
id = flask_util.get_required_param('id')
return redirect(ActivityPub.subdomain_url(f'/convert/web/{id}'), code=301)
return redirect(subdomain_wrap(ActivityPub, f'/convert/web/{id}'), code=301)
@app.get(f'/convert/<any({",".join(SOURCES)}):src>/<any({",".join(DESTS)}):dest>/<path:_>')
@ -130,4 +130,4 @@ def convert_source_path_redirect(src, dest, _):
request.url = request.url.replace(f'/{src}/', '/')
return convert(dest, None, src)
return redirect(PROTOCOLS[src].subdomain_url(new_path), code=301)
return redirect(subdomain_wrap(PROTOCOLS[src], new_path), code=301)

Wyświetl plik

@ -465,7 +465,7 @@ class User(StringIdModel, metaclass=ProtocolUserMeta):
str
"""
# must match the URL route for activitypub.actor()
url = self.subdomain_url(f'/ap/{self.key.id()}')
url = common.subdomain_wrap(self, f'/ap/{self.key.id()}')
if rest:
url += f'/{rest.lstrip("/")}'
return url
@ -874,7 +874,7 @@ class Object(StringIdModel):
if not self.source_protocol:
logger.warning(f'!!! No source_protocol for {id} !!!')
protocol = PROTOCOLS.get(self.source_protocol) or Protocol
return protocol.subdomain_url(f'convert/web/{id}')
return common.subdomain_wrap(protocol, f'convert/web/{id}')
def actor_link(self, image=True, sized=False):
"""Returns a pretty HTML link with the actor's name and picture.

Wyświetl plik

@ -114,21 +114,6 @@ class Protocol:
label = domain.removesuffix(common.SUPERDOMAIN)
return PROTOCOLS.get(label)
@classmethod
def subdomain_url(cls, path=None):
"""Returns the URL for a given path on this protocol's subdomain.
Eg for the path ``foo/bar`` on ActivityPub, returns
``https://ap.brid.gy/foo/bar``.
Args:
path (str)
Returns:
str: URL
"""
return urljoin(f'https://{cls.ABBREV or "fed"}{common.SUPERDOMAIN}/', path)
@classmethod
def owns_id(cls, id):
"""Returns whether this protocol owns the id, or None if it's unclear.

Wyświetl plik

@ -6,6 +6,7 @@ from .testutil import Fake, TestCase
import common
from flask_app import app
from ui import UIProtocol
from web import Web
@ -53,6 +54,14 @@ class CommonTest(TestCase):
for obj in '', {}, []:
self.assertEqual(obj, common.redirect_unwrap(obj))
def test_subdomain_wrap(self):
self.assertEqual('https://fa.brid.gy/',
common.subdomain_wrap(Fake))
self.assertEqual('https://fa.brid.gy/foo?bar',
common.subdomain_wrap(Fake, 'foo?bar'))
self.assertEqual('https://fed.brid.gy/',
common.subdomain_wrap(UIProtocol))
def test_unwrap_protocol_subdomain(self):
self.assert_equals({
'type': 'Like',

Wyświetl plik

@ -81,11 +81,6 @@ class ProtocolTest(TestCase):
with app.test_request_context('/foo', base_url=url):
self.assertEqual(expected, Protocol.for_request(fed=Fake))
def test_subdomain_url(self):
self.assertEqual('https://fa.brid.gy/', Fake.subdomain_url())
self.assertEqual('https://fa.brid.gy/foo?bar', Fake.subdomain_url('foo?bar'))
self.assertEqual('https://fed.brid.gy/', UIProtocol.subdomain_url())
def test_for_id(self):
for id, expected in [
(None, None),

Wyświetl plik

@ -137,7 +137,7 @@ class Webfinger(flask_util.XrdOrJrd):
# https://www.w3.org/TR/activitypub/#sharedInbox
'rel': 'sharedInbox',
'type': as2.CONTENT_TYPE,
'href': cls.subdomain_url('/ap/sharedInbox'),
'href': common.subdomain_wrap(cls, '/ap/sharedInbox'),
},
# remote follow