From 68703447d8fc57c7b77b66a6acc05bf22fb7d71b Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Sat, 31 Jul 2021 14:06:45 +0000 Subject: Display inbox/outbox contents correctly As per previous commit, the contents of inboxes and outboxes were not being properly displayed, which was difficult to test because of there being very few items in the test DB. This commit includes moving functions such as prepared_output into a new file sw/util.py; including them in sw/users.py made little sense and caused a circular dependency with sw/auth.py. A stub function for item info retreival has also been created, in new file sw/items.py. --- sw/users.py | 80 +++++++++++++++++++------------------------------------------ 1 file changed, 24 insertions(+), 56 deletions(-) (limited to 'sw/users.py') diff --git a/sw/users.py b/sw/users.py index 573b58f..2df6039 100644 --- a/sw/users.py +++ b/sw/users.py @@ -14,6 +14,7 @@ import json from sw.db import get_db from sw.time import now from sw.auth import specific_user_required, json_input, from_trusted_server +from sw.util import prepared_output bp = Blueprint("users", __name__, url_prefix="/u") @@ -33,39 +34,6 @@ def ap(r): return res -def decide_response_filetype(): - res = request.accept_mimetypes.best_match( - [ - "text/html", - "application/ld+json", - "application/json", - ] - ) - - if not request.accept_mimetypes: - return (".json", "application/ld+json") - - elif res == "text/html": - return (".html", "text/html") - - elif res == "application/json": - return (".json", "application/json") - - elif res == "application/ld+json": - return (".json", "application/ld+json") - - else: # res is None - abort(406) - - -def prepared_output(filename, **kwargs): - ext, mime = decide_response_filetype() - - body = render_template(f"{filename}{ext}", **kwargs) - - return Response(body, content_type=mime) - - @bp.route("/") def get_user_info(username): db = get_db() @@ -108,20 +76,12 @@ def get_user_inbox_page(username): (username, max_id, max_id, inbox_fetch_limit), ).fetchall() - def ap(r): - def f(k): - return k.startswith("ap_") and r[k] is not None - - res = {k[3:]: r[k] for k in filter(f, r.keys())} - - return json.dumps(res) - ids = [x["id"] for x in res] return prepared_output( "get_user_inbox_page", user=g.user, - items=json.dumps([ap(x) for x in res]), + items=[url_for("items.get_item_info", item_id=x) for x in ids], max_id=max(ids, default=None), min_id=min(ids, default=None), ) @@ -163,8 +123,10 @@ def post_user_inbox_item(username): db = get_db() time = now() + # TODO: this currently doesn't store anonymous/dereferenced objects correctly + res = db.execute( - "insert into inbox (recipient, received, ap_content) values (?,?,?) ;", + "insert into inbox (recipient, received, content_id) values (?,?,?) ;", (username, time, str(data)), ) @@ -182,8 +144,8 @@ def get_user_outbox(username): return prepared_output("get_user_outbox", user=g.user) -@bp.route("/username/outbox/items") -def get_user_outbox_page(username, page_index): +@bp.route("//outbox/items") +def get_user_outbox_page(username): db = get_db() outbox_fetch_limit = 10 # TODO: give this variable a home @@ -191,27 +153,33 @@ def get_user_outbox_page(username, page_index): max_id = request.args.get("max_id") res = db.execute( - "select * from outbox " - "where sender=? " - "and sent