Optimize markAllCallHistoryReadWithPredicate

Co-authored-by: Jamie Kyle <113370520+jamiebuilds-signal@users.noreply.github.com>
This commit is contained in:
automated-signal 2024-06-26 20:22:32 -05:00 committed by GitHub
parent 3ecd3333f2
commit 481238ad9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3851,28 +3851,22 @@ async function markAllCallHistoryReadWithPredicate(
? sqlFragment`callsHistory.peerId IS ${target.peerId}`
: sqlFragment`TRUE`;
const [selectQuery, selectParams] = sql`
SELECT callsHistory.callId
FROM callsHistory
WHERE ${predicate}
AND callsHistory.timestamp <= ${timestamp}
const [updateQuery, updateParams] = sql`
UPDATE messages
SET
seenStatus = ${SEEN_STATUS_SEEN},
json = json_patch(json, ${jsonPatch})
WHERE id IN (
SELECT id FROM messages
INNER JOIN callsHistory ON callsHistory.callId IS messages.callId
WHERE messages.type IS 'call-history'
AND messages.seenStatus IS ${SEEN_STATUS_UNSEEN}
AND callsHistory.timestamp <= ${timestamp}
AND ${predicate}
)
`;
const callIds = db.prepare(selectQuery).pluck().all(selectParams);
batchMultiVarQuery(db, callIds, ids => {
const idList = sqlJoin(ids.map(id => sqlFragment`${id}`));
const [updateQuery, updateParams] = sql`
UPDATE messages
SET
seenStatus = ${SEEN_STATUS_SEEN},
json = json_patch(json, ${jsonPatch})
WHERE callId IN (${idList});
`;
db.prepare(updateQuery).run(updateParams);
});
db.prepare(updateQuery).run(updateParams);
})();
}