summaryrefslogtreecommitdiff
path: root/sw
diff options
context:
space:
mode:
authorTom Harley2021-08-01 14:27:21 +0000
committerTom Harley2021-08-02 14:41:06 +0000
commitbafe42275e65b98951dd3335342e38d7bf1b1db5 (patch)
tree69af3fd0d1ff77baea81a3396a45c7287afdecc0 /sw
parentSupport dev environment via nix-shell (diff)
downloadswap-untested.tar.gz
swap-untested.zip

Utilise jinja inheritance to streamline in/outbox

untested

Using jinja’s templating extensions, we can recreate the activitypub inheritance hierarchy without using clunky python OO.

Extending from multiple template files in sequence might cause unnecesary IO to slow response times; if this ever becomes an issue, it should be possible to pre-process some of the templating since a lot of it will be based on constant expressions (such as @context and type).

I think some formatting fixes leaked into this commit; oops! I’m leaving this note here in the hopes that if I ever want to clean this up, I can locate this commit easier.

Diffstat (limited to 'sw')
-rw-r--r--sw/db.py11
-rw-r--r--sw/templates/ap/collection_page.html19
-rw-r--r--sw/templates/ap/collection_page.json23
-rw-r--r--sw/templates/ap/ordered_collection_page.html1
-rw-r--r--sw/templates/ap/ordered_collection_page.json5
-rw-r--r--sw/templates/base.json6
-rw-r--r--sw/templates/get_user_inbox_page.html16
-rw-r--r--sw/templates/get_user_inbox_page.json21
-rw-r--r--sw/templates/get_user_outbox_page.html10
-rw-r--r--sw/templates/get_user_outbox_page.json15
-rw-r--r--sw/templates/user_info.html3
-rw-r--r--sw/users.py15
12 files changed, 91 insertions, 54 deletions
diff --git a/sw/db.py b/sw/db.py
index 3739a62..344726b 100644
--- a/sw/db.py
+++ b/sw/db.py
@@ -1,20 +1,27 @@
import sqlite3
+import logging
+
+from collections import defaultdict
import click
-from flask import current_app, g
+
+from flask import current_app, g, url_for
from flask.cli import with_appcontext
from werkzeug.security import generate_password_hash
from sw.time import now
+
def get_db():
if "db" not in g:
g.db = sqlite3.connect(
- current_app.config["DATABASE"], detect_types=sqlite3.PARSE_DECLTYPES,
+ current_app.config["DATABASE"],
+ detect_types=sqlite3.PARSE_DECLTYPES,
)
g.db.row_factory = sqlite3.Row
+ g.db.set_trace_callback(logging.debug)
return g.db
diff --git a/sw/templates/ap/collection_page.html b/sw/templates/ap/collection_page.html
new file mode 100644
index 0000000..dc209d0
--- /dev/null
+++ b/sw/templates/ap/collection_page.html
@@ -0,0 +1,19 @@
+{% extends "base.html" %}
+
+{% block content %}
+
+{% block items %}
+<ol>
+ {% for item in items %}
+ {% block item_info scoped %}
+ <li value="{{ item.id }}">
+ <a href="{{ url_for('items.get_item_info', item_id=item.id) }}">
+ {{ item.summary or item.name or "unknown item" }}
+ </a>
+ </li>
+ {% endblock %}
+ {% endfor %}
+</ol>
+{% endblock %}
+
+{% endblock %}
diff --git a/sw/templates/ap/collection_page.json b/sw/templates/ap/collection_page.json
new file mode 100644
index 0000000..e3bfe3b
--- /dev/null
+++ b/sw/templates/ap/collection_page.json
@@ -0,0 +1,23 @@
+{% extends "base.json" %}
+
+{% block content %}
+
+ {% block type %}
+ "type": "CollectionPage",
+ {% endblock %}
+
+ {% block nav %}
+ {% endblock %}
+
+ {% block items %}
+ "items": [
+ {% for item in items %}
+ {% block item_info scoped %}
+ "{{ url_for('items.get_item_info', item_id=item['id']) }}"
+ {% endblock %}
+ {{- "," if not loop.last }}
+ {% endfor %}
+ ],
+ {% endblock %}
+
+{% endblock %}
diff --git a/sw/templates/ap/ordered_collection_page.html b/sw/templates/ap/ordered_collection_page.html
new file mode 100644
index 0000000..57ed54b
--- /dev/null
+++ b/sw/templates/ap/ordered_collection_page.html
@@ -0,0 +1 @@
+{% extends "ap/collection_page.html" %}
diff --git a/sw/templates/ap/ordered_collection_page.json b/sw/templates/ap/ordered_collection_page.json
new file mode 100644
index 0000000..1a97b4a
--- /dev/null
+++ b/sw/templates/ap/ordered_collection_page.json
@@ -0,0 +1,5 @@
+{% extends "ap/collection_page.json" %}
+
+{% block type %}
+"type": "OrderedCollectionPage",
+{% endblock %}
diff --git a/sw/templates/base.json b/sw/templates/base.json
index e0a0876..58da7f8 100644
--- a/sw/templates/base.json
+++ b/sw/templates/base.json
@@ -1,4 +1,8 @@
{
-{% block content %}{% endblock %}
+{% block content %}
+{% endblock %}
+
+{% block context %}
"@context": "https://www.w3.org/ns/activitystreams"
+{% endblock %}
}
diff --git a/sw/templates/get_user_inbox_page.html b/sw/templates/get_user_inbox_page.html
index a331049..a839015 100644
--- a/sw/templates/get_user_inbox_page.html
+++ b/sw/templates/get_user_inbox_page.html
@@ -1,13 +1,13 @@
-{% extends "base.html" %}
+{% extends "ap/ordered_collection_page.html" %}
{% block header %}
-<h1>{% block title %}{{ user["username"] }}'s Inbox {% endblock %}</h1>
+<h1>{% block title %}{{ user["username"] }}'s Inbox{% endblock %}</h1>
{% endblock %}
-{% block content %}
-<ul>
-{% for item in items %}
-<li value="{{ items['id'] }}">{{ item }}</li>
-{% endfor %}
-</ul>
+{% block item_info scoped %}
+
+<li> <a href="{{ item }}">
+ {{ item }}
+</a> </li>
+
{% endblock %}
diff --git a/sw/templates/get_user_inbox_page.json b/sw/templates/get_user_inbox_page.json
index 96bf9e8..886aeda 100644
--- a/sw/templates/get_user_inbox_page.json
+++ b/sw/templates/get_user_inbox_page.json
@@ -1,20 +1,19 @@
-{% extends "base.json" %}
-
-{% block content %}
-"type": "OrderedCollectionPage",
+{% extends "ap/ordered_collection_page.json" %}
+{% block nav %}
"id": "{{ url_for('.get_user_inbox_page', username=user['username'], max_id=max_id) }}",
+"current": "{{ url_for('.get_user_inbox_page', username=user['username']) }}",
+"partOf": "{{ url_for('.get_user_inbox', username=user['username']) }}",
{% 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": [
- {% for item in items %}
- "{{ item }}"{{ ',' if not loop.last }}
- {% endfor %}
-],
+{% endblock %}
+
+{% block item_info scoped %}
+
+"{{ item }}"
+
{% endblock %}
diff --git a/sw/templates/get_user_outbox_page.html b/sw/templates/get_user_outbox_page.html
index 2907604..dfa60c2 100644
--- a/sw/templates/get_user_outbox_page.html
+++ b/sw/templates/get_user_outbox_page.html
@@ -1,13 +1,5 @@
-{% extends "base.html" %}
+{% extends "ap/ordered_collection_page.html" %}
{% block header %}
<h1>{% block title %}{{ user["username"] }}'s Outbox {% endblock %}</h1>
{% endblock %}
-
-{% block content %}
-<ul>
-{% for item in items %}
-<li value="{{ item['id'] }}">{{ item }}</li>
-{% endfor %}
-</ul>
-{% endblock %}
diff --git a/sw/templates/get_user_outbox_page.json b/sw/templates/get_user_outbox_page.json
index 2f3936f..8cdb93b 100644
--- a/sw/templates/get_user_outbox_page.json
+++ b/sw/templates/get_user_outbox_page.json
@@ -1,20 +1,13 @@
-{% extends "base.json" %}
-
-{% block content %}
-"type": "OrderedCollecdtionPage",
+{% extends "ap/ordered_collection_page.json" %}
+{% block nav %}
"id": "{{ url_for('.get_user_outbox_page', username=user['username'], max_id=max_id) }}",
+"current": "{{ url_for('.get_user_outbox_page', username=user['username']) }}",
+"partOf": "{{ url_for('.get_user_outbox', username=user['username']) }}",
{% 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": [
- {% for item in items %}
- "{{ item }}"{{ "," if not loop.last }}
- {% endfor %}
-],
{% endblock %}
diff --git a/sw/templates/user_info.html b/sw/templates/user_info.html
index 96c3d2f..557957a 100644
--- a/sw/templates/user_info.html
+++ b/sw/templates/user_info.html
@@ -9,5 +9,6 @@
<p>user ID: {{ user["id"] }}</p>
<p>no. of posts: {{ user["outbox_size"] }}</p>
<br>
-<p>inbox: <a href="{{ url_for('users.get_user_inbox', username=user["username"]) }}">{{ url_for('users.get_user_inbox', username=user["username"]) }}</a></p>
+<p><a href="{{ url_for('users.get_user_inbox', username=user["username"]) }}">Inbox</a></p>
+<p><a href="{{ url_for('users.get_user_outbox', username=user["username"]) }}">Outbox</a></p>
{% endblock %}
diff --git a/sw/users.py b/sw/users.py
index 2df6039..9853748 100644
--- a/sw/users.py
+++ b/sw/users.py
@@ -81,7 +81,7 @@ def get_user_inbox_page(username):
return prepared_output(
"get_user_inbox_page",
user=g.user,
- items=[url_for("items.get_item_info", item_id=x) for x in ids],
+ items=[x["content_id"] for x in res],
max_id=max(ids, default=None),
min_id=min(ids, default=None),
)
@@ -131,7 +131,8 @@ def post_user_inbox_item(username):
)
db.execute(
- "update users set inbox_size = inbox_size + 1 where id = ? ;", (g.user["id"],),
+ "update users set inbox_size = inbox_size + 1 where id = ? ;",
+ (g.user["id"],),
)
db.commit()
@@ -166,20 +167,12 @@ def get_user_outbox_page(username):
},
).fetchall()
- def ap(r):
- def f(k):
- return r[k] is not None
-
- 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=[url_for("items.get_item_info", item_id=x) for x in ids],
+ items=res,
max_id=max(ids, default=None),
min_id=min(ids, default=None),
)