refactor api endpoint for fetching messages

This commit is contained in:
liamcottle
2024-05-05 20:23:48 +12:00
parent ae2901a05d
commit f14c5929a5
2 changed files with 24 additions and 33 deletions

View File

@@ -789,12 +789,7 @@
try {
// fetch lxmf messages from "us to destination" and from "destination to us"
const response = await window.axios.get('/api/v1/lxmf-messages', {
params: {
source_hash: this.config.lxmf_address_hash,
destination_hash: destinationHash,
},
});
const response = await window.axios.get(`/api/v1/lxmf-messages/conversation/${destinationHash}`);
// do nothing if response is for a previous request
if(seq !== this.lxmfMessagesRequestSequence){

50
web.py
View File

@@ -143,19 +143,35 @@ class ReticulumWebChat:
return websocket_response
# serve lxmf messages
@routes.get("/api/v1/lxmf-messages")
@routes.delete("/api/v1/lxmf-messages/{id}")
async def index(request):
# get query params
source_hash = request.query.get("source_hash", None)
destination_hash = request.query.get("destination_hash", None)
# get path params
id = request.match_info.get("id", None)
# source_hash is required
if source_hash is None:
if id is None:
return web.json_response({
"message": "source_hash is required",
"message": "id is required",
}, status=422)
# delete lxmf messages from db where id matches
database.LxmfMessage.delete_by_id(id)
return web.json_response({
"message": "ok",
})
# serve lxmf messages
@routes.get("/api/v1/lxmf-messages/conversation/{destination_hash}")
async def index(request):
# get path params
destination_hash = request.match_info.get("destination_hash", None)
# get source hash from local lxmf destination
source_hash = self.local_lxmf_destination.hash.hex()
# destination_hash is required
if destination_hash is None:
return web.json_response({
@@ -192,26 +208,6 @@ class ReticulumWebChat:
"lxmf_messages": lxmf_messages,
})
# serve lxmf messages
@routes.delete("/api/v1/lxmf-messages/{id}")
async def index(request):
# get path params
id = request.match_info.get("id", None)
# source_hash is required
if id is None:
return web.json_response({
"message": "id is required",
}, status=422)
# delete lxmf messages from db where id matches
database.LxmfMessage.delete_by_id(id)
return web.json_response({
"message": "ok",
})
# delete lxmf messages for conversation
@routes.delete("/api/v1/lxmf-messages/conversation/{destination_hash}")
async def index(request):
@@ -222,7 +218,7 @@ class ReticulumWebChat:
# get source hash from local lxmf destination
source_hash = self.local_lxmf_destination.hash.hex()
# source_hash is required
# destination_hash is required
if destination_hash is None:
return web.json_response({
"message": "destination_hash is required",