From ad355df6c599911414219c924a64b1763859f14d Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sat, 21 Jul 2018 00:15:47 +0200 Subject: [PATCH] Oops add missing code --- templates/lookup.html | 40 ++++++++++++++++++++++++++++++++++++++++ utils/lookup.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 templates/lookup.html create mode 100644 utils/lookup.py diff --git a/templates/lookup.html b/templates/lookup.html new file mode 100644 index 0000000..35af1b8 --- /dev/null +++ b/templates/lookup.html @@ -0,0 +1,40 @@ +{% extends "layout.html" %} +{% import 'utils.html' as utils %} +{% block title %}Lookup - {{ config.NAME }}{% endblock %} +{% block content %} +
+{% include "header.html" %} +
+ +
+ + + +
+ +{% if data %} +{% set data = data.to_dict() %} +
+ {% if data | has_type('Person') or data | has_type('Service') %} +
+
+ + + + +
+
+ + + {{ utils.display_actor_inline(data, size=80) }} + {% elif data | has_type('Create') %} + {{ utils.display_note(data.object, ui=True) }} + {% elif data | has_type('Note') %} + {{ utils.display_note(data, ui=True) }} + {% endif %} +
+{% endif %} + +
+
+{% endblock %} diff --git a/utils/lookup.py b/utils/lookup.py new file mode 100644 index 0000000..65c2c19 --- /dev/null +++ b/utils/lookup.py @@ -0,0 +1,32 @@ +import little_boxes.activitypub as ap +import json +import requests + +import mf2py + + +def lookup(url: str) -> ap.BaseActivity: + """Try to find an AP object related to the given URL.""" + backend = ap.get_backend() + resp = requests.get( + url, + timeout=15, + allow_redirects=False, + headers={"User-Agent": backend.user_agent()}, + ) + resp.raise_for_status() + + # If the page is HTML, maybe it contains an alternate link pointing to an AP object + for alternate in mf2py.parse(resp.text).get("alternates", []): + if alternate.get("type") == "application/activity+json": + return ap.fetch_remote_activity(alternate["url"]) + + try: + # Maybe the page was JSON-LD? + data = resp.json() + return ap.parse_activity(data) + except json.JSONDecodeError: + pass + + # Try content negotiation (retry with the AP Accept header) + return ap.fetch_remote_activity(url)