2019-11-12 15:29:07 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Holds the current Platform object used by the code to do anything
|
|
|
|
* specific to the platform we're running on (eg. web, electron)
|
|
|
|
* Platforms are provided by the app layer.
|
|
|
|
* This allows the app layer to set a Platform without necessarily
|
|
|
|
* having to have a MatrixChat object
|
|
|
|
*/
|
|
|
|
|
|
|
|
import PlatformPeg from "./PlatformPeg";
|
|
|
|
import EventIndex from "./EventIndexing";
|
|
|
|
import MatrixClientPeg from "./MatrixClientPeg";
|
|
|
|
|
|
|
|
class EventIndexPeg {
|
|
|
|
constructor() {
|
|
|
|
this.index = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-13 12:30:38 +03:00
|
|
|
* Get the current event index.
|
|
|
|
*
|
2019-11-13 18:21:26 +03:00
|
|
|
* @return {EventIndex} The current event index.
|
2019-11-12 15:29:07 +03:00
|
|
|
*/
|
|
|
|
get() {
|
|
|
|
return this.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create a new EventIndex and initialize it if the platform supports it.
|
2019-11-13 18:21:26 +03:00
|
|
|
*
|
|
|
|
* @return {Promise<bool>} A promise that will resolve to true if an
|
|
|
|
* EventIndex was successfully initialized, false otherwise.
|
2019-11-12 15:29:07 +03:00
|
|
|
*/
|
|
|
|
async init() {
|
2019-11-13 14:25:16 +03:00
|
|
|
const indexManager = PlatformPeg.get().getEventIndexingManager();
|
|
|
|
if (indexManager === null) return false;
|
2019-11-12 15:29:07 +03:00
|
|
|
|
2019-11-13 17:39:39 +03:00
|
|
|
if (await indexManager.supportsEventIndexing() !== true) {
|
|
|
|
console.log("EventIndex: Platform doesn't support event indexing,",
|
|
|
|
"not initializing.");
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-12 15:29:07 +03:00
|
|
|
|
2019-11-13 17:39:39 +03:00
|
|
|
const index = new EventIndex();
|
2019-11-12 15:29:07 +03:00
|
|
|
const userId = MatrixClientPeg.get().getUserId();
|
2019-11-13 17:39:39 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
await index.init(userId);
|
|
|
|
} catch (e) {
|
|
|
|
console.log("EventIndex: Error initializing the event index", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("EventIndex: Successfully initialized the event index");
|
|
|
|
|
2019-11-12 15:29:07 +03:00
|
|
|
this.index = index;
|
|
|
|
|
2019-11-13 12:30:38 +03:00
|
|
|
return true;
|
2019-11-12 15:29:07 +03:00
|
|
|
}
|
|
|
|
|
2019-11-13 18:21:26 +03:00
|
|
|
/**
|
|
|
|
* Stop our event indexer.
|
|
|
|
*/
|
2019-11-12 17:40:49 +03:00
|
|
|
stop() {
|
|
|
|
if (this.index === null) return;
|
2019-11-13 12:30:38 +03:00
|
|
|
this.index.stop();
|
2019-11-12 17:40:49 +03:00
|
|
|
this.index = null;
|
2019-11-12 15:29:07 +03:00
|
|
|
}
|
|
|
|
|
2019-11-13 18:21:26 +03: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 15:29:07 +03:00
|
|
|
async deleteEventIndex() {
|
2019-11-12 17:40:49 +03:00
|
|
|
if (this.index === null) return;
|
2019-11-13 12:30:38 +03:00
|
|
|
this.index.deleteEventIndex();
|
2019-11-13 18:21:26 +03:00
|
|
|
this.index = null;
|
2019-11-12 15:29:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!global.mxEventIndexPeg) {
|
|
|
|
global.mxEventIndexPeg = new EventIndexPeg();
|
|
|
|
}
|
|
|
|
module.exports = global.mxEventIndexPeg;
|