From a195f5e257ed39c14223ce3f4cb0606b4f0f9b12 Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sat, 4 May 2024 03:10:06 +1200 Subject: [PATCH] lxmf messages api should support filtering for specific conversations --- database.py | 5 ----- web.py | 26 +++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/database.py b/database.py index 783de15..c7dbc9e 100644 --- a/database.py +++ b/database.py @@ -24,11 +24,6 @@ class LxmfMessage(BaseModel): created_at = DateTimeField(default=datetime.now) updated_at = DateTimeField(default=datetime.now) - # override save to auto update updated_at when calling save() - def save(self, *args, **kwargs): - self.updated_at = datetime.now() - return super(LxmfMessage, self).save(*args, **kwargs) - # define database and table name class Meta: table_name = "lxmf_messages" diff --git a/web.py b/web.py index 9fd84c8..7e65203 100644 --- a/web.py +++ b/web.py @@ -161,9 +161,29 @@ class ReticulumWebChat: @routes.get("/api/v1/lxmf-messages") async def index(request): - # get lxmf messages from db + # get query params + source_hash = request.query.get("source_hash", None) + destination_hash = request.query.get("destination_hash", None) + + # source_hash is required + if source_hash is None: + return web.json_response({ + "message": "source_hash is required", + }, status=422) + + # destination_hash is required + if destination_hash is None: + return web.json_response({ + "message": "destination_hash is required", + }, status=422) + + # get lxmf messages from db where "source to destination" or "destination to source" + db_lxmf_messages = (database.LxmfMessage.select() + .where(database.LxmfMessage.source_hash == source_hash and database.LxmfMessage.destination_hash == destination_hash) + .orwhere(database.LxmfMessage.source_hash == destination_hash and database.LxmfMessage.destination_hash == source_hash)) + + # convert to response json lxmf_messages = [] - db_lxmf_messages = database.LxmfMessage.select() for db_lxmf_message in db_lxmf_messages: lxmf_messages.append({ "id": db_lxmf_message.id, @@ -521,7 +541,7 @@ class ReticulumWebChat: "progress": lxmf_message_dict["progress"], "content": lxmf_message_dict["content"], "fields": json.dumps(lxmf_message_dict["fields"]), - "updated_at": datetime.now(timezone.utc), + "updated_at": datetime.now(), } # upsert to database