bridgy-fed/render.py

54 wiersze
1.7 KiB
Python
Czysty Zwykły widok Historia

# coding=utf-8
"""Renders mf2 proxy pages based on stored Responses."""
import datetime
2021-07-06 03:54:23 +00:00
from flask import Flask, request
from flask_caching import Cache
2017-10-15 17:50:01 +00:00
from granary import as2, atom, microformats2
from oauth_dropins.webutil import appengine_config, handlers
from oauth_dropins.webutil.util import json_loads
import common
from models import Response
CACHE_TIME = datetime.timedelta(minutes=15)
app = Flask('bridgy-fed')
app.config.from_mapping({'CACHE_TYPE': 'SimpleCache'})
app.wsgi_app = handlers.ndb_context_middleware(
app.wsgi_app, client=appengine_config.ndb_client)
cache = Cache(app)
2021-07-07 03:33:48 +00:00
def not_5xx(resp):
return isinstance(resp, tuple) and resp[1] // 100 != 5
2021-07-06 03:54:23 +00:00
@app.route('/render')
@cache.cached(timeout=CACHE_TIME.total_seconds(), query_string=True,
2021-07-07 03:33:48 +00:00
response_filter=not_5xx)
2021-07-06 03:54:23 +00:00
def render():
source = common.get_required_param(request, 'source')
target = common.get_required_param(request, 'target')
2021-07-06 03:54:23 +00:00
id = f'{source} {target}'
resp = Response.get_by_id(id)
2021-07-06 03:54:23 +00:00
if not resp:
return (f'No stored response for {id}', 404)
if resp.source_mf2:
as1 = microformats2.json_to_object(json_loads(resp.source_mf2))
elif resp.source_as2:
as1 = as2.to_as1(json_loads(resp.source_as2))
elif resp.source_atom:
as1 = atom.atom_to_activity(resp.source_atom)
else:
return (f'Stored response for {id} has no data', 404)
# add HTML meta redirect to source page. should trigger for end users in
# browsers but not for webmention receivers (hopefully).
html = microformats2.activities_to_html([as1])
utf8 = '<meta charset="utf-8">'
refresh = f'<meta http-equiv="refresh" content="0;url={source}">'
return html.replace(utf8, utf8 + '\n' + refresh)