add migration to drop existing textsecure prefix from group convo ids

This commit is contained in:
Audric Ackermann 2020-09-22 08:58:21 +10:00
parent 86b15328c1
commit 4505bed61b
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4
2 changed files with 61 additions and 1 deletions

View File

@ -812,6 +812,7 @@ const LOKI_SCHEMA_VERSIONS = [
updateToLokiSchemaVersion6,
updateToLokiSchemaVersion7,
updateToLokiSchemaVersion8,
updateToLokiSchemaVersion9,
];
async function updateToLokiSchemaVersion1(currentVersion, instance) {
@ -1075,6 +1076,26 @@ async function updateToLokiSchemaVersion8(currentVersion, instance) {
console.log('updateToLokiSchemaVersion8: success!');
}
async function updateToLokiSchemaVersion9(currentVersion, instance) {
if (currentVersion >= 9) {
return;
}
console.log('updateToLokiSchemaVersion9: starting...');
await instance.run('BEGIN TRANSACTION;');
await removePrefixFromGroupConversations(instance);
await instance.run(
`INSERT INTO loki_schema (
version
) values (
9
);`
);
await instance.run('COMMIT TRANSACTION;');
console.log('updateToLokiSchemaVersion9: success!');
}
async function updateLokiSchema(instance) {
const result = await instance.get(
"SELECT name FROM sqlite_master WHERE type = 'table' AND name='loki_schema';"
@ -3039,3 +3060,39 @@ async function removeKnownAttachments(allAttachments) {
return Object.keys(lookup);
}
async function removePrefixFromGroupConversations(instance) {
const rows = await instance.all(
`SELECT json FROM conversations WHERE
type = 'group' AND
id LIKE '__textsecure_group__!%';`
);
const objs = map(rows, row => jsonToObject(row.json));
await Promise.all(
objs.map(async o => {
const oldId = o.id;
const newId = oldId.replace('__textsecure_group__!', '');
console.log(`migrating conversation, ${oldId} to ${newId}`);
const morphedObject = {
...o,
id: newId,
};
await instance.run(
`UPDATE ${CONVERSATIONS_TABLE} SET
id = $newId,
json = $json
WHERE id = $oldId;`,
{
$newId: newId,
$json: objectToJSON(morphedObject),
$oldId: oldId,
}
);
})
);
}

View File

@ -645,7 +645,10 @@ export async function handleMessageEvent(event: MessageEvent): Promise<void> {
let conversationId = id;
if (isGroupMessage) {
// remove the prefix from the source object so this is correct for all other
message.group.id = message.group.id.replace(PubKey.PREFIX_GROUP_TEXTSECURE, '');
message.group.id = message.group.id.replace(
PubKey.PREFIX_GROUP_TEXTSECURE,
''
);
conversationId = message.group.id;
}