From bafe42275e65b98951dd3335342e38d7bf1b1db5 Mon Sep 17 00:00:00 2001
From: Tom Harley
Date: Sun, 1 Aug 2021 14:27:21 +0000
Subject: Utilise jinja inheritance to streamline in/outbox
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.
---
sw/db.py | 11 +++++++++--
sw/templates/ap/collection_page.html | 19 +++++++++++++++++++
sw/templates/ap/collection_page.json | 23 +++++++++++++++++++++++
sw/templates/ap/ordered_collection_page.html | 1 +
sw/templates/ap/ordered_collection_page.json | 5 +++++
sw/templates/base.json | 6 +++++-
sw/templates/get_user_inbox_page.html | 16 ++++++++--------
sw/templates/get_user_inbox_page.json | 21 ++++++++++-----------
sw/templates/get_user_outbox_page.html | 10 +---------
sw/templates/get_user_outbox_page.json | 15 ++++-----------
sw/templates/user_info.html | 3 ++-
sw/users.py | 15 ++++-----------
12 files changed, 91 insertions(+), 54 deletions(-)
create mode 100644 sw/templates/ap/collection_page.html
create mode 100644 sw/templates/ap/collection_page.json
create mode 100644 sw/templates/ap/ordered_collection_page.html
create mode 100644 sw/templates/ap/ordered_collection_page.json
(limited to 'sw')
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 %}
+
+ {% for item in items %}
+ {% block item_info scoped %}
+ -
+
+ {{ item.summary or item.name or "unknown item" }}
+
+
+ {% endblock %}
+ {% endfor %}
+
+{% 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 %}
-{% block title %}{{ user["username"] }}'s Inbox {% endblock %}
+{% block title %}{{ user["username"] }}'s Inbox{% endblock %}
{% endblock %}
-{% block content %}
-
-{% for item in items %}
-- {{ item }}
-{% endfor %}
-
+{% block item_info scoped %}
+
+
+ {{ item }}
+
+
{% 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 %}
{% block title %}{{ user["username"] }}'s Outbox {% endblock %}
{% endblock %}
-
-{% block content %}
-
-{% for item in items %}
-- {{ item }}
-{% endfor %}
-
-{% 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 @@
user ID: {{ user["id"] }}
no. of posts: {{ user["outbox_size"] }}
-inbox: {{ url_for('users.get_user_inbox', username=user["username"]) }}
+Inbox
+Outbox
{% 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),
)
--
cgit v1.2.1