centralize formatting routines; add message_feed to network client
parent
33b5912937
commit
8e2b2efdef
|
@ -586,3 +586,43 @@ class BBJ(object):
|
||||||
assert self.get_me()["is_admin"]
|
assert self.get_me()["is_admin"]
|
||||||
response = self("set_thread_pin", thread_id=pinned, pinned=new_status)
|
response = self("set_thread_pin", thread_id=pinned, pinned=new_status)
|
||||||
return response["data"]
|
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"]
|
||||||
|
}
|
||||||
|
|
36
server.py
36
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):
|
def validate(json, args):
|
||||||
"""
|
"""
|
||||||
Ensure the json object contains all the keys needed to satisfy
|
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
|
first. The order in the threads object is undefined and you should
|
||||||
instead use their `last_mod` attribute if you intend to list them
|
instead use their `last_mod` attribute if you intend to list them
|
||||||
out visually.
|
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"])
|
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
|
@api_method
|
||||||
|
@ -287,9 +311,7 @@ class API(object):
|
||||||
thread = db.thread_get(database, args["thread_id"])
|
thread = db.thread_get(database, args["thread_id"])
|
||||||
cherrypy.thread_data.usermap = \
|
cherrypy.thread_data.usermap = \
|
||||||
create_usermap(database, thread["messages"])
|
create_usermap(database, thread["messages"])
|
||||||
if args.get("format") == "sequential":
|
do_formatting(args.get("format"), thread["messages"])
|
||||||
formatting.apply_formatting(thread["messages"],
|
|
||||||
formatting.sequential_expressions)
|
|
||||||
return thread
|
return thread
|
||||||
|
|
||||||
|
|
||||||
|
@ -405,11 +427,7 @@ class API(object):
|
||||||
"""
|
"""
|
||||||
validate(args, ["format", "body"])
|
validate(args, ["format", "body"])
|
||||||
message = [{"body": args["body"]}]
|
message = [{"body": args["body"]}]
|
||||||
if args["format"] == "sequential":
|
do_formatting(args["format"], message)
|
||||||
formatter = formatting.sequential_expressions
|
|
||||||
else:
|
|
||||||
raise BBJParameterError("invalid format directive.")
|
|
||||||
formatting.apply_formatting(message, formatter)
|
|
||||||
return message[0]["body"]
|
return message[0]["body"]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -73,11 +73,10 @@ def message_feed(connection, time):
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"threads": threads,
|
"threads": threads,
|
||||||
"messages": sorted(messages, key=lambda m: m["created"])
|
"messages": sorted(messages, key=lambda m: m["created"], reverse=True)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### THREADS ###
|
### THREADS ###
|
||||||
|
|
||||||
def thread_get(connection, thread_id, messages=True):
|
def thread_get(connection, thread_id, messages=True):
|
||||||
|
|
Loading…
Reference in New Issue