2015-06-23 17:41:25 +02:00
|
|
|
/*
|
2016-01-07 05:06:39 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-06-21 15:04:43 +02:00
|
|
|
Copyright 2017 Vector Creations Ltd.
|
2015-06-23 17:41:25 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2016-08-03 17:45:23 +02:00
|
|
|
import utils from 'matrix-js-sdk/lib/utils';
|
2016-09-08 04:02:26 +02:00
|
|
|
import EventTimeline from 'matrix-js-sdk/lib/models/event-timeline';
|
|
|
|
import EventTimelineSet from 'matrix-js-sdk/lib/models/event-timeline-set';
|
2017-06-13 13:46:49 +02:00
|
|
|
import createMatrixClient from './utils/createMatrixClient';
|
2015-07-20 14:19:47 +02:00
|
|
|
|
2016-08-03 11:46:42 +02:00
|
|
|
interface MatrixClientCreds {
|
|
|
|
homeserverUrl: string,
|
|
|
|
identityServerUrl: string,
|
|
|
|
userId: string,
|
2016-08-11 15:21:52 +02:00
|
|
|
deviceId: string,
|
2016-08-03 11:46:42 +02:00
|
|
|
accessToken: string,
|
|
|
|
guest: boolean,
|
|
|
|
}
|
|
|
|
|
2016-07-22 16:47:47 +02:00
|
|
|
/**
|
|
|
|
* Wrapper object for handling the js-sdk Matrix Client object in the react-sdk
|
|
|
|
* Handles the creation/initialisation of client objects.
|
|
|
|
* This module provides a singleton instance of this class so the 'current'
|
|
|
|
* Matrix Client object is available easily.
|
|
|
|
*/
|
2016-07-21 18:57:55 +02:00
|
|
|
class MatrixClientPeg {
|
2016-07-22 16:47:47 +02:00
|
|
|
constructor() {
|
2016-07-21 18:57:55 +02:00
|
|
|
this.matrixClient = null;
|
2016-08-02 15:04:20 +02:00
|
|
|
|
2016-08-03 18:23:09 +02:00
|
|
|
// These are the default options used when when the
|
|
|
|
// client is started in 'start'. These can be altered
|
|
|
|
// at any time up to after the 'will_start_client'
|
|
|
|
// event is finished processing.
|
2016-08-02 15:04:20 +02:00
|
|
|
this.opts = {
|
|
|
|
initialSyncLimit: 20,
|
|
|
|
};
|
2017-04-06 12:13:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the script href passed to the IndexedDB web worker
|
|
|
|
* If set, a separate web worker will be started to run the IndexedDB
|
|
|
|
* queries on.
|
|
|
|
*
|
|
|
|
* @param {string} script href to the script to be passed to the web worker
|
|
|
|
*/
|
|
|
|
setIndexedDbWorkerScript(script) {
|
2017-06-21 18:43:34 +02:00
|
|
|
createMatrixClient.indexedDbWorkerScript = script;
|
2015-12-04 11:37:53 +01:00
|
|
|
}
|
|
|
|
|
2016-06-12 13:32:46 +02:00
|
|
|
get(): MatrixClient {
|
2016-07-21 18:57:55 +02:00
|
|
|
return this.matrixClient;
|
2015-09-28 18:46:49 +02:00
|
|
|
}
|
2015-06-09 18:40:42 +02:00
|
|
|
|
2015-09-28 18:46:49 +02:00
|
|
|
unset() {
|
2016-07-21 18:57:55 +02:00
|
|
|
this.matrixClient = null;
|
2015-09-28 18:46:49 +02:00
|
|
|
}
|
2015-09-16 14:48:24 +02:00
|
|
|
|
2016-07-22 16:47:47 +02:00
|
|
|
/**
|
|
|
|
* Replace this MatrixClientPeg's client with a client instance that has
|
|
|
|
* Home Server / Identity Server URLs and active credentials
|
|
|
|
*/
|
2016-08-03 11:46:42 +02:00
|
|
|
replaceUsingCreds(creds: MatrixClientCreds) {
|
2016-08-11 14:50:38 +02:00
|
|
|
this._createClient(creds);
|
2016-07-25 17:28:28 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 17:20:21 +02:00
|
|
|
async start() {
|
2017-07-19 00:46:03 +02:00
|
|
|
// try to initialise e2e on the new client
|
|
|
|
try {
|
|
|
|
// check that we have a version of the js-sdk which includes initCrypto
|
|
|
|
if (this.matrixClient.initCrypto) {
|
|
|
|
await this.matrixClient.initCrypto();
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
// this can happen for a number of reasons, the most likely being
|
|
|
|
// that the olm library was missing. It's not fatal.
|
|
|
|
console.warn("Unable to initialise e2e: " + e);
|
|
|
|
}
|
|
|
|
|
2016-08-03 17:45:23 +02:00
|
|
|
const opts = utils.deepCopy(this.opts);
|
2016-08-03 18:23:09 +02:00
|
|
|
// the react sdk doesn't work without this, so don't allow
|
2016-08-03 17:39:47 +02:00
|
|
|
opts.pendingEventOrdering = "detached";
|
2017-10-14 22:37:47 +02:00
|
|
|
opts.disablePresence = true; // we do this manually
|
2017-02-02 15:27:27 +01:00
|
|
|
|
2017-07-05 17:20:21 +02:00
|
|
|
try {
|
2017-10-11 18:56:17 +02:00
|
|
|
const promise = this.matrixClient.store.startup();
|
2017-07-05 17:20:21 +02:00
|
|
|
console.log(`MatrixClientPeg: waiting for MatrixClient store to initialise`);
|
|
|
|
await promise;
|
|
|
|
} catch(err) {
|
|
|
|
// log any errors when starting up the database (if one exists)
|
2017-06-20 18:38:02 +02:00
|
|
|
console.error(`Error starting matrixclient store: ${err}`);
|
2017-07-05 17:20:21 +02:00
|
|
|
}
|
2017-02-02 15:27:27 +01:00
|
|
|
|
2017-02-10 17:19:39 +01:00
|
|
|
// regardless of errors, start the client. If we did error out, we'll
|
|
|
|
// just end up doing a full initial /sync.
|
2017-07-05 17:20:21 +02:00
|
|
|
|
|
|
|
console.log(`MatrixClientPeg: really starting MatrixClient`);
|
|
|
|
this.get().startClient(opts);
|
|
|
|
console.log(`MatrixClientPeg: MatrixClient started`);
|
2016-08-03 17:41:22 +02:00
|
|
|
}
|
2016-08-03 17:39:47 +02:00
|
|
|
|
2016-08-03 11:46:42 +02:00
|
|
|
getCredentials(): MatrixClientCreds {
|
2016-08-02 15:04:20 +02:00
|
|
|
return {
|
|
|
|
homeserverUrl: this.matrixClient.baseUrl,
|
|
|
|
identityServerUrl: this.matrixClient.idBaseUrl,
|
|
|
|
userId: this.matrixClient.credentials.userId,
|
2016-08-11 15:21:52 +02:00
|
|
|
deviceId: this.matrixClient.getDeviceId(),
|
2016-08-02 15:04:20 +02:00
|
|
|
accessToken: this.matrixClient.getAccessToken(),
|
|
|
|
guest: this.matrixClient.isGuest(),
|
|
|
|
};
|
2016-07-21 18:57:55 +02:00
|
|
|
}
|
|
|
|
|
2016-09-27 20:38:10 +02:00
|
|
|
/**
|
|
|
|
* Return the server name of the user's home server
|
|
|
|
* Throws an error if unable to deduce the home server name
|
|
|
|
* (eg. if the user is not logged in)
|
|
|
|
*/
|
|
|
|
getHomeServerName() {
|
|
|
|
const matches = /^@.+:(.+)$/.exec(this.matrixClient.credentials.userId);
|
|
|
|
if (matches === null || matches.length < 1) {
|
2016-09-28 11:17:47 +02:00
|
|
|
throw new Error("Failed to derive home server name from user ID!");
|
2016-09-27 20:38:10 +02:00
|
|
|
}
|
|
|
|
return matches[1];
|
|
|
|
}
|
|
|
|
|
2016-08-11 14:50:38 +02:00
|
|
|
_createClient(creds: MatrixClientCreds) {
|
2017-10-11 18:56:17 +02:00
|
|
|
const opts = {
|
2016-08-11 14:50:38 +02:00
|
|
|
baseUrl: creds.homeserverUrl,
|
|
|
|
idBaseUrl: creds.identityServerUrl,
|
|
|
|
accessToken: creds.accessToken,
|
|
|
|
userId: creds.userId,
|
2016-08-11 15:21:52 +02:00
|
|
|
deviceId: creds.deviceId,
|
2016-07-21 18:57:55 +02:00
|
|
|
timelineSupport: true,
|
|
|
|
};
|
|
|
|
|
2017-06-21 15:04:43 +02:00
|
|
|
this.matrixClient = createMatrixClient(opts, this.indexedDbWorkerScript);
|
2016-07-21 18:57:55 +02:00
|
|
|
|
|
|
|
// we're going to add eventlisteners for each matrix event tile, so the
|
|
|
|
// potential number of event listeners is quite high.
|
|
|
|
this.matrixClient.setMaxListeners(500);
|
2016-07-25 17:20:03 +02:00
|
|
|
|
2016-08-11 14:50:38 +02:00
|
|
|
this.matrixClient.setGuest(Boolean(creds.guest));
|
2016-09-08 04:02:26 +02:00
|
|
|
|
2017-10-11 18:56:17 +02:00
|
|
|
const notifTimelineSet = new EventTimelineSet(null, {
|
|
|
|
timelineSupport: true,
|
2016-09-08 04:02:26 +02:00
|
|
|
});
|
|
|
|
// XXX: what is our initial pagination token?! it somehow needs to be synchronised with /sync.
|
|
|
|
notifTimelineSet.getLiveTimeline().setPaginationToken("", EventTimeline.BACKWARDS);
|
|
|
|
this.matrixClient.setNotifTimelineSet(notifTimelineSet);
|
2016-07-21 18:57:55 +02:00
|
|
|
}
|
2015-09-28 18:46:49 +02:00
|
|
|
}
|
2015-06-09 18:40:42 +02:00
|
|
|
|
2016-07-21 18:57:55 +02:00
|
|
|
if (!global.mxMatrixClientPeg) {
|
2016-07-22 16:47:47 +02:00
|
|
|
global.mxMatrixClientPeg = new MatrixClientPeg();
|
2015-09-28 18:46:49 +02:00
|
|
|
}
|
2016-07-21 18:57:55 +02:00
|
|
|
module.exports = global.mxMatrixClientPeg;
|