lxmf messages api should support filtering for specific conversations

This commit is contained in:
liamcottle
2024-05-04 03:10:06 +12:00
parent db90498d03
commit a195f5e257
2 changed files with 23 additions and 8 deletions

View File

@@ -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"

26
web.py
View File

@@ -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