From c77d6a4d4a1d628788be73491aa7fa4e4fdcff05 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Fri, 30 Jul 2021 18:35:08 +0000 Subject: Add templates Rather than using jsonify on a dict, the system now uses render_template on json documents. This gives the ability to extend previous files (which might be useful for activitypub features), and also enables using different templates for different filetypes. Supplied are templates for accessing inboxes and outboxes in JSON-LD and HTML. The "items" field in get_user_inbox_page.json and get_user_outbox_page.json might be currently borked. --- sw/schema.sql | 3 + sw/templates/base.html | 20 +++++ sw/templates/base.json | 4 + sw/templates/get_user_inbox.html | 11 +++ sw/templates/get_user_inbox.json | 11 +++ sw/templates/get_user_inbox_page.html | 13 +++ sw/templates/get_user_inbox_page.json | 16 ++++ sw/templates/get_user_outbox.html | 11 +++ sw/templates/get_user_outbox.json | 9 ++ sw/templates/get_user_outbox_page.html | 13 +++ sw/templates/get_user_outbox_page.json | 16 ++++ sw/templates/index.html | 9 ++ sw/templates/user_info.html | 13 +++ sw/templates/user_info.json | 11 +++ sw/users.py | 157 ++++++++++++++++----------------- 15 files changed, 234 insertions(+), 83 deletions(-) create mode 100644 sw/templates/base.html create mode 100644 sw/templates/base.json create mode 100644 sw/templates/get_user_inbox.html create mode 100644 sw/templates/get_user_inbox.json create mode 100644 sw/templates/get_user_inbox_page.html create mode 100644 sw/templates/get_user_inbox_page.json create mode 100644 sw/templates/get_user_outbox.html create mode 100644 sw/templates/get_user_outbox.json create mode 100644 sw/templates/get_user_outbox_page.html create mode 100644 sw/templates/get_user_outbox_page.json create mode 100644 sw/templates/index.html create mode 100644 sw/templates/user_info.html create mode 100644 sw/templates/user_info.json (limited to 'sw') diff --git a/sw/schema.sql b/sw/schema.sql index 35aae2d..97f4785 100644 --- a/sw/schema.sql +++ b/sw/schema.sql @@ -1,6 +1,8 @@ drop table if exists inbox; create table inbox ( + id integer primary key, + recipient text, received text, @@ -34,6 +36,7 @@ create table users ( username text unique not null, password text not null, inbox_size integer default 0, + outbox_size integer default 0, ap_attachment text, ap_attributedTo text, diff --git a/sw/templates/base.html b/sw/templates/base.html new file mode 100644 index 0000000..4f2acf1 --- /dev/null +++ b/sw/templates/base.html @@ -0,0 +1,20 @@ + +{% block title %}{% endblock %} + + +
+
+ {% block header %}{% endblock %} +
+ {% block content %}{% endblock %} +
diff --git a/sw/templates/base.json b/sw/templates/base.json new file mode 100644 index 0000000..e0a0876 --- /dev/null +++ b/sw/templates/base.json @@ -0,0 +1,4 @@ +{ +{% block content %}{% endblock %} +"@context": "https://www.w3.org/ns/activitystreams" +} diff --git a/sw/templates/get_user_inbox.html b/sw/templates/get_user_inbox.html new file mode 100644 index 0000000..7956a21 --- /dev/null +++ b/sw/templates/get_user_inbox.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block header %} +

{% block title %}{{ user["username"] }}'s Inbox }}{% endblock %}

+{% endblock %} + +{% block content %} +

{{ user["inbox_size"] }} items

+

First page

+

Last page

+{% endblock %} diff --git a/sw/templates/get_user_inbox.json b/sw/templates/get_user_inbox.json new file mode 100644 index 0000000..c186fae --- /dev/null +++ b/sw/templates/get_user_inbox.json @@ -0,0 +1,11 @@ +{% extends "base.json" %} + +{% block content %} +"type": "OrderedCollection", + +"id": "{{ url_for('.get_user_inbox', username=user['username']) }}", + +"totalItems": {{ user['inbox_size'] }}, +"first": "{{ url_for('.get_user_inbox_page', username=user['username']) }}", +"last": "{{ url_for('.get_user_inbox_page', username=user['username'], min_id=1) }}", +{% endblock %} diff --git a/sw/templates/get_user_inbox_page.html b/sw/templates/get_user_inbox_page.html new file mode 100644 index 0000000..a331049 --- /dev/null +++ b/sw/templates/get_user_inbox_page.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} + +{% block header %} +

