Skip to content

Commit c3c5756

Browse files
committed
ElectronPlatform: Implement the EventIndexManager for Seshat.
1 parent a6839af commit c3c5756

File tree

2 files changed

+131
-55
lines changed

2 files changed

+131
-55
lines changed

electron_app/src/electron-main.js

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,6 @@ autoUpdater.on('update-downloaded', (ev, releaseNotes, releaseName, releaseDate,
155155
ipcMain.on('ipcCall', async function(ev, payload) {
156156
if (!mainWindow) return;
157157

158-
const sendError = (id, e) => {
159-
const error = {
160-
message: e.message
161-
}
162-
163-
mainWindow.webContents.send('ipcReply', {
164-
id:id,
165-
error: error
166-
});
167-
}
168-
169158
const args = payload.args || [];
170159
let ret;
171160

@@ -218,12 +207,50 @@ ipcMain.on('ipcCall', async function(ev, payload) {
218207
ret = vectorConfig;
219208
break;
220209

210+
default:
211+
mainWindow.webContents.send('ipcReply', {
212+
id: payload.id,
213+
error: "Unknown IPC Call: " + payload.name,
214+
});
215+
return;
216+
}
217+
218+
mainWindow.webContents.send('ipcReply', {
219+
id: payload.id,
220+
reply: ret,
221+
});
222+
});
223+
224+
ipcMain.on('seshat', async function(ev, payload) {
225+
if (!mainWindow) return;
226+
227+
const sendError = (id, e) => {
228+
const error = {
229+
message: e.message
230+
}
231+
232+
mainWindow.webContents.send('seshatReply', {
233+
id:id,
234+
error: error
235+
});
236+
}
237+
238+
const args = payload.args || [];
239+
let ret;
240+
241+
switch (payload.name) {
242+
case 'supportsEventIndexing':
243+
if (Seshat === null) ret = false;
244+
else ret = true;
245+
break;
246+
221247
case 'initEventIndex':
222248
if (args[0] && eventIndex === null) {
223249
let p = path.normalize(path.join(eventStorePath, args[0]));
224250
try {
225251
await makeDir(p);
226252
eventIndex = new Seshat(p);
253+
// eventIndex = new Seshat(p, {passphrase: "DEFAULT_PASSPHRASE"});
227254
console.log("Initialized event store");
228255
} catch (e) {
229256
sendError(payload.id, e);
@@ -317,14 +344,14 @@ ipcMain.on('ipcCall', async function(ev, payload) {
317344
break;
318345

319346
default:
320-
mainWindow.webContents.send('ipcReply', {
347+
mainWindow.webContents.send('seshatReply', {
321348
id: payload.id,
322349
error: "Unknown IPC Call: " + payload.name,
323350
});
324351
return;
325352
}
326353

327-
mainWindow.webContents.send('ipcReply', {
354+
mainWindow.webContents.send('seshatReply', {
328355
id: payload.id,
329356
reply: ret,
330357
});

src/vector/platform/ElectronPlatform.js

Lines changed: 91 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License.
2020
*/
2121

2222
import VectorBasePlatform, {updateCheckStatusEnum} from './VectorBasePlatform';
23+
import BaseEventIndexManager from 'matrix-react-sdk/lib/BaseEventIndexManager';
2324
import dis from 'matrix-react-sdk/lib/dispatcher';
2425
import { _t } from 'matrix-react-sdk/lib/languageHandler';
2526
import Promise from 'bluebird';
@@ -66,12 +67,100 @@ function getUpdateCheckStatus(status) {
6667
}
6768
}
6869

70+
class SeshatIndexerManager extends BaseEventIndexManager {
71+
constructor() {
72+
super();
73+
74+
this._pendingIpcCalls = {};
75+
this._nextIpcCallId = 0;
76+
ipcRenderer.on('seshatReply', this._onIpcReply.bind(this));
77+
}
78+
79+
async _ipcCall(name: string, ...args: []): Promise<{}> {
80+
// TODO this should be moved into the preload.js file.
81+
const ipcCallId = ++this._nextIpcCallId;
82+
return new Promise((resolve, reject) => {
83+
this._pendingIpcCalls[ipcCallId] = {resolve, reject};
84+
window.ipcRenderer.send('seshat', {id: ipcCallId, name, args});
85+
});
86+
}
87+
88+
_onIpcReply(ev: {}, payload: {}) {
89+
if (payload.id === undefined) {
90+
console.warn("Ignoring IPC reply with no ID");
91+
return;
92+
}
93+
94+
if (this._pendingIpcCalls[payload.id] === undefined) {
95+
console.warn("Unknown IPC payload ID: " + payload.id);
96+
return;
97+
}
98+
99+
const callbacks = this._pendingIpcCalls[payload.id];
100+
delete this._pendingIpcCalls[payload.id];
101+
if (payload.error) {
102+
callbacks.reject(payload.error);
103+
} else {
104+
callbacks.resolve(payload.reply);
105+
}
106+
}
107+
108+
async supportsEventIndexing(): Promise<boolean> {
109+
return this._ipcCall('supportsEventIndexing');
110+
}
111+
112+
async initEventIndex(userId: string): Promise<> {
113+
return this._ipcCall('initEventIndex', userId);
114+
}
115+
116+
async addEventToIndex(ev: MatrixEvent, profile: MatrixProfile): Promise<> {
117+
return this._ipcCall('addEventToIndex', ev, profile);
118+
}
119+
120+
async isEventIndexEmpty(): Promise<boolean> {
121+
return this._ipcCall('isEventIndexEmpty');
122+
}
123+
124+
async commitLiveEvents(): Promise<> {
125+
return this._ipcCall('commitLiveEvents');
126+
}
127+
128+
async searchEventIndex(searchConfig: SearchConfig): Promise<SearchResult> {
129+
return this._ipcCall('searchEventIndex', searchConfig);
130+
}
131+
132+
async addHistoricEvents(
133+
events: [HistoricEvent],
134+
checkpoint: CrawlerCheckpoint | null = null,
135+
oldCheckpoint: CrawlerCheckpoint | null = null,
136+
): Promise<> {
137+
return this._ipcCall('addHistoricEvents', events, checkpoint, oldCheckpoint);
138+
}
139+
140+
async addCrawlerCheckpoint(checkpoint: CrawlerCheckpoint): Promise<> {
141+
return this._ipcCall('addCrawlerCheckpoint', checkpoint);
142+
}
143+
144+
async removeCrawlerCheckpoint(checkpoint: CrawlerCheckpoint): Promise<> {
145+
return this._ipcCall('removeCrawlerCheckpoint', checkpoint);
146+
}
147+
148+
async loadCheckpoints(): Promise<[CrawlerCheckpoint]> {
149+
return this._ipcCall('loadCheckpoints');
150+
}
151+
152+
async deleteEventIndex(): Promise<> {
153+
return this._ipcCall('deleteEventIndex');
154+
}
155+
}
156+
69157
export default class ElectronPlatform extends VectorBasePlatform {
70158
constructor() {
71159
super();
72160

73161
this._pendingIpcCalls = {};
74162
this._nextIpcCallId = 0;
163+
this.eventIndexManager = new SeshatIndexerManager();
75164

76165
dis.register(_onAction);
77166
/*
@@ -294,47 +383,7 @@ export default class ElectronPlatform extends VectorBasePlatform {
294383
}
295384
}
296385

297-
async initEventIndex(userId: string): void {
298-
return this._ipcCall('initEventIndex', userId);
299-
}
300-
301-
supportsEventIndexing(): boolean {
302-
return true;
303-
}
304-
305-
async addEventToIndex(ev: {}, profile: {}): void {
306-
return this._ipcCall('addEventToIndex', ev, profile);
307-
}
308-
309-
async isEventIndexEmpty(): Promise<boolean> {
310-
return this._ipcCall('isEventIndexEmpty');
311-
}
312-
313-
async commitLiveEvents(): Promise<{}> {
314-
return this._ipcCall('commitLiveEvents');
315-
}
316-
317-
async searchEventIndex(term: string): Promise<{}> {
318-
return this._ipcCall('searchEventIndex', term);
319-
}
320-
321-
async addHistoricEvents(events: string, checkpoint: {} = null, oldCheckpoint: {} = null): Promise<{}> {
322-
return this._ipcCall('addHistoricEvents', events, checkpoint, oldCheckpoint);
323-
}
324-
325-
async addCrawlerCheckpoint(checkpoint: {}): Promise<{}> {
326-
return this._ipcCall('addCrawlerCheckpoint', checkpoint);
327-
}
328-
329-
async removeCrawlerCheckpoint(checkpoint: {}): Promise<{}> {
330-
return this._ipcCall('removeCrawlerCheckpoint', checkpoint);
331-
}
332-
333-
async loadCheckpoints(checkpoint: {}): Promise<[{}]> {
334-
return this._ipcCall('loadCheckpoints');
335-
}
336-
337-
async deleteEventIndex(): Promise<> {
338-
return this._ipcCall('deleteEventIndex');
386+
getEventIndexingManager(): BaseEventIndexManager | null {
387+
return this.eventIndexManager;
339388
}
340389
}

0 commit comments

Comments
 (0)