centralize formatting routines; add message_feed to network client

pull/4/head
Blake DeMarcy 2017-04-26 15:35:54 -05:00
parent 33b5912937
commit 8e2b2efdef
3 changed files with 68 additions and 11 deletions

View File

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

View File

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

View File

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