From 28639fb6f89c7573918ddc0550d8f1a29a4619e3 Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sat, 1 Jun 2024 17:36:43 +1200 Subject: [PATCH] implement api to fetch unique lxmf conversations --- web.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/web.py b/web.py index df9048f..2e979a4 100644 --- a/web.py +++ b/web.py @@ -801,6 +801,56 @@ class ReticulumWebChat: "message": "ok", }) + # get unqiue lxmf conversations + @routes.get("/api/v1/lxmf/conversations") + async def index(request): + + # sql query to fetch unique source/destination hash pairs ordered by the most recently updated message + query = """ + WITH NormalizedMessages AS ( + SELECT + CASE WHEN source_hash < destination_hash THEN source_hash ELSE destination_hash END AS normalized_source, + CASE WHEN source_hash < destination_hash THEN destination_hash ELSE source_hash END AS normalized_destination, + MAX(updated_at) AS most_recent_updated_at + FROM lxmf_messages + GROUP BY normalized_source, normalized_destination + ) + SELECT + normalized_source AS source_hash, + normalized_destination AS destination_hash, + most_recent_updated_at + FROM NormalizedMessages + ORDER BY most_recent_updated_at DESC; + """ + + # execute sql query + cursor = database.database.execute_sql(query) + + # parse results to get a list of conversations we have sent or received a message from + conversations = [] + for row in cursor.fetchall(): + + # get data from row + source_hash = row[0] + destination_hash = row[1] + updated_at = row[2] + + # determine destination hash of other user + if source_hash == self.local_lxmf_destination.hexhash: + other_user_hash = destination_hash + else: + other_user_hash = source_hash + + # add to conversations + conversations.append({ + "destination_hash": other_user_hash, + "updated_at": updated_at, + }) + + return web.json_response({ + "conversations": conversations, + }) + # called when web app has started async def on_startup(app):