diff --git a/ts/SignalProtocolStore.ts b/ts/SignalProtocolStore.ts index 3920965e7..cdabf1987 100644 --- a/ts/SignalProtocolStore.ts +++ b/ts/SignalProtocolStore.ts @@ -1948,12 +1948,6 @@ export class SignalProtocolStore extends EventsMixin { }); } - updateUnprocessedAttempts(id: string, attempts: number): Promise { - return this.withZone(GLOBAL_ZONE, 'updateUnprocessedAttempts', async () => { - await window.Signal.Data.updateUnprocessedAttempts(id, attempts); - }); - } - updateUnprocessedWithData( id: string, data: UnprocessedUpdateType diff --git a/ts/sql/Client.ts b/ts/sql/Client.ts index 61bf00764..7d8afe0c0 100644 --- a/ts/sql/Client.ts +++ b/ts/sql/Client.ts @@ -233,7 +233,6 @@ const dataInterface: ClientInterface = { getUnprocessedCount, getAllUnprocessed, getUnprocessedById, - updateUnprocessedAttempts, updateUnprocessedWithData, updateUnprocessedsWithData, removeUnprocessed, @@ -1422,9 +1421,6 @@ async function getUnprocessedById(id: string) { return channels.getUnprocessedById(id); } -async function updateUnprocessedAttempts(id: string, attempts: number) { - await channels.updateUnprocessedAttempts(id, attempts); -} async function updateUnprocessedWithData( id: string, data: UnprocessedUpdateType diff --git a/ts/sql/Interface.ts b/ts/sql/Interface.ts index 38838a42c..52f1a34e8 100644 --- a/ts/sql/Interface.ts +++ b/ts/sql/Interface.ts @@ -392,7 +392,6 @@ export type DataInterface = { getUnprocessedCount: () => Promise; getAllUnprocessed: () => Promise>; - updateUnprocessedAttempts: (id: string, attempts: number) => Promise; updateUnprocessedWithData: ( id: string, data: UnprocessedUpdateType diff --git a/ts/sql/Server.ts b/ts/sql/Server.ts index ba5e8f3c7..7ff0b690b 100644 --- a/ts/sql/Server.ts +++ b/ts/sql/Server.ts @@ -223,7 +223,6 @@ const dataInterface: ServerInterface = { getUnprocessedCount, getAllUnprocessed, - updateUnprocessedAttempts, updateUnprocessedWithData, updateUnprocessedsWithData, getUnprocessedById, @@ -5118,6 +5117,8 @@ async function getTapToViewMessagesNeedingErase(): Promise> { return rows.map(row => jsonToObject(row.json)); } +const MAX_UNPROCESSED_ATTEMPTS = 3; + function saveUnprocessedSync(data: UnprocessedType): string { const db = getInstance(); const { @@ -5137,6 +5138,11 @@ function saveUnprocessedSync(data: UnprocessedType): string { throw new Error('saveUnprocessedSync: id was falsey'); } + if (attempts >= MAX_UNPROCESSED_ATTEMPTS) { + removeUnprocessedSync(id); + return id; + } + prepare( db, ` @@ -5183,23 +5189,6 @@ function saveUnprocessedSync(data: UnprocessedType): string { return id; } -async function updateUnprocessedAttempts( - id: string, - attempts: number -): Promise { - const db = getInstance(); - db.prepare( - ` - UPDATE unprocessed - SET attempts = $attempts - WHERE id = $id; - ` - ).run({ - id, - attempts, - }); -} - function updateUnprocessedWithDataSync( id: string, data: UnprocessedUpdateType @@ -5299,7 +5288,7 @@ function removeUnprocessedsSync(ids: Array): void { ).run(ids); } -async function removeUnprocessed(id: string | Array): Promise { +function removeUnprocessedSync(id: string | Array): void { if (!Array.isArray(id)) { const db = getInstance(); @@ -5309,10 +5298,14 @@ async function removeUnprocessed(id: string | Array): Promise { } if (!id.length) { - throw new Error('removeUnprocessed: No ids to delete!'); + throw new Error('removeUnprocessedSync: No ids to delete!'); } - batchMultiVarQuery(id, removeUnprocessedsSync); + assertSync(batchMultiVarQuery(id, removeUnprocessedsSync)); +} + +async function removeUnprocessed(id: string | Array): Promise { + removeUnprocessedSync(id); } async function removeAllUnprocessed(): Promise { diff --git a/ts/textsecure/MessageReceiver.ts b/ts/textsecure/MessageReceiver.ts index 58b122f64..41d7fb050 100644 --- a/ts/textsecure/MessageReceiver.ts +++ b/ts/textsecure/MessageReceiver.ts @@ -706,30 +706,14 @@ export default class MessageReceiver const items = await this.storage.protocol.getAllUnprocessed(); log.info('getAllFromCache loaded', items.length, 'saved envelopes'); - return Promise.all( - map(items, async item => { - const attempts = 1 + (item.attempts || 0); + return items.map(item => { + const { attempts = 0 } = item; - try { - if (attempts >= 3) { - log.warn('getAllFromCache final attempt for envelope', item.id); - await this.storage.protocol.removeUnprocessed(item.id); - } else { - await this.storage.protocol.updateUnprocessedAttempts( - item.id, - attempts - ); - } - } catch (error) { - log.error( - 'getAllFromCache error updating item after load:', - Errors.toLogFormat(error) - ); - } - - return item; - }) - ); + return { + ...item, + attempts: attempts + 1, + }; + }); } private async decryptAndCacheBatch(