2019-11-12 12:29:07 +00:00
|
|
|
/*
|
2019-11-18 09:16:29 +00:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-11-12 12:29:07 +00: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 08:26:46 +00:00
|
|
|
* Object holding the global EventIndex object. Can only be initialized if the
|
2019-11-18 13:29:03 +00:00
|
|
|
* platform supports event indexing.
|
2019-11-12 12:29:07 +00:00
|
|
|
*/
|
|
|
|
|
2019-11-19 11:52:12 +00:00
|
|
|
import PlatformPeg from "../PlatformPeg";
|
|
|
|
import EventIndex from "../indexing/EventIndex";
|
|
|
|
import SettingsStore from '../settings/SettingsStore';
|
2019-11-12 12:29:07 +00:00
|
|
|
|
|
|
|
class EventIndexPeg {
|
|
|
|
constructor() {
|
|
|
|
this.index = null;
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:27:10 +00:00
|
|
|
/**
|
|
|
|
* Create a new EventIndex and initialize it if the platform supports it.
|
2019-11-13 15:21:26 +00:00
|
|
|
*
|
|
|
|
* @return {Promise<bool>} A promise that will resolve to true if an
|
|
|
|
* EventIndex was successfully initialized, false otherwise.
|
2019-11-12 12:29:07 +00:00
|
|
|
*/
|
|
|
|
async init() {
|
2019-11-19 11:23:49 +00:00
|
|
|
if (!SettingsStore.isFeatureEnabled("feature_event_indexing")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-13 11:25:16 +00:00
|
|
|
const indexManager = PlatformPeg.get().getEventIndexingManager();
|
2019-11-18 09:34:48 +00:00
|
|
|
if (!indexManager || await indexManager.supportsEventIndexing() !== true) {
|
2019-11-13 14:39:39 +00:00
|
|
|
console.log("EventIndex: Platform doesn't support event indexing,",
|
|
|
|
"not initializing.");
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-12 12:29:07 +00:00
|
|
|
|
2019-11-13 14:39:39 +00:00
|
|
|
const index = new EventIndex();
|
|
|
|
|
|
|
|
try {
|
2019-11-14 13:13:49 +00:00
|
|
|
await index.init();
|
2019-11-13 14:39:39 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log("EventIndex: Error initializing the event index", e);
|
2019-11-14 15:01:14 +00:00
|
|
|
return false;
|
2019-11-13 14:39:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 12:29:07 +00:00
|
|
|
this.index = index;
|
|
|
|
|
2019-11-13 09:30:38 +00:00
|
|
|
return true;
|
2019-11-12 12:29:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 15:21:26 +00:00
|
|
|
/**
|
2019-11-14 15:13:22 +00:00
|
|
|
* Get the current event index.
|
|
|
|
*
|
|
|
|
* @return {EventIndex} The current event index.
|
2019-11-13 15:21:26 +00:00
|
|
|
*/
|
2019-11-14 15:13:22 +00:00
|
|
|
get() {
|
|
|
|
return this.index;
|
|
|
|
}
|
|
|
|
|
2019-11-12 14:40:49 +00:00
|
|
|
stop() {
|
|
|
|
if (this.index === null) return;
|
2019-11-14 15:13:22 +00: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;
|
|
|
|
this.index.close();
|
2019-11-12 14:40:49 +00:00
|
|
|
this.index = null;
|
2019-11-12 12:29:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 15:21:26 +00: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 12:29:07 +00:00
|
|
|
async deleteEventIndex() {
|
2019-11-14 15:13:22 +00:00
|
|
|
const indexManager = PlatformPeg.get().getEventIndexingManager();
|
|
|
|
|
|
|
|
if (indexManager !== null) {
|
2019-11-19 09:46:18 +00:00
|
|
|
this.unset();
|
2019-11-14 15:13:22 +00:00
|
|
|
console.log("EventIndex: Deleting event index.");
|
|
|
|
await indexManager.deleteEventIndex();
|
|
|
|
}
|
2019-11-12 12:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!global.mxEventIndexPeg) {
|
|
|
|
global.mxEventIndexPeg = new EventIndexPeg();
|
|
|
|
}
|
|
|
|
module.exports = global.mxEventIndexPeg;
|