Oops add missing code

This commit is contained in:
Thomas Sileo 2018-07-21 00:15:47 +02:00
parent a3cb459ae0
commit ad355df6c5
2 changed files with 72 additions and 0 deletions

40
templates/lookup.html Normal file
View file

@ -0,0 +1,40 @@
{% extends "layout.html" %}
{% import 'utils.html' as utils %}
{% block title %}Lookup - {{ config.NAME }}{% endblock %}
{% block content %}
<div id="container">
{% include "header.html" %}
<div style="margin-top:50px;">
<form id="lookup-form" method="POST">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<input type="text" name="url" value="{{url or '' }}" placeholder="https://url-of-a-user-or-a-note.tld">
<input type="submit" value="Lookup">
</form>
{% if data %}
{% set data = data.to_dict() %}
<div id="lookup-result" style="margin-top:30px;">
{% if data | has_type('Person') or data | has_type('Service') %}
<div style="margin-left:95px;padding-bottom:5px;margin-bottom:15px;">
<form action="/api/follow" class="action-form" method="POST">
<input type="hidden" name="redirect" value="{{ request.path }}"/>
<input type="hidden" name="actor" value="{{ data.id }}"/>
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<button type="submit" class="bar-item">follow</button>
</form>
</div>
{{ 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 %}
</div>
{% endif %}
</div>
</div>
{% endblock %}

32
utils/lookup.py Normal file
View file

@ -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)