From 4148628e70fcc56e158f37e0d99f87eb4fd3e090 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Fri, 16 Nov 2018 10:03:43 +1100 Subject: [PATCH] Remove any unsent messages when app is started. --- app/sql.js | 15 +++++++++++++++ js/background.js | 13 ++++++++----- js/models/messages.js | 1 + js/modules/data.js | 7 +++++++ 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app/sql.js b/app/sql.js index e87df9130..79d51f3b5 100644 --- a/app/sql.js +++ b/app/sql.js @@ -96,6 +96,7 @@ module.exports = { getMessageById, getAllMessages, getAllMessageIds, + getAllUnsentMessages, getMessagesBySentAt, getExpiredMessages, getOutgoingWithoutExpiresAt, @@ -203,6 +204,7 @@ async function updateToSchemaVersion1(currentVersion, instance) { unread INTEGER, expires_at INTEGER, + sent BOOLEAN, sent_at INTEGER, schemaVersion INTEGER, conversationId STRING, @@ -1115,6 +1117,7 @@ async function saveMessage(data, { forceSave } = {}) { received_at, schemaVersion, // eslint-disable-next-line camelcase + sent, sent_at, source, sourceDevice, @@ -1137,6 +1140,7 @@ async function saveMessage(data, { forceSave } = {}) { $hasVisualMediaAttachments: hasVisualMediaAttachments, $received_at: received_at, $schemaVersion: schemaVersion, + $sent: sent, $sent_at: sent_at, $source: source, $sourceDevice: sourceDevice, @@ -1158,6 +1162,7 @@ async function saveMessage(data, { forceSave } = {}) { id = $id, received_at = $received_at, schemaVersion = $schemaVersion, + sent = $sent, sent_at = $sent_at, source = $source, sourceDevice = $sourceDevice, @@ -1189,6 +1194,7 @@ async function saveMessage(data, { forceSave } = {}) { hasVisualMediaAttachments, received_at, schemaVersion, + sent, sent_at, source, sourceDevice, @@ -1207,6 +1213,7 @@ async function saveMessage(data, { forceSave } = {}) { $hasVisualMediaAttachments, $received_at, $schemaVersion, + $sent, $sent_at, $source, $sourceDevice, @@ -1293,6 +1300,14 @@ async function getMessageBySender({ source, sourceDevice, sent_at }) { return map(rows, row => jsonToObject(row.json)); } +async function getAllUnsentMessages() { + const rows = await db.all(` + SELECT json FROM messages WHERE NOT sent + ORDER BY sent_at DESC; + `); + return map(rows, row => jsonToObject(row.json)); +} + async function getUnreadByConversation(conversationId) { const rows = await db.all( `SELECT json FROM messages WHERE diff --git a/js/background.js b/js/background.js index af12a9f45..1c1afbc9d 100644 --- a/js/background.js +++ b/js/background.js @@ -318,11 +318,14 @@ Views.Initialization.setMessage(window.i18n('optimizingApplication')); window.log.info('Cleanup: starting...'); - const messagesForCleanup = await window.Signal.Data.getOutgoingWithoutExpiresAt( - { - MessageCollection: Whisper.MessageCollection, - } - ); + const results = await Promise.all([ + window.Signal.Data.getOutgoingWithoutExpiresAt({ MessageCollection: Whisper.MessageCollection }), + window.Signal.Data.getAllUnsentMessages({ MessageCollection: Whisper.MessageCollection }), + ]); + + // Combine the models + const messagesForCleanup = results.reduce((array, current) => array.concat(current.toArray()), []); + window.log.info( `Cleanup: Found ${messagesForCleanup.length} messages for cleanup` ); diff --git a/js/models/messages.js b/js/models/messages.js index 3b58ca934..247d4fe32 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -94,6 +94,7 @@ timestamp: new Date().getTime(), attachments: [], pow: false, + sent: false, }; }, validate(attributes) { diff --git a/js/modules/data.js b/js/modules/data.js index 5d4bfac86..0123eafe4 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -133,6 +133,7 @@ module.exports = { getMessageBySender, getMessageById, getAllMessages, + getAllUnsentMessages, getAllMessageIds, getMessagesBySentAt, getExpiredMessages, @@ -810,6 +811,12 @@ async function getAllMessages({ MessageCollection }) { return new MessageCollection(encoded); } +async function getAllUnsentMessages({ MessageCollection }) { + const messages = await channels.getAllUnsentMessages(); + const encoded = messages.map(m => keysToArrayBuffer(MESSAGE_PRE_KEYS, m)); + return new MessageCollection(encoded); +} + async function getAllMessageIds() { const ids = await channels.getAllMessageIds(); return ids;