microblog.pub/blueprints/well_known.py

96 lines
2.8 KiB
Python
Raw Permalink Normal View History

2019-08-01 12:55:30 -05:00
import mimetypes
2019-08-01 13:01:40 -05:00
from typing import Any
2019-08-01 12:55:30 -05:00
import flask
from flask import abort
from flask import request
from little_boxes import activitypub as ap
import config
from config import DB
2019-08-01 15:25:58 -05:00
from core.meta import Box
2019-08-24 03:58:35 -05:00
from core.shared import jsonify
2019-08-01 12:55:30 -05:00
blueprint = flask.Blueprint("well_known", __name__)
@blueprint.route("/.well-known/webfinger")
2019-08-01 13:01:40 -05:00
def wellknown_webfinger() -> Any:
2019-08-01 12:55:30 -05:00
"""Exposes/servers WebFinger data."""
resource = request.args.get("resource")
if resource not in [f"acct:{config.USERNAME}@{config.DOMAIN}", config.ID]:
abort(404)
out = {
"subject": f"acct:{config.USERNAME}@{config.DOMAIN}",
"aliases": [config.ID],
"links": [
{
"rel": "http://webfinger.net/rel/profile-page",
"type": "text/html",
"href": config.ID,
},
{"rel": "self", "type": "application/activity+json", "href": config.ID},
{
"rel": "http://ostatus.org/schema/1.0/subscribe",
"template": config.BASE_URL + "/authorize_follow?profile={uri}",
},
{"rel": "magic-public-key", "href": config.KEY.to_magic_key()},
{
"href": config.ICON_URL,
"rel": "http://webfinger.net/rel/avatar",
"type": mimetypes.guess_type(config.ICON_URL)[0],
},
],
}
2019-08-24 03:58:35 -05:00
return jsonify(out, "application/jrd+json; charset=utf-8")
2019-08-01 12:55:30 -05:00
@blueprint.route("/.well-known/nodeinfo")
2019-08-01 13:01:40 -05:00
def wellknown_nodeinfo() -> Any:
2019-08-01 12:55:30 -05:00
"""Exposes the NodeInfo endpoint (http://nodeinfo.diaspora.software/)."""
2019-08-24 03:58:35 -05:00
return jsonify(
{
"links": [
{
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.1",
"href": f"{config.ID}/nodeinfo",
}
]
}
2019-08-01 12:55:30 -05:00
)
@blueprint.route("/nodeinfo")
2019-08-01 13:01:40 -05:00
def nodeinfo() -> Any:
2019-08-01 12:55:30 -05:00
"""NodeInfo endpoint."""
q = {
"box": Box.OUTBOX.value,
"meta.deleted": False,
"type": {"$in": [ap.ActivityType.CREATE.value, ap.ActivityType.ANNOUNCE.value]},
}
2019-08-24 03:58:35 -05:00
out = {
"version": "2.1",
"software": {
"name": "microblogpub",
"version": config.VERSION,
"repository": "https://github.com/tsileo/microblog.pub",
2019-08-01 12:55:30 -05:00
},
2019-08-24 03:58:35 -05:00
"protocols": ["activitypub"],
"services": {"inbound": [], "outbound": []},
"openRegistrations": False,
"usage": {"users": {"total": 1}, "localPosts": DB.activities.count(q)},
"metadata": {
"nodeName": f"@{config.USERNAME}@{config.DOMAIN}",
"version": config.VERSION,
"versionDate": config.VERSION_DATE,
},
}
return jsonify(
out,
"application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.1#",
2019-08-01 12:55:30 -05:00
)