Cleanup JSON outputs
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import json
|
||||
import mimetypes
|
||||
from datetime import datetime
|
||||
from datetime import timedelta
|
||||
@@ -10,7 +9,6 @@ from typing import List
|
||||
|
||||
import flask
|
||||
from bson.objectid import ObjectId
|
||||
from flask import Response
|
||||
from flask import abort
|
||||
from flask import current_app as app
|
||||
from flask import redirect
|
||||
@@ -41,6 +39,7 @@ from core.meta import _meta
|
||||
from core.shared import MY_PERSON
|
||||
from core.shared import _Response
|
||||
from core.shared import csrf
|
||||
from core.shared import jsonify
|
||||
from core.shared import login_required
|
||||
from core.tasks import Tasks
|
||||
from utils import emojis
|
||||
@@ -124,7 +123,7 @@ def _user_api_response(**kwargs) -> _Response:
|
||||
if _redirect:
|
||||
return redirect(_redirect)
|
||||
|
||||
resp = flask.jsonify(**kwargs)
|
||||
resp = jsonify(kwargs)
|
||||
resp.status_code = 201
|
||||
return resp
|
||||
|
||||
@@ -132,7 +131,7 @@ def _user_api_response(**kwargs) -> _Response:
|
||||
@blueprint.route("/api/key")
|
||||
@login_required
|
||||
def api_user_key() -> _Response:
|
||||
return flask.jsonify(api_key=ADMIN_API_KEY)
|
||||
return jsonify({"api_key": ADMIN_API_KEY})
|
||||
|
||||
|
||||
@blueprint.route("/note/delete", methods=["POST"])
|
||||
@@ -575,25 +574,24 @@ def api_follow() -> _Response:
|
||||
def api_debug() -> _Response:
|
||||
"""Endpoint used/needed for testing, only works in DEBUG_MODE."""
|
||||
if not DEBUG_MODE:
|
||||
return flask.jsonify(message="DEBUG_MODE is off")
|
||||
return jsonify({"message": "DEBUG_MODE is off"})
|
||||
|
||||
if request.method == "DELETE":
|
||||
_drop_db()
|
||||
return flask.jsonify(message="DB dropped")
|
||||
return jsonify(dict(message="DB dropped"))
|
||||
|
||||
return flask.jsonify(
|
||||
inbox=DB.activities.count({"box": Box.INBOX.value}),
|
||||
outbox=DB.activities.count({"box": Box.OUTBOX.value}),
|
||||
outbox_data=without_id(DB.activities.find({"box": Box.OUTBOX.value})),
|
||||
return jsonify(
|
||||
dict(
|
||||
inbox=DB.activities.count({"box": Box.INBOX.value}),
|
||||
outbox=DB.activities.count({"box": Box.OUTBOX.value}),
|
||||
outbox_data=without_id(DB.activities.find({"box": Box.OUTBOX.value})),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@blueprint.route("/stream")
|
||||
@api_required
|
||||
def api_stream() -> _Response:
|
||||
return Response(
|
||||
response=json.dumps(
|
||||
feed.build_inbox_json_feed("/api/stream", request.args.get("cursor"))
|
||||
),
|
||||
headers={"Content-Type": "application/json"},
|
||||
return jsonify(
|
||||
feed.build_inbox_json_feed("/api/stream", request.args.get("cursor"))
|
||||
)
|
||||
|
@@ -1,5 +1,4 @@
|
||||
import binascii
|
||||
import json
|
||||
import os
|
||||
from datetime import datetime
|
||||
from datetime import timedelta
|
||||
@@ -20,6 +19,7 @@ from config import DB
|
||||
from config import JWT
|
||||
from core.shared import _get_ip
|
||||
from core.shared import htmlify
|
||||
from core.shared import jsonify
|
||||
from core.shared import login_required
|
||||
|
||||
blueprint = flask.Blueprint("indieauth", __name__)
|
||||
@@ -27,11 +27,7 @@ blueprint = flask.Blueprint("indieauth", __name__)
|
||||
|
||||
def build_auth_resp(payload):
|
||||
if request.headers.get("Accept") == "application/json":
|
||||
return Response(
|
||||
status=200,
|
||||
headers={"Content-Type": "application/json"},
|
||||
response=json.dumps(payload),
|
||||
)
|
||||
return jsonify(payload)
|
||||
return Response(
|
||||
status=200,
|
||||
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
||||
|
@@ -1,9 +1,7 @@
|
||||
import json
|
||||
import mimetypes
|
||||
from typing import Any
|
||||
|
||||
import flask
|
||||
from flask import Response
|
||||
from flask import abort
|
||||
from flask import request
|
||||
from little_boxes import activitypub as ap
|
||||
@@ -11,6 +9,7 @@ from little_boxes import activitypub as ap
|
||||
import config
|
||||
from config import DB
|
||||
from core.meta import Box
|
||||
from core.shared import jsonify
|
||||
|
||||
blueprint = flask.Blueprint("well_known", __name__)
|
||||
|
||||
@@ -45,22 +44,21 @@ def wellknown_webfinger() -> Any:
|
||||
],
|
||||
}
|
||||
|
||||
return Response(
|
||||
response=json.dumps(out),
|
||||
headers={"Content-Type": "application/jrd+json; charset=utf-8"},
|
||||
)
|
||||
return jsonify(out, "application/jrd+json; charset=utf-8")
|
||||
|
||||
|
||||
@blueprint.route("/.well-known/nodeinfo")
|
||||
def wellknown_nodeinfo() -> Any:
|
||||
"""Exposes the NodeInfo endpoint (http://nodeinfo.diaspora.software/)."""
|
||||
return flask.jsonify(
|
||||
links=[
|
||||
{
|
||||
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.1",
|
||||
"href": f"{config.ID}/nodeinfo",
|
||||
}
|
||||
]
|
||||
return jsonify(
|
||||
{
|
||||
"links": [
|
||||
{
|
||||
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.1",
|
||||
"href": f"{config.ID}/nodeinfo",
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -73,29 +71,25 @@ def nodeinfo() -> Any:
|
||||
"type": {"$in": [ap.ActivityType.CREATE.value, ap.ActivityType.ANNOUNCE.value]},
|
||||
}
|
||||
|
||||
response = json.dumps(
|
||||
{
|
||||
"version": "2.1",
|
||||
"software": {
|
||||
"name": "microblogpub",
|
||||
"version": config.VERSION,
|
||||
"repository": "https://github.com/tsileo/microblog.pub",
|
||||
},
|
||||
"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 Response(
|
||||
headers={
|
||||
"Content-Type": "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.1#"
|
||||
out = {
|
||||
"version": "2.1",
|
||||
"software": {
|
||||
"name": "microblogpub",
|
||||
"version": config.VERSION,
|
||||
"repository": "https://github.com/tsileo/microblog.pub",
|
||||
},
|
||||
response=response,
|
||||
"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#",
|
||||
)
|
||||
|
Reference in New Issue
Block a user