2017-11-09 18:17:19 -05:00
|
|
|
from django.conf import settings
|
2017-10-31 00:42:08 -04:00
|
|
|
from django.http import HttpResponse, JsonResponse
|
2017-10-26 22:24:57 -04:00
|
|
|
from django.urls import reverse
|
|
|
|
from lemoncurry.utils import load_package_json, origin
|
|
|
|
from urllib.parse import urljoin
|
2017-10-26 21:25:17 -04:00
|
|
|
from xrd import XRD, Attribute, Element, Link
|
|
|
|
|
|
|
|
|
2017-10-26 22:24:57 -04:00
|
|
|
def add_links(request, dest):
|
|
|
|
base = origin(request)
|
2017-10-31 00:42:08 -04:00
|
|
|
pkg = load_package_json()
|
2023-08-10 02:52:37 -04:00
|
|
|
webfinger = reverse("wellknowns:webfinger") + "?resource={uri}"
|
|
|
|
license = "https://creativecommons.org/licenses/by-sa/4.0/"
|
2017-10-31 00:42:08 -04:00
|
|
|
|
2017-10-26 21:25:17 -04:00
|
|
|
links = (
|
2017-10-31 00:42:08 -04:00
|
|
|
Link(
|
2023-08-10 02:52:37 -04:00
|
|
|
href=urljoin(base, reverse("entries:atom")),
|
|
|
|
rel="alternate",
|
|
|
|
type_="application/atom+xml",
|
2017-10-31 00:42:08 -04:00
|
|
|
),
|
2017-11-03 02:19:25 -04:00
|
|
|
Link(
|
2023-08-10 02:52:37 -04:00
|
|
|
href=urljoin(base, reverse("entries:rss")),
|
|
|
|
rel="alternate",
|
|
|
|
type_="application/rss+xml",
|
2017-11-09 18:17:19 -05:00
|
|
|
),
|
|
|
|
Link(
|
2023-08-10 02:52:37 -04:00
|
|
|
href=urljoin(base, reverse("lemonauth:indie")), rel="authorization_endpoint"
|
2017-11-03 02:19:25 -04:00
|
|
|
),
|
2023-08-10 02:52:37 -04:00
|
|
|
Link(href=pkg["repository"], type_="text/html", rel="code-repository"),
|
|
|
|
Link(href=settings.PUSH_HUB, rel="hub"),
|
|
|
|
Link(href=license, type_="text/html", rel="license"),
|
|
|
|
Link(href=license + "rdf", type_="application/rdf+xml", rel="license"),
|
2017-10-31 00:42:08 -04:00
|
|
|
Link(
|
|
|
|
template=urljoin(base, webfinger),
|
2023-08-10 02:52:37 -04:00
|
|
|
type_="application/json",
|
|
|
|
rel="lrdd",
|
2017-10-31 00:42:08 -04:00
|
|
|
),
|
|
|
|
Link(
|
2023-08-10 02:52:37 -04:00
|
|
|
href=urljoin(base, reverse("wellknowns:manifest")),
|
|
|
|
rel="manifest",
|
|
|
|
type_="application/json",
|
2017-10-31 00:42:08 -04:00
|
|
|
),
|
2023-08-10 02:52:37 -04:00
|
|
|
Link(href=urljoin(base, reverse("micropub:micropub")), rel="micropub"),
|
|
|
|
Link(href=urljoin(base, reverse("lemonauth:token")), rel="token_endpoint"),
|
|
|
|
Link(href="https://openid.indieauth.com/openid", rel="openid.server"),
|
|
|
|
Link(href=base, rel="openid.delegate"),
|
2017-10-26 21:25:17 -04:00
|
|
|
)
|
|
|
|
dest.extend(links)
|
|
|
|
|
|
|
|
|
|
|
|
def host_meta(request):
|
2023-08-10 02:52:37 -04:00
|
|
|
h = XRD(subject="https://" + request.site.domain)
|
2017-10-26 22:24:57 -04:00
|
|
|
add_links(request, h.links)
|
2017-10-26 21:25:17 -04:00
|
|
|
return h
|
|
|
|
|
|
|
|
|
|
|
|
def host_meta_xml(request):
|
|
|
|
return HttpResponse(
|
2023-08-10 02:52:37 -04:00
|
|
|
host_meta(request).to_xml().toprettyxml(indent=" ", encoding="utf-8"),
|
|
|
|
content_type="application/xrd+xml",
|
2017-10-26 21:25:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-10-31 00:42:08 -04:00
|
|
|
# The XRD package doesn't actually generate correct JSON, so we have to do it
|
|
|
|
# ourselves instead.
|
2017-10-26 21:25:17 -04:00
|
|
|
def host_meta_json(request):
|
2017-10-31 00:42:08 -04:00
|
|
|
meta = host_meta(request)
|
|
|
|
links = []
|
|
|
|
for l in meta.links:
|
|
|
|
link = {
|
2023-08-10 02:52:37 -04:00
|
|
|
"rel": l.rel,
|
|
|
|
"type": l.type,
|
|
|
|
"href": l.href,
|
|
|
|
"template": l.template,
|
2017-10-31 00:42:08 -04:00
|
|
|
}
|
|
|
|
for k in list(link.keys()):
|
|
|
|
if not link[k]:
|
|
|
|
del link[k]
|
|
|
|
links.append(link)
|
|
|
|
|
2023-08-10 02:52:37 -04:00
|
|
|
meta = {"links": links, "subject": meta.subject}
|
2017-10-31 00:42:08 -04:00
|
|
|
return JsonResponse(meta)
|