add option to include ops with index (or only ops with gets)

This commit is contained in:
Blake DeMarcy 2017-04-29 20:48:38 -05:00
parent d5ae142a7f
commit e673706ae5
3 changed files with 24 additions and 13 deletions

View File

@ -424,7 +424,7 @@ class BBJ(object):
return response["data"] return response["data"]
def thread_index(self): def thread_index(self, include_op=False):
""" """
Returns a tuple where [0] is a list of all threads ordered by Returns a tuple where [0] is a list of all threads ordered by
most recently interacted, and [1] is a usermap object. most recently interacted, and [1] is a usermap object.
@ -435,11 +435,11 @@ class BBJ(object):
author_id = thread["author"] author_id = thread["author"]
print(usermap[author_id]["user_name"]) print(usermap[author_id]["user_name"])
""" """
response = self("thread_index") response = self("thread_index", include_op=include_op)
return response["data"], response["usermap"] return response["data"], response["usermap"]
def thread_load(self, thread_id, format=None): def thread_load(self, thread_id, format=None, op_only=False):
""" """
Returns a tuple where [0] is a thread object and [1] is a usermap object. Returns a tuple where [0] is a thread object and [1] is a usermap object.
@ -450,7 +450,8 @@ class BBJ(object):
print(usermap[author_id]["user_name"]) print(usermap[author_id]["user_name"])
print(message["body"]) print(message["body"])
""" """
response = self("thread_load", format=format, thread_id=thread_id) response = self("thread_load",
format=format, thread_id=thread_id, op_only=op_only)
return response["data"], response["usermap"] return response["data"], response["usermap"]

View File

@ -241,8 +241,13 @@ class API(object):
""" """
Return an array with all the threads, ordered by most recent activity. Return an array with all the threads, ordered by most recent activity.
Requires no arguments. Requires no arguments.
Optionally, you may supply the argument `include_op`, which, when non-nil,
will include a "messages" key with the object, whose sole content is the
original message (post_id 0).
""" """
threads = db.thread_index(database) op = isinstance(args, dict) and args.get("include_op")
threads = db.thread_index(database, include_op=op)
cherrypy.thread_data.usermap = create_usermap(database, threads, True) cherrypy.thread_data.usermap = create_usermap(database, threads, True)
return threads return threads
@ -328,9 +333,13 @@ class API(object):
Requires the argument `thread_id`. `format` may also be Requires the argument `thread_id`. `format` may also be
specified as a formatter to run the messages through. specified as a formatter to run the messages through.
Currently only "sequential" is supported. Currently only "sequential" is supported.
You may also supply the parameter `op_only`. When it's value
is non-nil, the messages array will only include post_id 0 (the first)
""" """
validate(args, ["thread_id"]) validate(args, ["thread_id"])
thread = db.thread_get(database, args["thread_id"]) thread = db.thread_get(
database, args["thread_id"], op_only=args.get("op_only"))
cherrypy.thread_data.usermap = \ cherrypy.thread_data.usermap = \
create_usermap(database, thread["messages"]) create_usermap(database, thread["messages"])
do_formatting(args.get("format"), thread["messages"]) do_formatting(args.get("format"), thread["messages"])

View File

@ -79,7 +79,7 @@ def message_feed(connection, time):
### THREADS ### ### THREADS ###
def thread_get(connection, thread_id, messages=True): def thread_get(connection, thread_id, messages=True, op_only=False):
""" """
Fetch the thread_id from the database. Formatting is be handled Fetch the thread_id from the database. Formatting is be handled
elsewhere. elsewhere.
@ -96,17 +96,18 @@ def thread_get(connection, thread_id, messages=True):
raise BBJParameterError("Thread does not exist.") raise BBJParameterError("Thread does not exist.")
thread = schema.thread(*thread) thread = schema.thread(*thread)
if messages: if messages or op_only:
c.execute(""" query = "SELECT * FROM messages WHERE thread_id = ? %s"
SELECT * FROM messages WHERE thread_id = ? c.execute(query % (
ORDER BY post_id""", (thread_id,)) "AND post_id = 0" if op_only else "ORDER BY post_id"
), (thread_id,))
# create a list where each post_id matches its list[index] # create a list where each post_id matches its list[index]
thread["messages"] = [schema.message(*values) for values in c.fetchall()] thread["messages"] = [schema.message(*values) for values in c.fetchall()]
return thread return thread
def thread_index(connection): def thread_index(connection, include_op=False):
""" """
Return a list with each thread, ordered by the date they Return a list with each thread, ordered by the date they
were last modifed (which could be when it was submitted were last modifed (which could be when it was submitted
@ -119,7 +120,7 @@ def thread_index(connection):
ORDER BY last_mod DESC""") ORDER BY last_mod DESC""")
threads = [ threads = [
thread_get(connection, obj[0], messages=False) thread_get(connection, obj[0], False, include_op)
for obj in c.fetchall() for obj in c.fetchall()
] ]
return threads return threads