feat: remove legacy from expirationType during db migration

fix migrations to work with user config changes
pull/2861/head
William Grant 2 years ago
parent 7342488689
commit 2a6d8a6544

@ -142,6 +142,7 @@ export const OverlayDisappearingMessages = ({ unlockNewModes }: { unlockNewModes
defaultExpirationType = 'deleteAfterSend';
}
convo.set('expirationType', defaultExpirationType);
// TODO do we need to add libsession stuff here probably not?
setModeSelected(defaultExpirationType);
}
}

@ -865,6 +865,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
lastDisappearingMessageChangeTimestamp,
source,
});
let message: MessageModel | undefined = existingMessage || undefined;
// we don't have info about who made the change and when, when we get a change from a config message, so do not add a control message

@ -34,7 +34,6 @@ import {
} from '../database_utility';
import { getIdentityKeys, sqlNode } from '../sql';
import { FEATURE_RELEASE_TIMESTAMPS } from '../../session/constants';
import { sleepFor } from '../../session/utils/Promise';
const hasDebugEnvVariable = Boolean(process.env.SESSION_DEBUG);
@ -1582,6 +1581,15 @@ function updateToSessionSchemaVersion30(currentVersion: number, db: BetterSqlite
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
/**
* Get's the user's private and public keys from the database
* @param db the database
* @returns the keys { privateEd25519: string, publicEd25519: string }
*/
function getOurAccountKeys(db: BetterSqlite3.Database) {
const keys = getIdentityKeys(db);
return keys;
}
function updateToSessionSchemaVersion31(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 31;
@ -1594,7 +1602,7 @@ function updateToSessionSchemaVersion31(currentVersion: number, db: BetterSqlite
// In the migration 30, we made all the changes which didn't require the user to be logged in yet.
// in this one, we check if a user is logged in, and if yes we build and save the config dumps for the current state of the database.
try {
const keys = getIdentityKeys(db);
const keys = getOurAccountKeys(db);
const userAlreadyCreated = !!keys && !isEmpty(keys.privateEd25519);
@ -1866,52 +1874,62 @@ function updateToSessionSchemaVersion33(currentVersion: number, db: BetterSqlite
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
// Conversation changes
db.prepare(
`ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN expirationType TEXT DEFAULT "off";`
).run();
try {
const keys = getOurAccountKeys(db);
db.prepare(
`ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN lastDisappearingMessageChangeTimestamp INTEGER DEFAULT 0;`
).run();
const userAlreadyCreated = !!keys && !isEmpty(keys.privateEd25519);
db.prepare(`ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN hasOutdatedClient TEXT;`).run();
if (!userAlreadyCreated) {
throw new Error('privateEd25519 was empty. Considering no users are logged in');
}
const { publicKeyHex } = keys;
// support disppearing messages legacy mode until after the platform agreed timestamp
if (Date.now() < FEATURE_RELEASE_TIMESTAMPS.DISAPPEARING_MESSAGES_V2) {
// Conversation changes
// TODO can this be moved into libsession completely
db.prepare(
`ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN expirationType TEXT DEFAULT "off";`
).run();
db.prepare(
`ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN lastDisappearingMessageChangeTimestamp INTEGER DEFAULT 0;`
).run();
db.prepare(`ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN hasOutdatedClient TEXT;`).run();
// Note to Self
db.prepare(
`UPDATE ${CONVERSATIONS_TABLE} SET
expirationType = $expirationType
WHERE expireTimer > 0;`
).run({ expirationType: 'legacy' });
} else {
WHERE id = $id AND type = 'private' AND expireTimer > 0;`
).run({ expirationType: 'deleteAfterSend', id: publicKeyHex });
// Private Conversations
db.prepare(
`UPDATE ${CONVERSATIONS_TABLE} SET
expirationType = $expirationType
WHERE type = 'private' AND expireTimer > 0;`
WHERE type = 'private' AND expirationType = 'off' AND expireTimer > 0;`
).run({ expirationType: 'deleteAfterRead' });
// TODO Audric update this to support model changes for closed groups
// Groups
db.prepare(
`UPDATE ${CONVERSATIONS_TABLE} SET
expirationType = $expirationType
WHERE type = 'group' AND is_medium_group = 1 AND expireTimer > 0;`
WHERE type = 'group' AND id LIKE '05%' AND expireTimer > 0;`
).run({ expirationType: 'deleteAfterSend' });
}
// TODO After testing -> rename expireTimer column to expirationTimer everywhere.
// Update Conversation Model expireTimer calls everywhere
// db.exec(
// `ALTER TABLE ${CONVERSATIONS_TABLE} RENAME COLUMN expireTimer TO expirationTimer;`
// );
// Message changes
db.prepare(`ALTER TABLE ${MESSAGES_TABLE} ADD COLUMN expirationType TEXT;`).run();
// Message changes
db.prepare(`ALTER TABLE ${MESSAGES_TABLE} ADD COLUMN expirationType TEXT;`).run();
} catch (e) {
console.error(
`Failed to migrate to disappearing messages v2. Might just not have a logged in user yet? `,
e.message,
e.stack,
e
);
// if we get an exception here, most likely no users are logged in yet. We can just continue the transaction and the wrappers will be created when a user creates a new account.
}
// TODO After testing -> rename expireTimer column to expirationTimer everywhere.
// db.exec(
// `ALTER TABLE ${MESSAGES_TABLE} RENAME COLUMN expireTimer TO expirationTimer;`
// );
writeSessionSchemaVersion(targetVersion, db);
})();

@ -55,8 +55,10 @@ export async function destroyMessagesAndUpdateRedux(
// Delete any attachments
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < messageIds.length; i++) {
/* eslint-disable no-await-in-loop */
const message = await Data.getMessageById(messageIds[i]);
await message?.cleanup();
/* eslint-enable no-await-in-loop */
}
// Delete all those messages in a single sql call
@ -241,7 +243,7 @@ export function setExpirationStartTimestamp(
window.log.debug(
`We compare 2 timestamps for a disappear ${
isLegacyMode ? 'legacy' : mode === 'deleteAfterRead' ? 'after read' : 'after send'
} message: \expirationStartTimestamp `,
} message: expirationStartTimestamp `,
new Date(expirationStartTimestamp).toLocaleTimeString(),
'\ntimestamp ',
new Date(timestamp).toLocaleTimeString()
@ -424,12 +426,13 @@ export async function checkHasOutdatedClient(
});
}
await convoToUpdate.commit();
} else {
if (expireUpdate.isLegacyDataMessage || expireUpdate.isLegacyConversationSettingMessage) {
convoToUpdate.set({
hasOutdatedClient: outdatedSender,
});
await convoToUpdate.commit();
}
return;
}
if (expireUpdate.isLegacyDataMessage || expireUpdate.isLegacyConversationSettingMessage) {
convoToUpdate.set({
hasOutdatedClient: outdatedSender,
});
await convoToUpdate.commit();
}
}

Loading…
Cancel
Save