From d5b9053f71affcc2a6ac4644c28b8bcb6c6e1859 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Fri, 2 Dec 2022 00:50:57 -0500 Subject: [PATCH] replace various classes with aputils classes --- relay/http_client.py | 9 ++++--- relay/misc.py | 29 --------------------- relay/views.py | 60 +++++++++++++++----------------------------- 3 files changed, 26 insertions(+), 72 deletions(-) diff --git a/relay/http_client.py b/relay/http_client.py index 3b8a182..e164430 100644 --- a/relay/http_client.py +++ b/relay/http_client.py @@ -1,9 +1,9 @@ -import aputils import logging import traceback from aiohttp import ClientSession, ClientTimeout, TCPConnector from aiohttp.client_exceptions import ClientConnectorError, ServerTimeoutError +from aputils import Nodeinfo, WellKnownNodeinfo from datetime import datetime from cachetools import LRUCache from json.decoder import JSONDecodeError @@ -173,7 +173,10 @@ class HttpClient: ## Additional methods ## async def fetch_nodeinfo(self, domain): nodeinfo_url = None - wk_nodeinfo = await self.get(f'https://{domain}/.well-known/nodeinfo', loads=WKNodeinfo) + wk_nodeinfo = await self.get( + f'https://{domain}/.well-known/nodeinfo', + loads = WellKnownNodeinfo.new_from_json + ) for version in ['20', '21']: try: @@ -186,4 +189,4 @@ class HttpClient: logging.verbose(f'Failed to fetch nodeinfo url for domain: {domain}') return False - return await request(nodeinfo_url, loads=Nodeinfo) or False + return await request(nodeinfo_url, loads=Nodeinfo.new_from_json) or False diff --git a/relay/misc.py b/relay/misc.py index c155cfe..68b6a18 100644 --- a/relay/misc.py +++ b/relay/misc.py @@ -37,10 +37,6 @@ def set_app(new_app): app = new_app -def build_signing_string(headers, used_headers): - return '\n'.join(map(lambda x: ': '.join([x.lower(), headers[x]]), used_headers)) - - def boolean(value): if isinstance(value, str): if value.lower() in ['on', 'y', 'yes', 'true', 'enable', 'enabled', '1']: @@ -279,12 +275,6 @@ class Message(DotDict): return aputils.Signer.new_from_actor(self) -class Nodeinfo(DotDict): - @property - def swname(self): - return self.software.name - - class Response(AiohttpResponse): @classmethod def new(cls, body='', status=200, headers=None, ctype='text'): @@ -350,22 +340,3 @@ class View(AiohttpView): @property def database(self): return self.app.database - - -class WKNodeinfo(DotDict): - @classmethod - def new(cls, v20, v21): - return cls({ - 'links': [ - {'rel': NODEINFO_NS['20'], 'href': v20}, - {'rel': NODEINFO_NS['21'], 'href': v21} - ] - }) - - - def get_url(self, version='20'): - for item in self.links: - if item['rel'] == NODEINFO_NS[version]: - return item['href'] - - raise KeyError(version) diff --git a/relay/views.py b/relay/views.py index 6b6b43f..d658468 100644 --- a/relay/views.py +++ b/relay/views.py @@ -8,7 +8,7 @@ from pathlib import Path from . import __version__, misc from .http_debug import STATS -from .misc import DotDict, Message, Response, WKNodeinfo +from .misc import DotDict, Message, Response from .processors import run_processor @@ -158,57 +158,37 @@ async def webfinger(request): if subject != f'acct:relay@{request.config.host}': return Response.new_error(404, 'user not found', 'json') - data = { - 'subject': subject, - 'aliases': [request.config.actor], - 'links': [ - {'href': request.config.actor, 'rel': 'self', 'type': 'application/activity+json'}, - {'href': request.config.actor, 'rel': 'self', 'type': 'application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"'} - ] - } + data = aputils.Webfinger.new( + handle = 'relay', + domain = request.config.host, + actor = request.config.actor + ) return Response.new(data, ctype='json') @register_route('GET', '/nodeinfo/{version:\d.\d\.json}') -async def nodeinfo_2_0(request): +async def nodeinfo(request): niversion = request.match_info['version'][:3] - data = { - 'openRegistrations': not request.config.whitelist_enabled, - 'protocols': ['activitypub'], - 'services': { - 'inbound': [], - 'outbound': [] - }, - 'software': { - 'name': 'activityrelay', - 'version': version - }, - 'usage': { - 'localPosts': 0, - 'users': { - 'total': 1 - } - }, - 'metadata': { - 'peers': request.database.hostnames - }, - 'version': niversion - } - if version == '2.1': - data['software']['repository'] = 'https://git.pleroma.social/pleroma/relay' + data = dict( + name = 'activityrelay', + version = version, + protocols = ['activitypub'], + open_regs = not request.config.whitelist_enabled, + users = 1, + metadata = {'peers': request.database.hostnames} + ) - return Response.new(data, ctype='json') + if niversion == '2.1': + data['repo'] = 'https://git.pleroma.social/pleroma/relay' + + return Response.new(aputils.Nodeinfo.new(**data), ctype='json') @register_route('GET', '/.well-known/nodeinfo') async def nodeinfo_wellknown(request): - data = WKNodeinfo.new( - v20 = f'https://{request.config.host}/nodeinfo/2.0.json', - v21 = f'https://{request.config.host}/nodeinfo/2.1.json' - ) - + data = aputils.WellKnownNodeinfo.new_template(request.config.host) return Response.new(data, ctype='json')