From 8e2b2efdefd21933a37fe8e842a2af55aa5cbfcc Mon Sep 17 00:00:00 2001 From: Blake DeMarcy Date: Wed, 26 Apr 2017 15:35:54 -0500 Subject: [PATCH] centralize formatting routines; add message_feed to network client --- clients/network_client.py | 40 +++++++++++++++++++++++++++++++++++++++ server.py | 36 ++++++++++++++++++++++++++--------- src/db.py | 3 +-- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/clients/network_client.py b/clients/network_client.py index 2fd17e5..5ce7975 100644 --- a/clients/network_client.py +++ b/clients/network_client.py @@ -586,3 +586,43 @@ class BBJ(object): assert self.get_me()["is_admin"] response = self("set_thread_pin", thread_id=pinned, pinned=new_status) return response["data"] + + + def message_feed(self, time, format=None): + """ + Returns a special object representing all activity on the board since + the argument `time`, a unix/epoch timestamp. + + { + "threads": { + "thread_id": { + ...thread object + }, + ...more thread_id/object pairs + }, + "messages": [...standard message object array sorted by date], + "usermap": { + ...standard user_id mapping object + } + } + + The message objects in "messages" are the same objects returned + in threads normally. They each have a thread_id parameter, and + you can access metadata for these threads by the "threads" object + which is also provided. All user_ids can be resolved into full user + objects from the usermap object. + + The "messages" array is already sorted by submission time, newest + first. The order in the threads object is undefined and you should + instead use their `last_mod` attribute if you intend to list them + out visually. + + the optional argument `format` can be given and bahaves the same + as `thread_load`. + """ + response = self("message_feed", time=time, format=format) + return { + "usermap": response["usermap"], + "threads": response["data"]["threads"], + "messages": response["data"]["messages"] + } diff --git a/server.py b/server.py index cb9a4f8..34f7e4a 100644 --- a/server.py +++ b/server.py @@ -106,6 +106,20 @@ def create_usermap(connection, obj, index=False): } +def do_formatting(format_spec, messages): + if not format_spec: + return None + + elif format_spec == "sequential": + method = formatting.sequential_expressions + + else: + raise BBJParameterError("invalid formatter specification") + + formatting.apply_formatting(messages, method) + return True + + def validate(json, args): """ Ensure the json object contains all the keys needed to satisfy @@ -236,10 +250,20 @@ class API(object): first. The order in the threads object is undefined and you should instead use their `last_mod` attribute if you intend to list them out visually. + + You may optionally provide a `format` argument: this is treated + the same way as the `thread_load` endpoint and you should refer + to its documentation for more info. """ validate(args, ["time"]) - return db.message_feed(database, args["time"]) + feed = db.message_feed(database, args["time"]) + _map = create_usermap(database, feed["messages"]) + _map.update(create_usermap(database, feed["threads"].values(), True)) + cherrypy.thread_data.usermap.update(_map) + + do_formatting(args.get("format"), feed["messages"]) + return feed @api_method @@ -287,9 +311,7 @@ class API(object): thread = db.thread_get(database, args["thread_id"]) cherrypy.thread_data.usermap = \ create_usermap(database, thread["messages"]) - if args.get("format") == "sequential": - formatting.apply_formatting(thread["messages"], - formatting.sequential_expressions) + do_formatting(args.get("format"), thread["messages"]) return thread @@ -405,11 +427,7 @@ class API(object): """ validate(args, ["format", "body"]) message = [{"body": args["body"]}] - if args["format"] == "sequential": - formatter = formatting.sequential_expressions - else: - raise BBJParameterError("invalid format directive.") - formatting.apply_formatting(message, formatter) + do_formatting(args["format"], message) return message[0]["body"] diff --git a/src/db.py b/src/db.py index ebb205d..37dd159 100644 --- a/src/db.py +++ b/src/db.py @@ -73,11 +73,10 @@ def message_feed(connection, time): return { "threads": threads, - "messages": sorted(messages, key=lambda m: m["created"]) + "messages": sorted(messages, key=lambda m: m["created"], reverse=True) } - ### THREADS ### def thread_get(connection, thread_id, messages=True):