From 4010373a7b79e2604101b730b09b6c40c4d4ba5c Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 21 Apr 2022 16:40:52 +1000 Subject: [PATCH] make sure we do not save more than one entry in the read_by Session has read by only for private chats, so we do not care about having more than one entry in read_by --- ts/models/messageType.ts | 4 ++-- ts/node/sql.ts | 16 ++++++++++++---- ts/util/readReceipts.ts | 9 +++++++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ts/models/messageType.ts b/ts/models/messageType.ts index f987e4cc6..d873d1942 100644 --- a/ts/models/messageType.ts +++ b/ts/models/messageType.ts @@ -18,7 +18,7 @@ export interface MessageAttributes { preview?: any; body?: string; expirationStartTimestamp: number; - read_by: Array; + read_by: Array; // we actually only care about the length of this. values are not used for anything expires_at?: number; type: MessageModelType; group_update?: MessageGroupUpdate; @@ -159,7 +159,7 @@ export interface MessageAttributesOptionals { preview?: any; body?: string; expirationStartTimestamp?: number; - read_by?: Array; + read_by?: Array; // we actually only care about the length of this. values are not used for anything expires_at?: number; type: MessageModelType; group_update?: MessageGroupUpdate; diff --git a/ts/node/sql.ts b/ts/node/sql.ts index 47df2b949..48e7d45d2 100644 --- a/ts/node/sql.ts +++ b/ts/node/sql.ts @@ -1323,9 +1323,7 @@ function updateToLokiSchemaVersion22(currentVersion: number, db: BetterSqlite3.D console.log(`updateToLokiSchemaVersion${targetVersion}: starting...`); db.transaction(() => { - db.exec(` - DROP INDEX messages_duplicate_check; - `); + db.exec(`DROP INDEX messages_duplicate_check;`); db.exec(` ALTER TABLE ${MESSAGES_TABLE} DROP sourceDevice; @@ -1340,6 +1338,15 @@ function updateToLokiSchemaVersion22(currentVersion: number, db: BetterSqlite3.D ); `); + dropFtsAndTriggers(db); + // we also want to remove the read_by it could have 20 times the same value set in the array + // we do this once, and updated the code to not allow multiple entries in read_by as we do not care about multiple entries + // (read_by is only used in private chats) + db.exec(` + UPDATE ${MESSAGES_TABLE} SET + json = json_remove(json, '$.schemaVersion', '$.recipients', '$.decrypted_at', '$.sourceDevice', '$.read_by') + `); + rebuildFtsTable(db); writeLokiSchemaVersion(targetVersion, db); })(); console.log(`updateToLokiSchemaVersion${targetVersion}: success!`); @@ -3445,7 +3452,6 @@ function cleanUpUnusedNodeForKeyEntries() { const swarmUnused = difference(allEntriesInSnodeForPubkey, allIdsToKeep); - console.log('swarmStored but unused ', swarmUnused.length); if (swarmUnused.length) { const start = Date.now(); @@ -3567,6 +3573,8 @@ function cleanUpOldOpengroups() { ); } + cleanUpMessagesJson(); + rebuildFtsTable(assertGlobalInstance()); } diff --git a/ts/util/readReceipts.ts b/ts/util/readReceipts.ts index e0fbdc13e..17226908a 100644 --- a/ts/util/readReceipts.ts +++ b/ts/util/readReceipts.ts @@ -42,12 +42,17 @@ async function onReadReceipt(receipt: { source: string; timestamp: number; readA return; } - const readBy = message.get('read_by') || []; + // readBy is only used for private conversations + // we do not care of who read it. If the length is > 0 , it is read and false otherwise + let readBy = message.get('read_by') || []; const expirationStartTimestamp = message.get('expirationStartTimestamp'); - if (!readBy.includes(receipt.source)) { + if (!readBy.length) { readBy.push(receipt.source); } + if (readBy.length > 1) { + readBy = readBy.slice(0, 1); + } message.set({ read_by: readBy, expirationStartTimestamp: expirationStartTimestamp || Date.now(),