2017-10-10 14:42:10 +00:00
|
|
|
# coding=utf-8
|
|
|
|
"""Renders mf2 proxy pages based on stored Responses."""
|
2019-04-16 15:02:19 +00:00
|
|
|
import datetime
|
2017-10-10 14:42:10 +00:00
|
|
|
|
2017-10-15 17:50:01 +00:00
|
|
|
from granary import as2, atom, microformats2
|
2020-01-31 15:38:58 +00:00
|
|
|
from oauth_dropins.webutil.handlers import cache_response
|
2017-10-10 14:42:10 +00:00
|
|
|
from oauth_dropins.webutil import util
|
2019-12-25 07:26:58 +00:00
|
|
|
from oauth_dropins.webutil.util import json_loads
|
2017-10-10 14:42:10 +00:00
|
|
|
import webapp2
|
|
|
|
|
2020-01-31 15:38:58 +00:00
|
|
|
import common
|
2017-10-10 14:42:10 +00:00
|
|
|
from models import Response
|
|
|
|
|
2019-04-16 15:02:19 +00:00
|
|
|
CACHE_TIME = datetime.timedelta(minutes=15)
|
|
|
|
|
2017-10-10 14:42:10 +00:00
|
|
|
|
2020-01-31 15:38:58 +00:00
|
|
|
class RenderHandler(common.Handler):
|
2017-10-10 14:42:10 +00:00
|
|
|
"""Fetches a stored Response and renders it as HTML."""
|
|
|
|
|
2019-11-13 05:28:44 +00:00
|
|
|
@cache_response(CACHE_TIME)
|
2017-10-10 14:42:10 +00:00
|
|
|
def get(self):
|
|
|
|
source = util.get_required_param(self, 'source')
|
|
|
|
target = util.get_required_param(self, 'target')
|
|
|
|
|
|
|
|
id = '%s %s' % (source, target)
|
|
|
|
resp = Response.get_by_id(id)
|
|
|
|
if not resp:
|
|
|
|
self.abort(404, 'No stored response for %s' % id)
|
|
|
|
|
|
|
|
if resp.source_mf2:
|
2019-12-25 07:26:58 +00:00
|
|
|
as1 = microformats2.json_to_object(json_loads(resp.source_mf2))
|
2017-10-10 14:42:10 +00:00
|
|
|
elif resp.source_as2:
|
2019-12-25 07:26:58 +00:00
|
|
|
as1 = as2.to_as1(json_loads(resp.source_as2))
|
2017-10-10 14:42:10 +00:00
|
|
|
elif resp.source_atom:
|
2017-10-15 17:50:01 +00:00
|
|
|
as1 = atom.atom_to_activity(resp.source_atom)
|
2017-10-10 14:42:10 +00:00
|
|
|
else:
|
|
|
|
self.abort(404, 'Stored response for %s has no data' % id)
|
|
|
|
|
2019-04-17 15:44:26 +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 = '<meta http-equiv="refresh" content="0;url=%s">' % source
|
|
|
|
html = html.replace(utf8, utf8 + '\n' + refresh)
|
|
|
|
|
|
|
|
self.response.write(html)
|
2017-10-10 14:42:10 +00:00
|
|
|
|
|
|
|
|
2019-12-26 06:20:57 +00:00
|
|
|
ROUTES = [
|
|
|
|
('/render', RenderHandler),
|
|
|
|
]
|