{% block title %}{{ user["username"] }}'s Inbox {% endblock %}

+{% endblock %} + +{% block content %} + +{% endblock %} diff --git a/sw/templates/get_user_inbox_page.json b/sw/templates/get_user_inbox_page.json new file mode 100644 index 0000000..50a9f06 --- /dev/null +++ b/sw/templates/get_user_inbox_page.json @@ -0,0 +1,16 @@ +{% extends "base.json" %} + +{% block content %} +"type": "OrderedCollectionPage", + +"id": "{{ url_for('.get_user_inbox_page', username=user['username'], max_id=max_id) }}", + +{% if items %} +"next": "{{ url_for('.get_user_inbox_page', username=user['username'], max_id=(min_id-1)) }}", +"prev": "{{ url_for('.get_user_inbox_page', username=user['username'], min_id=(max_id+1)) }}", +{% endif %} +"current": "{{ url_for('.get_user_inbox_page', username=user['username']) }}", +"partOf": "{{ url_for('.get_user_inbox', username=user['username']) }}", + +"items": {{ items }}, +{% endblock %} diff --git a/sw/templates/get_user_outbox.html b/sw/templates/get_user_outbox.html new file mode 100644 index 0000000..9f4d893 --- /dev/null +++ b/sw/templates/get_user_outbox.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block header %} +

{% block title %}{{ user["username"] }}'s Outbox }}{% endblock %}

+{% endblock %} + +{% block content %} +

{{ user["outbox_size"] }} items

+

First page

+

Last page

+{% endblock %} diff --git a/sw/templates/get_user_outbox.json b/sw/templates/get_user_outbox.json new file mode 100644 index 0000000..ce2fad4 --- /dev/null +++ b/sw/templates/get_user_outbox.json @@ -0,0 +1,9 @@ +{% extends "base.json" %} + +{% block content %} +"id": "{{ url_for('.get_user_inbox', username=user['username']) }}", +"type": "OrderedCollection", + +"totalItems": "{{ user['outbox_size'] }}", +"first": "{{ url_for('.get_user_outbox_page', username=user['username']) }}", +{% endblock %} diff --git a/sw/templates/get_user_outbox_page.html b/sw/templates/get_user_outbox_page.html new file mode 100644 index 0000000..2907604 --- /dev/null +++ b/sw/templates/get_user_outbox_page.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} + +{% block header %} +

{% block title %}{{ user["username"] }}'s Outbox {% endblock %}

+{% endblock %} + +{% block content %} + +{% endblock %} diff --git a/sw/templates/get_user_outbox_page.json b/sw/templates/get_user_outbox_page.json new file mode 100644 index 0000000..b25a5ce --- /dev/null +++ b/sw/templates/get_user_outbox_page.json @@ -0,0 +1,16 @@ +{% extends "base.json" %} + +{% block content %} +"type": "OrderedCollecdtionPage", + +"id": "{{ url_for('.get_user_outbox_page', username=user['username'], max_id=max_id) }}", + +{% if items %} +"next": "{{ url_for('.get_user_outbox_page', username=user['username'], max_id=(min_id-1)) }}", +"prev": "{{ url_for('.get_user_outbox_page', username=user['username'], min_id=(max_id+1)) }}", +{% endif %} +"current": "{{ url_for('.get_user_outbox_page', username=user['username']) }}", +"partOf": "{{ url_for('.get_user_outbox', username=user['username']) }}", + +"items": {{ items }}, +{% endblock %} diff --git a/sw/templates/index.html b/sw/templates/index.html new file mode 100644 index 0000000..12e9e79 --- /dev/null +++ b/sw/templates/index.html @@ -0,0 +1,9 @@ +{% extends "base.html" %} + +{% block header %} +

{% block title %}Register{% endblock %}

+{% endblock %} + +{% block content %} +Hello, World! +{% endblock %} diff --git a/sw/templates/user_info.html b/sw/templates/user_info.html new file mode 100644 index 0000000..96c3d2f --- /dev/null +++ b/sw/templates/user_info.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} + +{% block header %} +

{% block title %}User {{ user["username"] }}{% endblock %}

+{% endblock %} + +{% block content %} +

