From c3c5756c7a183890fc3de7ac2f0b1d45e5b7182c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 13 Nov 2019 12:15:26 +0100 Subject: [PATCH] ElectronPlatform: Implement the EventIndexManager for Seshat. --- electron_app/src/electron-main.js | 53 +++++++--- src/vector/platform/ElectronPlatform.js | 133 ++++++++++++++++-------- 2 files changed, 131 insertions(+), 55 deletions(-) diff --git a/electron_app/src/electron-main.js b/electron_app/src/electron-main.js index 175dc271..832b350e 100644 --- a/electron_app/src/electron-main.js +++ b/electron_app/src/electron-main.js @@ -155,17 +155,6 @@ autoUpdater.on('update-downloaded', (ev, releaseNotes, releaseName, releaseDate, ipcMain.on('ipcCall', async function(ev, payload) { if (!mainWindow) return; - const sendError = (id, e) => { - const error = { - message: e.message - } - - mainWindow.webContents.send('ipcReply', { - id:id, - error: error - }); - } - const args = payload.args || []; let ret; @@ -218,12 +207,50 @@ ipcMain.on('ipcCall', async function(ev, payload) { ret = vectorConfig; break; + default: + mainWindow.webContents.send('ipcReply', { + id: payload.id, + error: "Unknown IPC Call: " + payload.name, + }); + return; + } + + mainWindow.webContents.send('ipcReply', { + id: payload.id, + reply: ret, + }); +}); + +ipcMain.on('seshat', async function(ev, payload) { + if (!mainWindow) return; + + const sendError = (id, e) => { + const error = { + message: e.message + } + + mainWindow.webContents.send('seshatReply', { + id:id, + error: error + }); + } + + const args = payload.args || []; + let ret; + + switch (payload.name) { + case 'supportsEventIndexing': + if (Seshat === null) ret = false; + else ret = true; + break; + case 'initEventIndex': if (args[0] && eventIndex === null) { let p = path.normalize(path.join(eventStorePath, args[0])); try { await makeDir(p); eventIndex = new Seshat(p); + // eventIndex = new Seshat(p, {passphrase: "DEFAULT_PASSPHRASE"}); console.log("Initialized event store"); } catch (e) { sendError(payload.id, e); @@ -317,14 +344,14 @@ ipcMain.on('ipcCall', async function(ev, payload) { break; default: - mainWindow.webContents.send('ipcReply', { + mainWindow.webContents.send('seshatReply', { id: payload.id, error: "Unknown IPC Call: " + payload.name, }); return; } - mainWindow.webContents.send('ipcReply', { + mainWindow.webContents.send('seshatReply', { id: payload.id, reply: ret, }); diff --git a/src/vector/platform/ElectronPlatform.js b/src/vector/platform/ElectronPlatform.js index d6a689fc..14f37442 100644 --- a/src/vector/platform/ElectronPlatform.js +++ b/src/vector/platform/ElectronPlatform.js @@ -20,6 +20,7 @@ limitations under the License. */ import VectorBasePlatform, {updateCheckStatusEnum} from './VectorBasePlatform'; +import BaseEventIndexManager from 'matrix-react-sdk/lib/BaseEventIndexManager'; import dis from 'matrix-react-sdk/lib/dispatcher'; import { _t } from 'matrix-react-sdk/lib/languageHandler'; import Promise from 'bluebird'; @@ -66,12 +67,100 @@ function getUpdateCheckStatus(status) { } } +class SeshatIndexerManager extends BaseEventIndexManager { + constructor() { + super(); + + this._pendingIpcCalls = {}; + this._nextIpcCallId = 0; + ipcRenderer.on('seshatReply', this._onIpcReply.bind(this)); + } + + async _ipcCall(name: string, ...args: []): Promise<{}> { + // TODO this should be moved into the preload.js file. + const ipcCallId = ++this._nextIpcCallId; + return new Promise((resolve, reject) => { + this._pendingIpcCalls[ipcCallId] = {resolve, reject}; + window.ipcRenderer.send('seshat', {id: ipcCallId, name, args}); + }); + } + + _onIpcReply(ev: {}, payload: {}) { + if (payload.id === undefined) { + console.warn("Ignoring IPC reply with no ID"); + return; + } + + if (this._pendingIpcCalls[payload.id] === undefined) { + console.warn("Unknown IPC payload ID: " + payload.id); + return; + } + + const callbacks = this._pendingIpcCalls[payload.id]; + delete this._pendingIpcCalls[payload.id]; + if (payload.error) { + callbacks.reject(payload.error); + } else { + callbacks.resolve(payload.reply); + } + } + + async supportsEventIndexing(): Promise { + return this._ipcCall('supportsEventIndexing'); + } + + async initEventIndex(userId: string): Promise<> { + return this._ipcCall('initEventIndex', userId); + } + + async addEventToIndex(ev: MatrixEvent, profile: MatrixProfile): Promise<> { + return this._ipcCall('addEventToIndex', ev, profile); + } + + async isEventIndexEmpty(): Promise { + return this._ipcCall('isEventIndexEmpty'); + } + + async commitLiveEvents(): Promise<> { + return this._ipcCall('commitLiveEvents'); + } + + async searchEventIndex(searchConfig: SearchConfig): Promise { + return this._ipcCall('searchEventIndex', searchConfig); + } + + async addHistoricEvents( + events: [HistoricEvent], + checkpoint: CrawlerCheckpoint | null = null, + oldCheckpoint: CrawlerCheckpoint | null = null, + ): Promise<> { + return this._ipcCall('addHistoricEvents', events, checkpoint, oldCheckpoint); + } + + async addCrawlerCheckpoint(checkpoint: CrawlerCheckpoint): Promise<> { + return this._ipcCall('addCrawlerCheckpoint', checkpoint); + } + + async removeCrawlerCheckpoint(checkpoint: CrawlerCheckpoint): Promise<> { + return this._ipcCall('removeCrawlerCheckpoint', checkpoint); + } + + async loadCheckpoints(): Promise<[CrawlerCheckpoint]> { + return this._ipcCall('loadCheckpoints'); + } + + async deleteEventIndex(): Promise<> { + return this._ipcCall('deleteEventIndex'); + } +} + export default class ElectronPlatform extends VectorBasePlatform { constructor() { super(); this._pendingIpcCalls = {}; this._nextIpcCallId = 0; + this.eventIndexManager = new SeshatIndexerManager(); dis.register(_onAction); /* @@ -294,47 +383,7 @@ export default class ElectronPlatform extends VectorBasePlatform { } } - async initEventIndex(userId: string): void { - return this._ipcCall('initEventIndex', userId); - } - - supportsEventIndexing(): boolean { - return true; - } - - async addEventToIndex(ev: {}, profile: {}): void { - return this._ipcCall('addEventToIndex', ev, profile); - } - - async isEventIndexEmpty(): Promise { - return this._ipcCall('isEventIndexEmpty'); - } - - async commitLiveEvents(): Promise<{}> { - return this._ipcCall('commitLiveEvents'); - } - - async searchEventIndex(term: string): Promise<{}> { - return this._ipcCall('searchEventIndex', term); - } - - async addHistoricEvents(events: string, checkpoint: {} = null, oldCheckpoint: {} = null): Promise<{}> { - return this._ipcCall('addHistoricEvents', events, checkpoint, oldCheckpoint); - } - - async addCrawlerCheckpoint(checkpoint: {}): Promise<{}> { - return this._ipcCall('addCrawlerCheckpoint', checkpoint); - } - - async removeCrawlerCheckpoint(checkpoint: {}): Promise<{}> { - return this._ipcCall('removeCrawlerCheckpoint', checkpoint); - } - - async loadCheckpoints(checkpoint: {}): Promise<[{}]> { - return this._ipcCall('loadCheckpoints'); - } - - async deleteEventIndex(): Promise<> { - return this._ipcCall('deleteEventIndex'); + getEventIndexingManager(): BaseEventIndexManager | null { + return this.eventIndexManager; } }