Add API to get recent chat messages

This commit is contained in:
swamp 2022-02-28 18:58:51 +00:00 committed by j
parent 2efcdb0624
commit 95dcdea1c1

11
chat.py
View file

@ -136,5 +136,16 @@ def messages_post():
return "OK" return "OK"
# make sure only messages from the last hour are sent
# this command also only sends messages the user doesn't have yet.
@app.route('/<last_index>/api.json')
def api(last_index):
curr_time = datetime.now()
ci = len(messages)-1
while curr_time - messages[ci].time < datetime.timedelta(hours = 1) and ci > last_index:
ci -= 1
return jsonify({"newest_msg_index": len(messages) , "messages": messages[ci:]})
if __name__ == "__main__": if __name__ == "__main__":
socketio.run(app) socketio.run(app)