summaryrefslogtreecommitdiff
path: root/sw/users.py
diff options
context:
space:
mode:
authorTom Harley2021-07-06 18:30:31 +0000
committerTom Harley2021-07-06 18:30:31 +0000
commit0a0007dee01f2bf17371967105a6ab3c43f85550 (patch)
tree9e01b8fa0f677a6bcd650afbb022c67a53ef3b4b /sw/users.py
parentAdd nixfmt to project (diff)
downloadswap-0a0007dee01f2bf17371967105a6ab3c43f85550.tar.gz
swap-0a0007dee01f2bf17371967105a6ab3c43f85550.zip

Begin work on adding user interaction

This commit adds support for getting information about a user, as well as accessing their inbox (not actually getting a CollectionPage yet, though).

Currently the inbox does not require the right user to be logged in to access their personal inbox; this should be fixed.

Diffstat (limited to 'sw/users.py')
-rw-r--r--sw/users.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/sw/users.py b/sw/users.py
new file mode 100644
index 0000000..724965e
--- /dev/null
+++ b/sw/users.py
@@ -0,0 +1,58 @@
+from flask import Blueprint, jsonify, url_for, request
+
+import json
+
+from sw.db import get_db
+from sw.time import now
+
+bp = Blueprint("users", __name__, url_prefix="/u")
+
+
+def ap(r):
+ def f(k):
+ return k.startswith("ap_") and r[k] is not None
+
+ res = {k: r[k] for k in filter(f, r.keys())}
+
+ res["id"] = url_for(".get_user_info", username=r["username"])
+ res["inbox"] = url_for(".get_user_inbox", username=r["username"])
+ # res["outbox"] = url_for(".get_user_outbox", username=r["username"])
+ # res["following"] = url_for(".get_user_following", username=r["username"])
+ # res["followers"] = url_for(".get_user_followers", username=r["username"])
+
+ return res
+
+
+@bp.route("/<username>")
+def get_user_info(username):
+ db = get_db()
+
+ user = db.execute("select * from users where username=?", (username,)).fetchone()
+
+ if user is None:
+ abort(404, "User not found")
+
+ return jsonify(ap(user))
+
+
+@bp.route("/<username>/inbox")
+def get_user_inbox(username):
+ db = get_db()
+
+ inbox_size = db.execute(
+ "select inbox_size from users where username=?", (username,)
+ ).fetchone()[0]
+
+ if inbox_size is None:
+ abort(404, "User not found")
+
+ request_time = request.date or now()
+
+ return jsonify(
+ {
+ "type": "OrderedCollection",
+ "totalItems": inbox_size,
+ "first": url_for(".get_user_inbox", username=username, before=request_time),
+ "current": url_for(".get_user_inbox", username=username),
+ }
+ )