diff --git a/app/sql.js b/app/sql.js index a448be12a..64cec5cb0 100644 --- a/app/sql.js +++ b/app/sql.js @@ -394,9 +394,8 @@ async function updateToSchemaVersion6(currentVersion, instance) { await instance.run( `CREATE TABLE seenMessages( - id STRING PRIMARY KEY ASC, - expiresAt INTEGER, - hash STRING + hash STRING PRIMARY KEY, + expiresAt INTEGER );` ); @@ -1256,15 +1255,12 @@ async function saveSeenMessageHash(data) { } = data; await db.run( `INSERT INTO seenMessages ( - id, expiresAt, hash ) values ( - $id, $expiresAt, $hash );`, { - $id: generateUUID(), $expiresAt: expiresAt, $hash: hash, } diff --git a/libtextsecure/http-resources.js b/libtextsecure/http-resources.js index 08934dc39..576ae98d9 100644 --- a/libtextsecure/http-resources.js +++ b/libtextsecure/http-resources.js @@ -40,6 +40,17 @@ }; }; + const filterIncomingMessages = async function filterIncomingMessages(messages) { + const incomingHashes = messages.map(m => m.hash); + const dupHashes = await window.Signal.Data.getSeenMessagesByHashList(incomingHashes); + const newMessages = messages.filter(m => !dupHashes.includes(m.hash)); + const newHashes = newMessages.map(m => ({ + expiresAt: m.expiration, + hash: m.hash, + })); + await window.Signal.Data.saveSeenMessageHashes(newHashes); + return newMessages; + }; window.HttpResource = function HttpResource(_server, opts = {}) { server = _server; @@ -68,19 +79,8 @@ setTimeout(() => { pollServer(callBack); }, 5000); return; } - const incomingHashes = result.messages.map(m => m.hash); - const dupHashes = await window.Signal.Data.getSeenMessagesByHashList(incomingHashes); - if (incomingHashes.length === dupHashes.length) { - setTimeout(() => { pollServer(callBack); }, 5000); - return; - } - const NewMessages = result.messages.filter(m => !dupHashes.includes(m.hash)); - const NewHashes = NewMessages.map(m => ({ - expiresAt: m.expiration, - hash: m.hash, - })); - await window.Signal.Data.saveMessageHashes(NewHashes); - NewMessages.forEach(async message => { + const newMessages = await filterIncomingMessages(result.messages); + newMessages.forEach(async message => { const { data } = message; const dataPlaintext = stringToArrayBufferBase64(data); const messageBuf = textsecure.protobuf.WebSocketMessage.decode(dataPlaintext);