fix: cleanup DaR unread messages after 14d

pull/3022/head
Audric Ackermann 1 year ago
parent 6e952398c9
commit 3d6e22f2fe

@ -104,6 +104,7 @@ const LOKI_SCHEMA_VERSIONS = [
updateToSessionSchemaVersion33, updateToSessionSchemaVersion33,
updateToSessionSchemaVersion34, updateToSessionSchemaVersion34,
updateToSessionSchemaVersion35, updateToSessionSchemaVersion35,
updateToSessionSchemaVersion36,
]; ];
function updateToSessionSchemaVersion1(currentVersion: number, db: BetterSqlite3.Database) { function updateToSessionSchemaVersion1(currentVersion: number, db: BetterSqlite3.Database) {
@ -1928,6 +1929,26 @@ function updateToSessionSchemaVersion35(currentVersion: number, db: BetterSqlite
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`); console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
} }
function updateToSessionSchemaVersion36(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 36;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`CREATE INDEX messages_DaR_unread_sent_at ON ${MESSAGES_TABLE} (
expirationType,
unread,
sent_at
);`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
export function printTableColumns(table: string, db: BetterSqlite3.Database) { export function printTableColumns(table: string, db: BetterSqlite3.Database) {
console.info(db.pragma(`table_info('${table}');`)); console.info(db.pragma(`table_info('${table}');`));
} }

@ -64,6 +64,7 @@ import { KNOWN_BLINDED_KEYS_ITEM, SettingsKey } from '../data/settings-key';
import { MessageAttributes } from '../models/messageType'; import { MessageAttributes } from '../models/messageType';
import { SignalService } from '../protobuf'; import { SignalService } from '../protobuf';
import { Quote } from '../receiver/types'; import { Quote } from '../receiver/types';
import { DURATION } from '../session/constants';
import { import {
getSQLCipherIntegrityCheck, getSQLCipherIntegrityCheck,
openAndMigrateDatabase, openAndMigrateDatabase,
@ -192,6 +193,7 @@ async function initializeSql({
console.info('total conversation count before cleaning: ', getConversationCount()); console.info('total conversation count before cleaning: ', getConversationCount());
cleanUpOldOpengroupsOnStart(); cleanUpOldOpengroupsOnStart();
cleanUpUnusedNodeForKeyEntriesOnStart(); cleanUpUnusedNodeForKeyEntriesOnStart();
cleanUpUnreadExpiredDaRMessages();
printDbStats(); printDbStats();
console.info('total message count after cleaning: ', getMessageCount()); console.info('total message count after cleaning: ', getMessageCount());
@ -1614,6 +1616,28 @@ function getExpiredMessages() {
return map(rows, row => jsonToObject(row.json)); return map(rows, row => jsonToObject(row.json));
} }
function cleanUpUnreadExpiredDaRMessages() {
// we cannot rely on network offset here, so we need to trust the user clock
const t14daysEarlier = Date.now() - 14 * DURATION.DAYS;
const start = Date.now();
const deleted = assertGlobalInstance()
.prepare(
`DELETE FROM ${MESSAGES_TABLE} WHERE
expirationType = 'deleteAfterRead' AND
unread = $unread AND
sent_at <= $t14daysEarlier;`
)
.run({
unread: toSqliteBoolean(true),
t14daysEarlier,
});
console.info(
`cleanUpUnreadExpiredDaRMessages: deleted ${
deleted.changes
} message(s) which were DaR and sent before ${t14daysEarlier} in ${Date.now() - start}ms`
);
}
function getOutgoingWithoutExpiresAt() { function getOutgoingWithoutExpiresAt() {
const rows = assertGlobalInstance() const rows = assertGlobalInstance()
.prepare( .prepare(

Loading…
Cancel
Save