delete one opengroupv1 message every 10 sec until there is no more

This commit is contained in:
Audric Ackermann 2021-06-11 15:14:52 +10:00
parent 9796f6fd52
commit 3f75fa54ad
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4
3 changed files with 49 additions and 28 deletions

View File

@ -113,6 +113,7 @@ module.exports = {
getAllV2OpenGroupRooms,
getV2OpenGroupRoomByRoomId,
removeV2OpenGroupRoom,
removeOneOpenGroupV1Message,
};
const CONVERSATIONS_TABLE = 'conversations';
@ -1138,6 +1139,7 @@ async function updateToLokiSchemaVersion14(currentVersion, instance) {
return;
}
console.log(`updateToLokiSchemaVersion${targetVersion}: starting...`);
await instance.run('BEGIN TRANSACTION;');
await instance.run('DROP TABLE IF EXISTS servers;');
await instance.run('DROP TABLE IF EXISTS sessions;');
@ -1147,31 +1149,6 @@ async function updateToLokiSchemaVersion14(currentVersion, instance) {
await instance.run('DROP TABLE IF EXISTS signedPreKeys;');
await instance.run('DROP TABLE IF EXISTS senderKeys;');
console.time('removingOpengroupv1Messages');
let toRemoveCount = 0;
do {
// eslint-disable-next-line no-await-in-loop
const row = await instance.get(`SELECT count(*) from ${MESSAGES_TABLE} WHERE
conversationId LIKE 'publicChat:1@%';`);
toRemoveCount = row['count(*)'];
if (toRemoveCount > 0) {
console.warn('toRemove count', toRemoveCount);
console.time('chunk');
// eslint-disable-next-line no-await-in-loop
await instance.all(
`DELETE FROM ${MESSAGES_TABLE} WHERE
conversationId LIKE 'publicChat:1@%'
;`
);
console.timeEnd('chunk');
}
} while (toRemoveCount > 0);
console.timeEnd('removingOpengroupv1Messages');
await instance.run(
`INSERT INTO loki_schema (
version
@ -1949,9 +1926,9 @@ async function saveMessages(arrayOfMessages) {
await promise;
}
async function removeMessage(id) {
async function removeMessage(id, instance) {
if (!Array.isArray(id)) {
await db.run(`DELETE FROM ${MESSAGES_TABLE} WHERE id = $id;`, { $id: id });
await (db || instance).run(`DELETE FROM ${MESSAGES_TABLE} WHERE id = $id;`, { $id: id });
return;
}
@ -1960,7 +1937,7 @@ async function removeMessage(id) {
}
// Our node interface doesn't seem to allow you to replace one single ? with an array
await db.run(
await (db || instance).run(
`DELETE FROM ${MESSAGES_TABLE} WHERE id IN ( ${id.map(() => '?').join(', ')} );`,
id
);
@ -2849,3 +2826,28 @@ async function removeV2OpenGroupRoom(conversationId) {
$conversationId: conversationId,
});
}
async function removeOneOpenGroupV1Message() {
// eslint-disable-next-line no-await-in-loop
const row = await db.get(`SELECT count(*) from ${MESSAGES_TABLE} WHERE
conversationId LIKE 'publicChat:1@%';`);
const toRemoveCount = row['count(*)'];
if (toRemoveCount <= 0) {
return 0;
}
console.warn('left opengroupv1 message to remove: ', toRemoveCount);
const rowMessageIds = await db.all(
`SELECT id from ${MESSAGES_TABLE} WHERE conversationId LIKE 'publicChat:1@%' ORDER BY id LIMIT 1;`
);
const messagesIds = map(rowMessageIds, r => r.id)[0];
console.time('removeOneOpenGroupV1Message');
// eslint-disable-next-line no-await-in-loop
await removeMessage(messagesIds);
console.timeEnd('removeOneOpenGroupV1Message');
return toRemoveCount - 1;
}

View File

@ -15,6 +15,7 @@ import {
hasSyncedInitialConfigurationItem,
lastAvatarUploadTimestamp,
removeConversation,
removeOneOpenGroupV1Message,
} from '../../data/data';
import { OnionPaths } from '../../session/onions';
import { getMessageQueue } from '../../session/sending';
@ -160,6 +161,16 @@ const triggerSyncIfNeeded = async () => {
}
};
const scheduleDeleteOpenGroupV1Messages = async () => {
const leftToRemove = await removeOneOpenGroupV1Message();
if (leftToRemove > 0) {
window?.log?.info(`We still have ${leftToRemove} opengroupv1 messages to remove...`);
setTimeout(scheduleDeleteOpenGroupV1Messages, 10000);
} else {
window?.log?.info('No more opengroupv1 messages to remove...');
}
};
const removeAllV1OpenGroups = async () => {
const allV1Convos = (await getAllOpenGroupV1Conversations()).models || [];
// do not remove messages of opengroupv1 for now. We have to find a way of doing it without making the whole app extremely slow
@ -180,6 +191,8 @@ const removeAllV1OpenGroups = async () => {
window.log.warn(`failed to delete opengroupv1 ${v1Convo.id}`, e);
}
}
setTimeout(scheduleDeleteOpenGroupV1Messages, 10000);
};
const triggerAvatarReUploadIfNeeded = async () => {

View File

@ -156,6 +156,7 @@ const channelsToMake = {
addClosedGroupEncryptionKeyPair,
isKeyPairAlreadySaved,
removeAllClosedGroupEncryptionKeyPairs,
removeOneOpenGroupV1Message,
// open group v2
...channelstoMakeOpenGroupV2,
@ -947,3 +948,8 @@ export async function getSnodePoolFromDb(): Promise<Array<Snode> | null> {
export async function updateSnodePoolOnDb(snodesAsJsonString: string): Promise<void> {
await exports.createOrUpdateItem({ id: SNODE_POOL_ITEM_ID, value: snodesAsJsonString });
}
/** Returns the number of message left to remove (opengroupv1) */
export async function removeOneOpenGroupV1Message(): Promise<number> {
return channels.removeOneOpenGroupV1Message();
}