fix crash on startup for migration 21 with closed groups

This commit is contained in:
Audric Ackermann 2022-03-10 10:51:15 +11:00
parent 236abe768b
commit d8f02a2f08
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4
1 changed files with 31 additions and 14 deletions

View File

@ -2,11 +2,21 @@ const path = require('path');
const fs = require('fs');
const rimraf = require('rimraf');
const SQL = require('better-sqlite3');
const { app, dialog, clipboard } = require('electron');
const { app, dialog, clipboard, Notification } = require('electron');
const { redactAll } = require('../js/modules/privacy');
const { remove: removeUserConfig } = require('./user_config');
const { map, isString, fromPairs, forEach, last, isEmpty, isObject, isNumber } = require('lodash');
const {
map,
flattenDeep,
uniq,
isString,
fromPairs,
forEach,
last,
isEmpty,
isObject,
isNumber,
} = require('lodash');
/* eslint-disable camelcase */
@ -1392,16 +1402,17 @@ function updateToLokiSchemaVersion21(currentVersion, db) {
`);
// all closed group admins
const closedGroupRows = getAllClosedGroupConversations(db) || [];
const closedGroups = getAllClosedGroupConversations(db) || [];
const adminIds = closedGroupRows.map(json => jsonToObject(json).groupAdmins);
forEach(adminIds, id => {
db.exec(
const adminIds = closedGroups.map(g => g.groupAdmins);
const flattenedAdmins = uniq(flattenDeep(adminIds)) || [];
forEach(flattenedAdmins, id => {
db.prepare(
`
UPDATE ${CONVERSATIONS_TABLE} SET
json = json_set(json, '$.didApproveMe', 1, '$.isApproved', 1)
WHERE type = id
values ($id);
WHERE id = $id;
`
).run({
id,
@ -1482,6 +1493,14 @@ function _initializePaths(configDir) {
databaseFilePath = path.join(dbDir, 'db.sqlite');
}
function showFailedToStart() {
const notification = new Notification({
title: 'Session failed to start',
body: 'Please start from terminal and open a github issue',
});
notification.show();
}
function initialize({ configDir, key, messages, passwordAttempt }) {
if (globalInstance) {
throw new Error('Cannot initialize more than once!');
@ -1544,9 +1563,7 @@ function initialize({ configDir, key, messages, passwordAttempt }) {
clipboard.writeText(`Database startup error:\n\n${redactAll(error.stack)}`);
} else {
close();
removeDB();
removeUserConfig();
app.relaunch();
showFailedToStart();
}
app.exit(1);
@ -2314,8 +2331,8 @@ function getUnreadCountByConversation(conversationId) {
function getMessageCountByType(conversationId, type = '%') {
const row = globalInstance
.prepare(
`SELECT count(*) from ${MESSAGES_TABLE}
WHERE conversationId = $conversationId
`SELECT count(*) from ${MESSAGES_TABLE}
WHERE conversationId = $conversationId
AND type = $type;`
)
.get({