COSCUP-ap-demo/app.py

123 lines
3 KiB
Python
Raw Normal View History

2023-06-07 08:58:53 +02:00
#!/usr/bin/env python3
2023-06-08 10:54:36 +02:00
"""Creatr App"""
2023-07-27 09:16:15 +02:00
import sys
2023-07-06 17:49:54 +02:00
from flask import Flask, Response, request, Request, abort, jsonify
2023-07-27 09:16:15 +02:00
from loguru import logger
2023-06-13 08:11:29 +02:00
from demo.utils.checker import inbox_prechecker
from demo.activitypub import ME
2023-07-06 17:49:54 +02:00
from demo import config
2023-06-07 08:58:53 +02:00
2023-07-27 09:16:15 +02:00
logger.remove()
logger.add(sys.stdout, level="DEBUG")
logger.add("output.log", level="DEBUG")
app = Flask(__name__,
static_folder="demo/static",)
2023-06-07 08:58:53 +02:00
def is_ap_requested(ap_request: Request) -> bool:
"""Check request accept headers."""
accept_str = ap_request.headers.get("accept")
2023-07-06 17:49:54 +02:00
if accept_str is None:
return False
for i in [
2023-07-06 17:49:54 +02:00
"application/activity+json",
"application/ld+json",
]:
2023-07-06 17:49:54 +02:00
if accept_str.startswith(i):
return True
return False
2023-06-07 08:58:53 +02:00
@app.route('/')
2023-06-08 10:54:36 +02:00
def index():
"""Show index page"""
2023-06-07 08:58:53 +02:00
return "Hello Fediverse!"
2023-06-13 08:11:29 +02:00
@app.route("/inbox", methods=["POST"])
def inbox():
"""Process inbox request"""
is_verify = inbox_prechecker(request)
if is_verify:
return "STUB"
return "STUB"
2023-07-06 17:49:54 +02:00
2023-07-09 10:03:31 +02:00
@app.route("/.well-known/nodeinfo")
def well_known_nodeinfo() -> Response:
"""Return nodeinfo path."""
return jsonify(
{
"links": [
{
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.0",
"href": f"{config.BASE_URL}/nodeinfo/2.0",
}
]
}
)
@app.get("/nodeinfo/2.0")
def nodeinfo() -> Response:
"""Return nodeinfo."""
return jsonify(
{
"version": "2.0",
"software": {
"name": "COSCUP-demo",
"version": "0.0.1",
},
"protocols": ["activitypub"],
"services": {"inbound": [], "outbound": []},
"usage": {"users": {"total": 1}},
"openRegistrations": False,
"metadata": {},
},
)
2023-07-06 17:49:54 +02:00
@app.route("/.well-known/webfinger")
def wellknown_webfinger() -> Response:
"""Exposes servers WebFinger data."""
resource = request.args.get("resource")
if resource not in [f"acct:{config.USERNAME}@{config.DOMAIN}", config.ID]:
abort(404)
resp = jsonify(
{
"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,
},
],
},
)
resp.headers["Access-Control-Allow-Origin"] = "*"
resp.headers["Content-Type"] = "application/jrd+json; charset=utf-8"
return resp
2023-07-12 09:46:21 +02:00
@app.route(f"/user/{config.USERNAME}")
def locate_user() -> Response:
"""Return user ActivityPub response."""
resp = jsonify(ME)
resp.headers["Content-Type"] = "application/activity+json"
return resp