2017-10-10 14:42:10 +00:00
|
|
|
# coding=utf-8
|
2022-11-12 23:27:59 +00:00
|
|
|
"""Renders mf2 proxy pages based on stored Activity entities."""
|
2019-04-16 15:02:19 +00:00
|
|
|
import datetime
|
2017-10-10 14:42:10 +00:00
|
|
|
|
2021-07-07 15:07:20 +00:00
|
|
|
from flask import request
|
2017-10-15 17:50:01 +00:00
|
|
|
from granary import as2, atom, microformats2
|
2021-07-18 04:22:13 +00:00
|
|
|
from oauth_dropins.webutil import flask_util
|
2021-08-06 17:29:25 +00:00
|
|
|
from oauth_dropins.webutil.flask_util import error
|
2019-12-25 07:26:58 +00:00
|
|
|
from oauth_dropins.webutil.util import json_loads
|
2017-10-10 14:42:10 +00:00
|
|
|
|
2021-07-07 15:07:20 +00:00
|
|
|
from app import app, cache
|
2020-01-31 15:38:58 +00:00
|
|
|
import common
|
2022-11-12 23:27:59 +00:00
|
|
|
from models import Activity
|
2017-10-10 14:42:10 +00:00
|
|
|
|
2019-04-16 15:02:19 +00:00
|
|
|
CACHE_TIME = datetime.timedelta(minutes=15)
|
|
|
|
|
2021-07-07 03:33:48 +00:00
|
|
|
|
2021-07-11 23:30:14 +00:00
|
|
|
@app.get('/render')
|
2021-08-28 14:18:46 +00:00
|
|
|
@flask_util.cached(cache, CACHE_TIME)
|
2021-07-06 03:54:23 +00:00
|
|
|
def render():
|
2022-11-12 23:27:59 +00:00
|
|
|
"""Fetches a stored Activity and renders it as HTML."""
|
2021-07-18 04:22:13 +00:00
|
|
|
source = flask_util.get_required_param('source')
|
|
|
|
target = flask_util.get_required_param('target')
|
2017-10-10 14:42:10 +00:00
|
|
|
|
2021-07-06 03:54:23 +00:00
|
|
|
id = f'{source} {target}'
|
2022-11-12 23:27:59 +00:00
|
|
|
activity = Activity.get_by_id(id)
|
|
|
|
if not activity:
|
|
|
|
error(f'No stored activity for {id}', status=404)
|
|
|
|
|
|
|
|
if activity.source_mf2:
|
|
|
|
as1 = microformats2.json_to_object(json_loads(activity.source_mf2))
|
|
|
|
elif activity.source_as2:
|
|
|
|
as1 = as2.to_as1(json_loads(activity.source_as2))
|
|
|
|
elif activity.source_atom:
|
|
|
|
as1 = atom.atom_to_activity(activity.source_atom)
|
2021-07-06 03:54:23 +00:00
|
|
|
else:
|
2022-11-12 23:27:59 +00:00
|
|
|
error(f'Stored activity for {id} has no data', status=404)
|
2021-07-06 03:54:23 +00:00
|
|
|
|
|
|
|
# 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)
|