From 4b350860282bc6fe32c4a6dca2cbd7bc1c45f9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 11 Mar 2020 11:27:00 +0100 Subject: [PATCH 1/3] electron-main: Reindex the event index if it indicates that this is needed. --- src/electron-main.js | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/src/electron-main.js b/src/electron-main.js index 5fae176..33c6acf 100644 --- a/src/electron-main.js +++ b/src/electron-main.js @@ -43,10 +43,19 @@ const Store = require('electron-store'); const fs = require('fs'); const afs = fs.promises; -let Seshat = null; +let seshatSupported = false; +let Seshat; +let SeshatRecovery; +let ReindexError; + +const seshatPassphrase = "DEFAULT_PASSPHRASE"; try { - Seshat = require('matrix-seshat'); + seshatModule = require('matrix-seshat'); + Seshat = seshatModule.Seshat; + SeshatRecovery = seshatModule.SeshatRecovery; + ReindexError = seshatModule.ReindexError; + seshatSupported = true; } catch (e) { if (e.code === "MODULE_NOT_FOUND") { console.log("Seshat isn't installed, event indexing is disabled."); @@ -349,18 +358,36 @@ ipcMain.on('seshat', async function(ev, payload) { switch (payload.name) { case 'supportsEventIndexing': - if (Seshat === null) ret = false; - else ret = true; + ret = seshatSupported; break; case 'initEventIndex': if (eventIndex === null) { try { await afs.mkdir(eventStorePath, {recursive: true}); - eventIndex = new Seshat(eventStorePath, {passphrase: "DEFAULT_PASSPHRASE"}); + eventIndex = new Seshat(eventStorePath, {passphrase: seshatPassphrase}); } catch (e) { - sendError(payload.id, e); - return; + if (e instanceof ReindexError) { + // If this is a reindex error, the index schema + // changed. Try to open the database in recovery mode, + // reindex the database and finally try to open the + // database again. + try { + recoveryIndex = new SeshatRecovery(eventStorePath, { + passphrase: seshatPassphrase + }); + await recoveryIndex.reindex(); + eventIndex = new Seshat(eventStorePath, { + passphrase: seshatPassphrase + }); + } catch (e) { + sendError(payload.id, e); + return; + } + } else { + sendError(payload.id, e); + return; + } } } break; From 3509d3d5af3e2744d5de3de37b3613bd1c6efd06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 11 Mar 2020 12:05:34 +0100 Subject: [PATCH 2/3] electron-main: Fix a couple of lint errors. --- src/electron-main.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/electron-main.js b/src/electron-main.js index 33c6acf..9afdc1b 100644 --- a/src/electron-main.js +++ b/src/electron-main.js @@ -51,7 +51,7 @@ let ReindexError; const seshatPassphrase = "DEFAULT_PASSPHRASE"; try { - seshatModule = require('matrix-seshat'); + const seshatModule = require('matrix-seshat'); Seshat = seshatModule.Seshat; SeshatRecovery = seshatModule.SeshatRecovery; ReindexError = seshatModule.ReindexError; @@ -373,12 +373,12 @@ ipcMain.on('seshat', async function(ev, payload) { // reindex the database and finally try to open the // database again. try { - recoveryIndex = new SeshatRecovery(eventStorePath, { - passphrase: seshatPassphrase + const recoveryIndex = new SeshatRecovery(eventStorePath, { + passphrase: seshatPassphrase, }); await recoveryIndex.reindex(); eventIndex = new Seshat(eventStorePath, { - passphrase: seshatPassphrase + passphrase: seshatPassphrase, }); } catch (e) { sendError(payload.id, e); From 23bfa9262e7fc570ecfaa0adeb0aaf1138dd5c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 12 Mar 2020 11:46:14 +0100 Subject: [PATCH 3/3] electron-main: Add support to delete events from the index. --- src/electron-main.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/electron-main.js b/src/electron-main.js index 9afdc1b..36c7aa9 100644 --- a/src/electron-main.js +++ b/src/electron-main.js @@ -427,6 +427,15 @@ ipcMain.on('seshat', async function(ev, payload) { } break; + case 'deleteEvent': + try { + ret = await eventIndex.deleteEvent(args[0]); + } catch (e) { + sendError(payload.id, e); + return; + } + break; + case 'commitLiveEvents': try { ret = await eventIndex.commit();