username: {{ user["username"] }}

+

user ID: {{ user["id"] }}

+

no. of posts: {{ user["outbox_size"] }}

+
+

inbox: {{ url_for('users.get_user_inbox', username=user["username"]) }}

+{% endblock %} diff --git a/sw/templates/user_info.json b/sw/templates/user_info.json new file mode 100644 index 0000000..a14d6bc --- /dev/null +++ b/sw/templates/user_info.json @@ -0,0 +1,11 @@ +{% extends "base.json" %} + +{% block content %} +"type": "Actor", + +"name": "{{ user['username'] }}", +"id": "{{ url_for('users.get_user_info', username=user['username']) }}", + +"inbox": "{{ url_for('users.get_user_inbox', username=user['username']) }}", +"outbox": "{{ url_for('users.get_user_outbox', username=user['username']) }}", +{% endblock %} diff --git a/sw/users.py b/sw/users.py index 71258c1..573b58f 100644 --- a/sw/users.py +++ b/sw/users.py @@ -1,4 +1,13 @@ -from flask import Blueprint, jsonify, url_for, request, abort, g, Response +from flask import ( + Blueprint, + jsonify, + url_for, + request, + abort, + g, + Response, + render_template, +) import json @@ -24,6 +33,39 @@ 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() @@ -33,82 +75,62 @@ def get_user_info(username): if user is None: abort(404, "User not found") - return jsonify(ap(user)) + return prepared_output("user_info", user=user) @bp.route("//inbox") @specific_user_required def get_user_inbox(username): - db = get_db() - - inbox_size = db.execute( - "select inbox_size from users where username=?", (username,) - ).fetchone()[0] - # since a logged-in user is required to access this endpoint, there # is no way that a non-existant user can be queried (in other # words, the user will not have the clearance to see an unreal # user's inbox) - return jsonify( - { - "id": url_for(".get_user_inbox", username=username), - "type": "OrderedCollection", - "totalItems": inbox_size, - "first": url_for( - ".get_user_inbox_page", username=username, page_index=now() - ), - # "last": url_for(".get_user_inbox_page", username=username, page=1), - } - ) + return prepared_output("get_user_inbox", user=g.user) -@bp.route("//inbox/") +@bp.route("//inbox/items") @specific_user_required -def get_user_inbox_page(username, page_index): +def get_user_inbox_page(username): db = get_db() inbox_fetch_limit = 10 # TODO: give this variable a home - if page_index == "latest": - page_index = now() + max_id = request.args.get("max_id") res = db.execute( "select * from inbox " "where recipient=? " - "and received/outbox") def get_user_outbox(username): - db = get_db() + return prepared_output("get_user_outbox", user=g.user) - outbox_size = db.execute( - "select outbox_size from users where username=?", (username,) - ).fetchone()["outbox_size"] - if outbox_size is None: - abort(404, "User not found") - - # TODO: outbox contents not filtered by recipient data - - return jsonify( - { - "id": url_for(".get_user_inbox", username=username), - "type": "OrderedCollection", - "totalItems": outbox_size, - "first": url_for( - ".get_user_outbox_page", username=username, page_index=now() - ), - } - ) - - -@bp.route("/username/outbox/") +@bp.route("/username/outbox/items") def get_user_outbox_page(username, page_index): db = get_db() outbox_fetch_limit = 10 # TODO: give this variable a home - if page_index == "latest": - page_index = now() + max_id = request.args.get("max_id") res = db.execute( "select * from outbox " @@ -203,26 +204,16 @@ def get_user_outbox_page(username, page_index): 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("items.get_item", item_id=r["id"]) - return json.dumps(res) - - earliest_sent = res[-1]["sent"] + res = {k[3:]: r[k] for k in filter(f, r.keys())} - # TODO: the final page of the collection should have a null/missing `next` property + return res - return jsonify( - { - "id": url_for( - ".get_user_outbox_page", username=username, page_index=page_index - ), - "type": "OrderedCollectionPage", - "next": url_for( - ".get_user_outbox_page", username=username, page_index=earliest_sent - ), - "partOf": url_for(".get_user_outbox", username=username), - "items": [ap(x) for x in res], - } + return prepared_output( + "get_user_outbox_page", + user=g.user, + items=[ap(x) for x in res], + max_id=max(ids, default=None), + min_id=min(ids, default=None), ) -- cgit v1.2.1