diff options
Diffstat (limited to 'sw/users.py')
| -rw-r--r-- | sw/users.py | 80 |
1 files changed, 24 insertions, 56 deletions
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("/<username>") 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("/<username>/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<? " - "order by sent desc " - "limit ? " + "select * from items " + "where actor=:actor " + "and (id <= :max_id or :max_id is null) " + "order by id desc " + "limit :limit " ";", - (username, page_index, outbox_fetch_limit), + { + "actor": username, + "max_id": max_id, + "limit": outbox_fetch_limit, + }, ).fetchall() def ap(r): def f(k): - return k.startswith("ap_") and r[k] is not None + return r[k] is not None - res = {k[3:]: r[k] for k in filter(f, r.keys())} + res = {k: r[k] for k in filter(f, r.keys())} return res + ids = [x["id"] for x in res] + return prepared_output( "get_user_outbox_page", user=g.user, - items=[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), ) @@ -228,7 +196,7 @@ def post_user_outbox_item(username): item_id = None if "id" not in data: res = db.exec( - "insert into items (created, updated, ap_content) " + "insert into items (created, updated, content_id) " "output Inserted.id " "values (?,?,?) " ";", @@ -247,7 +215,7 @@ def post_user_outbox_item(username): if not is_ap_activity(data): activity = create_wrapper(item_id) res = db.exec( - "insert into items (created, updated, ap_content) " + "insert into items (created, updated, content_id) " "output Inserted.id " "values (?,?,?) " ";", |
