From d8f02a2f089a0ce99d77ecf6439b0e2e08b9f1f0 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 10 Mar 2022 10:51:15 +1100 Subject: [PATCH] fix crash on startup for migration 21 with closed groups --- app/sql.js | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/app/sql.js b/app/sql.js index 4893aed9f..ecf7f05e8 100644 --- a/app/sql.js +++ b/app/sql.js @@ -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({