From 0f6846c3609c3b8caa34a296355e06d18261a1e1 Mon Sep 17 00:00:00 2001 From: Blake DeMarcy Date: Tue, 25 Apr 2017 03:36:51 -0500 Subject: [PATCH] add new parameter to threads: last_author --- clients/urwid/main.py | 12 +++++++++--- dbupdate.py | 9 +++++++++ schema.sql | 3 ++- server.py | 11 ++++++----- src/db.py | 8 +++++--- src/schema.py | 6 ++++-- src/utils.py | 3 ++- 7 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 dbupdate.py diff --git a/clients/urwid/main.py b/clients/urwid/main.py index fa343a3..0f0c942 100644 --- a/clients/urwid/main.py +++ b/clients/urwid/main.py @@ -594,17 +594,23 @@ class App(object): ] infoline = "%d replies; active %s" % ( - thread["reply_count"], self.timestring(thread["last_mod"], "delta")) + thread["reply_count"], + self.timestring(thread["last_mod"], "delta")) + last_author = self.usermap[thread["last_author"]] pile = [ urwid.Columns([(3, urwid.AttrMap(button, "button", "hover")), title]), urwid.Text(dateline), - urwid.AttrMap(urwid.Text(infoline), "dim"), + urwid.Text(("dim", infoline)), + urwid.Text([ + ("dim", "last post by "), + (str(last_author["color"]), "~" + last_author["user_name"]) + ]), urwid.AttrMap(urwid.Divider("-"), "dim") ] if self.prefs["index_spacing"]: - pile.insert(3, urwid.Divider()) + pile.insert(4, urwid.Divider()) pile = urwid.Pile(pile) pile.thread = thread diff --git a/dbupdate.py b/dbupdate.py new file mode 100644 index 0000000..717feb1 --- /dev/null +++ b/dbupdate.py @@ -0,0 +1,9 @@ +import sqlite3 + +with sqlite3.connect("data.sqlite") as _con: + _con.execute('ALTER TABLE threads ADD COLUMN last_author text DEFAULT ""') + _con.commit() + for tid in _con.execute("SELECT thread_id FROM threads"): + author = _con.execute("SELECT author FROM messages WHERE thread_id = ? ORDER BY post_id", tid).fetchall()[-1] + _con.execute("UPDATE threads SET last_author = ? WHERE thread_id = ?", author + tid) + _con.commit() diff --git a/schema.sql b/schema.sql index 7bd61f2..c5816f1 100644 --- a/schema.sql +++ b/schema.sql @@ -22,7 +22,8 @@ create table threads ( last_mod real, -- floating point unix timestamp (of last post or post edit) created real, -- floating point unix timestamp (when thread was made) reply_count int, -- integer (incremental, starting with 0) - pinned int -- boolean + pinned int, -- boolean + last_author text -- uuid string ); diff --git a/server.py b/server.py index aa88cdd..d20432f 100644 --- a/server.py +++ b/server.py @@ -87,24 +87,25 @@ def api_method(function): return wrapper -def create_usermap(connection, obj): +def create_usermap(connection, obj, index=False): """ Creates a mapping of all the user_ids that occur in OBJ to their full user objects (names, profile info, etc). Can be a thread_index or a messages object from one. """ - + user_set = {item["author"] for item in obj} + if index: + [user_set.add(item["last_author"]) for item in obj] return { user_id: db.user_resolve( connection, user_id, externalize=True, return_false=False) - for user_id in {item["author"] for item in obj} + for user_id in user_set } - def validate(json, args): """ Ensure the json object contains all the keys needed to satisfy @@ -206,7 +207,7 @@ class API(object): Requires no arguments. """ threads = db.thread_index(database) - cherrypy.thread_data.usermap = create_usermap(database, threads) + cherrypy.thread_data.usermap = create_usermap(database, threads, True) return threads diff --git a/src/db.py b/src/db.py index 7fd0fd5..1050e6a 100644 --- a/src/db.py +++ b/src/db.py @@ -106,11 +106,12 @@ def thread_create(connection, author_id, body, title, send_raw=False): thread_id = uuid1().hex scheme = schema.thread( thread_id, author_id, title, - now, now, -1, False) # see below for why i set -1 instead of 0 + now, now, -1, # see below for why i set -1 instead of 0 + False, author_id) connection.execute(""" INSERT INTO threads - VALUES (?,?,?,?,?,?,?) + VALUES (?,?,?,?,?,?,?,?) """, schema_values("thread", scheme)) connection.commit() # the thread is initially commited with reply_count -1 so that i can @@ -147,9 +148,10 @@ def thread_reply(connection, author_id, thread_id, body, send_raw=False, time_ov connection.execute(""" UPDATE threads SET reply_count = ?, + last_author = ?, last_mod = ? WHERE thread_id = ? - """, (count, now, thread_id)) + """, (count, author_id, now, thread_id)) connection.commit() return scheme diff --git a/src/schema.py b/src/schema.py index 7044c37..aedb765 100644 --- a/src/schema.py +++ b/src/schema.py @@ -128,7 +128,8 @@ def thread( last_mod, # floating point unix timestamp (of last post or post edit) created, # floating point unix timestamp (when thread was made) reply_count, # integer (incremental, starting with 0) - pinned): # boolean + pinned, # boolean + last_author): # uuid string return { "thread_id": thread_id, @@ -137,7 +138,8 @@ def thread( "last_mod": last_mod, "created": created, "reply_count": reply_count, - "pinned": bool(pinned) + "pinned": bool(pinned), + "last_author": last_author } diff --git a/src/utils.py b/src/utils.py index c533112..dbe4789 100644 --- a/src/utils.py +++ b/src/utils.py @@ -22,7 +22,8 @@ def schema_values(scheme, obj): elif scheme == "thread": return ordered_keys(obj, "thread_id", "author", "title", - "last_mod", "created", "reply_count", "pinned") + "last_mod", "created", "reply_count", + "pinned", "last_author") elif scheme == "message": return ordered_keys(obj,