From e673706ae52460507040e83a3428185eb53996bf Mon Sep 17 00:00:00 2001 From: Blake DeMarcy Date: Sat, 29 Apr 2017 20:48:38 -0500 Subject: [PATCH] add option to include ops with index (or only ops with gets) --- clients/network_client.py | 9 +++++---- server.py | 13 +++++++++++-- src/db.py | 15 ++++++++------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/clients/network_client.py b/clients/network_client.py index 5ce7975..8278ae4 100644 --- a/clients/network_client.py +++ b/clients/network_client.py @@ -424,7 +424,7 @@ class BBJ(object): 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 most recently interacted, and [1] is a usermap object. @@ -435,11 +435,11 @@ class BBJ(object): author_id = thread["author"] print(usermap[author_id]["user_name"]) """ - response = self("thread_index") + response = self("thread_index", include_op=include_op) 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. @@ -450,7 +450,8 @@ class BBJ(object): print(usermap[author_id]["user_name"]) 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"] diff --git a/server.py b/server.py index bd72615..3f65364 100644 --- a/server.py +++ b/server.py @@ -241,8 +241,13 @@ class API(object): """ Return an array with all the threads, ordered by most recent activity. 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) return threads @@ -328,9 +333,13 @@ class API(object): Requires the argument `thread_id`. `format` may also be specified as a formatter to run the messages through. 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"]) - 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 = \ create_usermap(database, thread["messages"]) do_formatting(args.get("format"), thread["messages"]) diff --git a/src/db.py b/src/db.py index 1c1aab2..ff4fc3e 100644 --- a/src/db.py +++ b/src/db.py @@ -79,7 +79,7 @@ def message_feed(connection, time): ### 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 elsewhere. @@ -96,17 +96,18 @@ def thread_get(connection, thread_id, messages=True): raise BBJParameterError("Thread does not exist.") thread = schema.thread(*thread) - if messages: - c.execute(""" - SELECT * FROM messages WHERE thread_id = ? - ORDER BY post_id""", (thread_id,)) + if messages or op_only: + query = "SELECT * FROM messages WHERE thread_id = ? %s" + c.execute(query % ( + "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] thread["messages"] = [schema.message(*values) for values in c.fetchall()] return thread -def thread_index(connection): +def thread_index(connection, include_op=False): """ Return a list with each thread, ordered by the date they were last modifed (which could be when it was submitted @@ -119,7 +120,7 @@ def thread_index(connection): ORDER BY last_mod DESC""") threads = [ - thread_get(connection, obj[0], messages=False) + thread_get(connection, obj[0], False, include_op) for obj in c.fetchall() ] return threads