From 95dcdea1c1f713e423884182344767fd744fce67 Mon Sep 17 00:00:00 2001 From: swamp <108-swamp@users.noreply.git.zeus.gent> Date: Mon, 28 Feb 2022 18:58:51 +0000 Subject: [PATCH] Add API to get recent chat messages --- chat.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/chat.py b/chat.py index 7ca3271..db83196 100644 --- a/chat.py +++ b/chat.py @@ -136,5 +136,16 @@ def messages_post(): 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('//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__": socketio.run(app)