2019-11-12 13:29:07 +01:00
|
|
|
/*
|
2021-04-06 13:26:50 +02:00
|
|
|
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
|
2019-11-12 13:29:07 +01:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2019-11-19 09:26:46 +01:00
|
|
|
* Object holding the global EventIndex object. Can only be initialized if the
|
2019-11-18 14:29:03 +01:00
|
|
|
* platform supports event indexing.
|
2019-11-12 13:29:07 +01:00
|
|
|
*/
|
|
|
|
|
2019-11-19 12:52:12 +01:00
|
|
|
import PlatformPeg from "../PlatformPeg";
|
|
|
|
import EventIndex from "../indexing/EventIndex";
|
2020-10-13 16:02:05 +02:00
|
|
|
import {MatrixClientPeg} from "../MatrixClientPeg";
|
2020-07-28 19:53:43 +02:00
|
|
|
import SettingsStore from '../settings/SettingsStore';
|
|
|
|
import {SettingLevel} from "../settings/SettingLevel";
|
2019-11-12 13:29:07 +01:00
|
|
|
|
2020-06-17 17:13:25 +02:00
|
|
|
const INDEX_VERSION = 1;
|
|
|
|
|
2021-04-06 13:26:50 +02:00
|
|
|
export class EventIndexPeg {
|
2021-04-26 15:06:25 +02:00
|
|
|
public index: EventIndex = null;
|
|
|
|
public error: Error = null;
|
2021-04-06 13:26:50 +02:00
|
|
|
|
2021-04-26 15:06:25 +02:00
|
|
|
private _supportIsInstalled = false;
|
2019-11-12 13:29:07 +01:00
|
|
|
|
2019-11-18 10:27:10 +01:00
|
|
|
/**
|
2020-01-23 11:47:49 +01:00
|
|
|
* Initialize the EventIndexPeg and if event indexing is enabled initialize
|
|
|
|
* the event index.
|
2019-11-13 16:21:26 +01:00
|
|
|
*
|
2020-01-23 11:47:49 +01:00
|
|
|
* @return {Promise<boolean>} A promise that will resolve to true if an
|
2019-11-13 16:21:26 +01:00
|
|
|
* EventIndex was successfully initialized, false otherwise.
|
2019-11-12 13:29:07 +01:00
|
|
|
*/
|
|
|
|
async init() {
|
2019-11-13 12:25:16 +01:00
|
|
|
const indexManager = PlatformPeg.get().getEventIndexingManager();
|
2020-01-23 11:47:49 +01:00
|
|
|
if (!indexManager) {
|
|
|
|
console.log("EventIndex: Platform doesn't support event indexing, not initializing.");
|
2019-11-13 15:39:39 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-12 13:29:07 +01:00
|
|
|
|
2020-01-23 11:47:49 +01:00
|
|
|
this._supportIsInstalled = await indexManager.supportsEventIndexing();
|
|
|
|
|
|
|
|
if (!this.supportIsInstalled()) {
|
|
|
|
console.log("EventIndex: Event indexing isn't installed for the platform, not initializing.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SettingsStore.getValueAt(SettingLevel.DEVICE, 'enableEventIndexing')) {
|
|
|
|
console.log("EventIndex: Event indexing is disabled, not initializing");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.initEventIndex();
|
|
|
|
}
|
|
|
|
|
2020-01-24 11:16:49 +01:00
|
|
|
/**
|
|
|
|
* Initialize the event index.
|
2020-01-23 11:47:49 +01:00
|
|
|
*
|
|
|
|
* @returns {boolean} True if the event index was succesfully initialized,
|
|
|
|
* false otherwise.
|
|
|
|
*/
|
|
|
|
async initEventIndex() {
|
2019-11-13 15:39:39 +01:00
|
|
|
const index = new EventIndex();
|
2020-06-17 17:13:25 +02:00
|
|
|
const indexManager = PlatformPeg.get().getEventIndexingManager();
|
2020-10-13 16:02:05 +02:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
2020-10-13 17:03:58 +02:00
|
|
|
const userId = client.getUserId();
|
|
|
|
const deviceId = client.getDeviceId();
|
2019-11-13 15:39:39 +01:00
|
|
|
|
|
|
|
try {
|
2020-10-13 17:03:58 +02:00
|
|
|
await indexManager.initEventIndex(userId, deviceId);
|
2020-06-17 17:13:25 +02:00
|
|
|
|
|
|
|
const userVersion = await indexManager.getUserVersion();
|
|
|
|
const eventIndexIsEmpty = await indexManager.isEventIndexEmpty();
|
|
|
|
|
|
|
|
if (eventIndexIsEmpty) {
|
|
|
|
await indexManager.setUserVersion(INDEX_VERSION);
|
|
|
|
} else if (userVersion === 0 && !eventIndexIsEmpty) {
|
|
|
|
await indexManager.closeEventIndex();
|
|
|
|
await this.deleteEventIndex();
|
|
|
|
|
2020-10-13 17:03:58 +02:00
|
|
|
await indexManager.initEventIndex(userId, deviceId);
|
2020-06-17 17:13:25 +02:00
|
|
|
await indexManager.setUserVersion(INDEX_VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("EventIndex: Successfully initialized the event index");
|
2019-11-14 14:13:49 +01:00
|
|
|
await index.init();
|
2019-11-13 15:39:39 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.log("EventIndex: Error initializing the event index", e);
|
2021-03-24 12:51:39 +01:00
|
|
|
this.error = e;
|
2019-11-14 16:01:14 +01:00
|
|
|
return false;
|
2019-11-13 15:39:39 +01:00
|
|
|
}
|
|
|
|
|
2019-11-12 13:29:07 +01:00
|
|
|
this.index = index;
|
|
|
|
|
2019-11-13 10:30:38 +01:00
|
|
|
return true;
|
2019-11-12 13:29:07 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 11:47:49 +01:00
|
|
|
/**
|
|
|
|
* Check if the current platform has support for event indexing.
|
|
|
|
*
|
|
|
|
* @return {boolean} True if it has support, false otherwise. Note that this
|
|
|
|
* does not mean that support is installed.
|
|
|
|
*/
|
|
|
|
platformHasSupport(): boolean {
|
|
|
|
return PlatformPeg.get().getEventIndexingManager() !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if event indexing support is installed for the platfrom.
|
|
|
|
*
|
|
|
|
* Event indexing might require additional optional modules to be installed,
|
|
|
|
* this tells us if those are installed. Note that this should only be
|
|
|
|
* called after the init() method was called.
|
|
|
|
*
|
|
|
|
* @return {boolean} True if support is installed, false otherwise.
|
|
|
|
*/
|
|
|
|
supportIsInstalled(): boolean {
|
|
|
|
return this._supportIsInstalled;
|
|
|
|
}
|
|
|
|
|
2019-11-13 16:21:26 +01:00
|
|
|
/**
|
2019-11-14 16:13:22 +01:00
|
|
|
* Get the current event index.
|
|
|
|
*
|
|
|
|
* @return {EventIndex} The current event index.
|
2019-11-13 16:21:26 +01:00
|
|
|
*/
|
2019-11-14 16:13:22 +01:00
|
|
|
get() {
|
|
|
|
return this.index;
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:37:53 +01:00
|
|
|
start() {
|
|
|
|
if (this.index === null) return;
|
|
|
|
this.index.startCrawler();
|
|
|
|
}
|
|
|
|
|
2019-11-12 15:40:49 +01:00
|
|
|
stop() {
|
|
|
|
if (this.index === null) return;
|
2019-11-14 16:13:22 +01:00
|
|
|
this.index.stopCrawler();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unset our event store
|
|
|
|
*
|
|
|
|
* After a call to this the init() method will need to be called again.
|
|
|
|
*
|
|
|
|
* @return {Promise} A promise that will resolve once the event index is
|
|
|
|
* closed.
|
|
|
|
*/
|
|
|
|
async unset() {
|
|
|
|
if (this.index === null) return;
|
2020-03-13 11:33:58 +01:00
|
|
|
await this.index.close();
|
2019-11-12 15:40:49 +01:00
|
|
|
this.index = null;
|
2019-11-12 13:29:07 +01:00
|
|
|
}
|
|
|
|
|
2019-11-13 16:21:26 +01:00
|
|
|
/**
|
|
|
|
* Delete our event indexer.
|
|
|
|
*
|
|
|
|
* After a call to this the init() method will need to be called again.
|
|
|
|
*
|
|
|
|
* @return {Promise} A promise that will resolve once the event index is
|
|
|
|
* deleted.
|
|
|
|
*/
|
2019-11-12 13:29:07 +01:00
|
|
|
async deleteEventIndex() {
|
2019-11-14 16:13:22 +01:00
|
|
|
const indexManager = PlatformPeg.get().getEventIndexingManager();
|
|
|
|
|
|
|
|
if (indexManager !== null) {
|
2020-03-13 11:33:58 +01:00
|
|
|
await this.unset();
|
2019-11-14 16:13:22 +01:00
|
|
|
console.log("EventIndex: Deleting event index.");
|
|
|
|
await indexManager.deleteEventIndex();
|
|
|
|
}
|
2019-11-12 13:29:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 13:26:50 +02:00
|
|
|
if (!window.mxEventIndexPeg) {
|
|
|
|
window.mxEventIndexPeg = new EventIndexPeg();
|
2019-11-12 13:29:07 +01:00
|
|
|
}
|
2021-04-06 13:26:50 +02:00
|
|
|
export default window.mxEventIndexPeg;
|