diff options
| author | Tom Harley | 2021-07-06 18:30:31 +0000 |
|---|---|---|
| committer | Tom Harley | 2021-07-06 18:30:31 +0000 |
| commit | 0a0007dee01f2bf17371967105a6ab3c43f85550 (patch) | |
| tree | 9e01b8fa0f677a6bcd650afbb022c67a53ef3b4b /sw | |
| parent | Add nixfmt to project (diff) | |
| download | swap-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')
| -rw-r--r-- | sw/__init__.py | 3 | ||||
| -rw-r--r-- | sw/auth.py | 22 | ||||
| -rw-r--r-- | sw/db.py | 16 | ||||
| -rw-r--r-- | sw/schema.sql | 36 | ||||
| -rw-r--r-- | sw/time.py | 9 | ||||
| -rw-r--r-- | sw/users.py | 58 |
6 files changed, 134 insertions, 10 deletions
diff --git a/sw/__init__.py b/sw/__init__.py index 2601940..b4224fb 100644 --- a/sw/__init__.py +++ b/sw/__init__.py @@ -2,7 +2,7 @@ import os from flask import Flask -from sw import auth, ap, db +from sw import auth, ap, db, users def create_app(test_config=None): @@ -31,5 +31,6 @@ def create_app(test_config=None): app.register_blueprint(auth.bp) app.register_blueprint(ap.bp) + app.register_blueprint(users.bp) return app @@ -28,7 +28,7 @@ def load_logged_in_user(): else: g.user = ( - get_db().execute("select * from user where id=?", (user_id,)).fetchone() + get_db().execute("select * from users where id=?", (user_id,)).fetchone() ) @@ -62,14 +62,14 @@ def register(): db = get_db() existing_user = db.execute( - "select id from user where username=?", (username,) + "select id from users where username=?", (username,) ).fetchone() if existing_user is not None: return f"Username {username} is taken", 409 db.execute( - "insert into user (username, password) values (?, ?)", + "insert into users (username, password) values (?, ?)", (username, generate_password_hash(password)), ) db.commit() @@ -89,7 +89,7 @@ def login(): password = request.json["password"] db = get_db() - user = db.execute("select * from user where username=?", (username,)).fetchone() + user = db.execute("select * from users where username=?", (username,)).fetchone() if user is None: return "Username or password incorrect", 403 @@ -112,7 +112,19 @@ def login_required(view): @functools.wraps(view) def wrapped_view(**kwargs): if g.user is None: - return "Login required", 403 + abort("Login required", 403) + + return view(**kwargs) + + return wrapped_view + + +def specific_user_required(view): + @functools.wraps(view) + def wrapped_view(**kwargs): + username = kwargs.get("username") or kwargs.get("user") + if g.user != username: + abort("Unauthorised", 403) return view(**kwargs) @@ -4,6 +4,8 @@ import click from flask import current_app, g from flask.cli import with_appcontext +from werkzeug.security import generate_password_hash + def get_db(): if "db" not in g: @@ -41,6 +43,20 @@ def init_db_command(): click.echo("Initialised the database.") +@click.command("add-user") +@click.argument("username") +@click.argument("password") +@with_appcontext +def add_user_command(username, password): + db = get_db() + db.execute( + "insert into users (username, password) values (?, ?);", + (username, generate_password_hash(password)), + ) + db.commit() + + def init_app(app): app.teardown_appcontext(close_db) app.cli.add_command(init_db_command) + app.cli.add_command(add_user_command) diff --git a/sw/schema.sql b/sw/schema.sql index d6ee27a..a51a7b9 100644 --- a/sw/schema.sql +++ b/sw/schema.sql @@ -39,10 +39,38 @@ create table inbox ( ap_instrument text ); -drop table if exists user; +drop table if exists users; -create table user ( - id integer primary key autoincrement, +create table users ( + id integer primary key, username text unique not null, - password text not null + password text not null, + inbox_size integer default 0, + + ap_attachment text, + ap_attributedTo text, + ap_audience text, + ap_content text, + ap_context text, + ap_name text, + ap_endTime text, + ap_generator text, + ap_icon text, + ap_image text, + ap_inReplyTo text, + ap_location text, + ap_preview text, + ap_published text, + ap_replies text, + ap_startTime text, + ap_summary text, + ap_tag text, + ap_updated text, + ap_url text, + ap_to text, + ap_bto text, + ap_cc text, + ap_bcc text, + ap_mediaType text, + ap_duration text ); diff --git a/sw/time.py b/sw/time.py new file mode 100644 index 0000000..8111232 --- /dev/null +++ b/sw/time.py @@ -0,0 +1,9 @@ +from datetime import datetime + + +def time_formatted(t): + return t.replace(microsecond=0).isoformat() + + +def now(): + return time_formatted(datetime.now()) 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), + } + ) |
