Merge branch 'dev' into 'master'

v0.2.2

See merge request pleroma/relay!37
This commit is contained in:
Izalia Mae 2022-08-26 15:21:56 +00:00
commit 729477820f
5 changed files with 27 additions and 10 deletions

View file

@ -1,4 +1,4 @@
__version__ = '0.2.1'
__version__ = '0.2.2'
from aiohttp.web import Application

View file

@ -1,6 +1,8 @@
import logging
import os
from pathlib import Path
## Add the verbose logging level
def verbose(message, *args, **kwargs):
@ -14,9 +16,15 @@ setattr(logging, 'VERBOSE', 15)
logging.addLevelName(15, 'VERBOSE')
## Get log level from environment if possible
## Get log level and file from environment if possible
env_log_level = os.environ.get('LOG_LEVEL', 'INFO').upper()
try:
env_log_file = Path(os.environ.get('LOG_FILE')).expanduser().resolve()
except TypeError:
env_log_file = None
## Make sure the level from the environment is valid
try:
@ -27,8 +35,13 @@ except AttributeError:
## Set logging config
handlers = [logging.StreamHandler()]
if env_log_file:
handlers.append(logging.FileHandler(env_log_file))
logging.basicConfig(
level = log_level,
format = "[%(asctime)s] %(levelname)s: %(message)s",
handlers = [logging.StreamHandler()]
handlers = handlers
)

View file

@ -58,7 +58,7 @@ def create_signature_header(headers):
def distill_object_id(activity):
logging.debug('>> determining object ID for', activity['object'])
logging.debug(f'>> determining object ID for {activity["object"]}')
try:
return activity['object']['id']
@ -136,7 +136,7 @@ async def fetch_actor_key(actor):
async def fetch_nodeinfo(domain):
nodeinfo_url = None
wk_nodeinfo = await request(f'https://{domain}/.well-known/nodeinfo', sign_headers=False)
wk_nodeinfo = await request(f'https://{domain}/.well-known/nodeinfo', sign_headers=False, activity=False)
if not wk_nodeinfo:
return
@ -149,7 +149,7 @@ async def fetch_nodeinfo(domain):
if not nodeinfo_url:
return
nodeinfo_data = await request(nodeinfo_url, sign_headers=False)
nodeinfo_data = await request(nodeinfo_url, sign_headers=False, activity=False)
try:
return nodeinfo_data['software']['name']
@ -211,7 +211,7 @@ async def unfollow_remote_actor(actor_uri):
await request(inbox, message)
async def request(uri, data=None, force=False, sign_headers=True):
async def request(uri, data=None, force=False, sign_headers=True, activity=True):
## If a get request and not force, try to use the cache first
if not data and not force:
try:
@ -223,14 +223,15 @@ async def request(uri, data=None, force=False, sign_headers=True):
url = urlparse(uri)
method = 'POST' if data else 'GET'
headers = {'User-Agent': 'ActivityRelay'}
mimetype = 'application/activity+json' if activity else 'application/json'
## Set the content type for a POST
if data and 'Content-Type' not in headers:
headers['Content-Type'] = 'application/activity+json'
headers['Content-Type'] = mimetype
## Set the accepted content type for a GET
elif not data and 'Accept' not in headers:
headers['Accept'] = 'application/activity+json'
headers['Accept'] = mimetype
if sign_headers:
signing_headers = {

View file

@ -118,5 +118,8 @@ processors = {
async def run_processor(request, data, actor):
if data['type'] not in processors:
return
logging.verbose(f'New activity from actor: {actor["id"]} {data["type"]}')
return await processors[data['type']](actor, data, request)

View file

@ -1,6 +1,6 @@
[metadata]
name = relay
version = 0.2.1
version = 0.2.2
description = Generic LitePub relay (works with all LitePub consumers and Mastodon)
long_description = file: README.md
long_description_content_type = text/markdown; charset=UTF-8