add new parameter to threads: last_author

pull/4/head
Blake DeMarcy 2017-04-25 03:36:51 -05:00
parent 90b5573f84
commit 0f6846c360
7 changed files with 37 additions and 15 deletions

View File

@ -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

9
dbupdate.py 100644
View File

@ -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()

View File

@ -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
);

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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,