diff --git a/src/components/messages/MessageViewer.vue b/src/components/messages/MessageViewer.vue index ae1f5eb..f96ef2c 100644 --- a/src/components/messages/MessageViewer.vue +++ b/src/components/messages/MessageViewer.vue @@ -33,7 +33,7 @@
- Failed + Failed: {{ message.error }} Delivered Sending
diff --git a/src/js/Connection.js b/src/js/Connection.js index 498b295..6d7e336 100644 --- a/src/js/Connection.js +++ b/src/js/Connection.js @@ -175,8 +175,13 @@ class Connection { // send message const message = await GlobalState.connection.sendTextMessage(publicKey, text); + // mark message as failed after estimated timeout + setTimeout(async () => { + await Database.Message.setMessageFailedByAckCode(message.expectedAckCrc, "timeout"); + }, message.estTimeout); + // save to database - return await Database.Message.insert({ + await Database.Message.insert({ status: "sending", to: publicKey, from: GlobalState.selfInfo.publicKey, diff --git a/src/js/Database.js b/src/js/Database.js index 857d247..4ab0962 100644 --- a/src/js/Database.js +++ b/src/js/Database.js @@ -112,7 +112,7 @@ class Message { // find one latest message by ack code // this will prevent updating older messages that might have the same ack code - const latestMessageByPacketId = database.messages.findOne({ + const message = database.messages.findOne({ selector: { expected_ack_crc: { $eq: ackCode, @@ -126,12 +126,48 @@ class Message { }); // patch the message state - return await latestMessageByPacketId.incrementalPatch({ + return await message.incrementalPatch({ status: "delivered", }); } + // mark a message as failed by its ack code + static async setMessageFailedByAckCode(ackCode, reason) { + + // find one latest message by ack code + // this will prevent updating older messages that might have the same ack code + const message = await database.messages.findOne({ + selector: { + expected_ack_crc: { + $eq: ackCode, + }, + }, + sort: [ + { + timestamp: "desc", + }, + ], + }).exec(); + + // do nothing if message not found + if(!message){ + return; + } + + // do nothing if already delivered + if(message.status === "delivered"){ + return; + } + + // patch the message state + await message.patch({ + status: "failed", + error: reason, + }); + + } + // get all messages static getAllMessages() { return database.messages.find();