2015-06-23 23:41:25 +08:00
|
|
|
/*
|
2016-01-07 12:06:39 +08:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-06-21 21:04:43 +08:00
|
|
|
Copyright 2017 Vector Creations Ltd.
|
2019-12-05 23:20:30 +08:00
|
|
|
Copyright 2017, 2018, 2019 New Vector Ltd
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2015-06-23 23:41:25 +08: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-09-18 16:27:43 +08:00
|
|
|
import {MatrixClient, MemoryStore} from 'matrix-js-sdk';
|
2018-10-04 20:40:56 +08:00
|
|
|
|
2016-08-03 23:45:23 +08:00
|
|
|
import utils from 'matrix-js-sdk/lib/utils';
|
2016-09-08 10:02:26 +08:00
|
|
|
import EventTimeline from 'matrix-js-sdk/lib/models/event-timeline';
|
|
|
|
import EventTimelineSet from 'matrix-js-sdk/lib/models/event-timeline-set';
|
2018-11-16 19:31:46 +08:00
|
|
|
import sdk from './index';
|
2017-06-13 19:46:49 +08:00
|
|
|
import createMatrixClient from './utils/createMatrixClient';
|
2017-11-18 01:48:42 +08:00
|
|
|
import SettingsStore from './settings/SettingsStore';
|
2017-12-08 01:10:45 +08:00
|
|
|
import MatrixActionCreators from './actions/MatrixActionCreators';
|
2018-11-16 19:31:46 +08:00
|
|
|
import Modal from './Modal';
|
2019-01-16 02:08:13 +08:00
|
|
|
import {verificationMethods} from 'matrix-js-sdk/lib/crypto';
|
2019-02-23 07:33:20 +08:00
|
|
|
import MatrixClientBackedSettingsHandler from "./settings/handlers/MatrixClientBackedSettingsHandler";
|
2019-04-04 18:59:53 +08:00
|
|
|
import * as StorageManager from './utils/StorageManager';
|
2019-08-22 21:44:09 +08:00
|
|
|
import IdentityAuthClient from './IdentityAuthClient';
|
2019-12-11 23:05:03 +08:00
|
|
|
import { crossSigningCallbacks } from './CrossSigningManager';
|
2015-07-20 20:19:47 +08:00
|
|
|
|
2016-08-03 17:46:42 +08:00
|
|
|
interface MatrixClientCreds {
|
|
|
|
homeserverUrl: string,
|
|
|
|
identityServerUrl: string,
|
|
|
|
userId: string,
|
2016-08-11 21:21:52 +08:00
|
|
|
deviceId: string,
|
2016-08-03 17:46:42 +08:00
|
|
|
accessToken: string,
|
|
|
|
guest: boolean,
|
|
|
|
}
|
|
|
|
|
2016-07-22 22:47:47 +08: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-22 00:57:55 +08:00
|
|
|
class MatrixClientPeg {
|
2016-07-22 22:47:47 +08:00
|
|
|
constructor() {
|
2016-07-22 00:57:55 +08:00
|
|
|
this.matrixClient = null;
|
2019-06-14 22:31:19 +08:00
|
|
|
this._justRegisteredUserId = null;
|
2016-08-02 21:04:20 +08:00
|
|
|
|
2016-08-04 00:23:09 +08: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 21:04:20 +08:00
|
|
|
this.opts = {
|
|
|
|
initialSyncLimit: 20,
|
|
|
|
};
|
2018-10-04 20:40:56 +08:00
|
|
|
// the credentials used to init the current client object.
|
|
|
|
// used if we tear it down & recreate it with a different store
|
|
|
|
this._currentClientCreds = null;
|
2017-04-06 18:13:39 +08: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-22 00:43:34 +08:00
|
|
|
createMatrixClient.indexedDbWorkerScript = script;
|
2015-12-04 18:37:53 +08:00
|
|
|
}
|
|
|
|
|
2016-06-12 19:32:46 +08:00
|
|
|
get(): MatrixClient {
|
2016-07-22 00:57:55 +08:00
|
|
|
return this.matrixClient;
|
2015-09-29 00:46:49 +08:00
|
|
|
}
|
2015-06-10 00:40:42 +08:00
|
|
|
|
2015-09-29 00:46:49 +08:00
|
|
|
unset() {
|
2016-07-22 00:57:55 +08:00
|
|
|
this.matrixClient = null;
|
2017-12-08 01:10:45 +08:00
|
|
|
|
|
|
|
MatrixActionCreators.stop();
|
2015-09-29 00:46:49 +08:00
|
|
|
}
|
2015-09-16 20:48:24 +08:00
|
|
|
|
2019-09-18 16:27:43 +08:00
|
|
|
/**
|
2019-06-14 22:31:19 +08:00
|
|
|
* If we've registered a user ID we set this to the ID of the
|
|
|
|
* user we've just registered. If they then go & log in, we
|
|
|
|
* can send them to the welcome user (obviously this doesn't
|
|
|
|
* guarentee they'll get a chat with the welcome user).
|
|
|
|
*
|
|
|
|
* @param {string} uid The user ID of the user we've just registered
|
|
|
|
*/
|
|
|
|
setJustRegisteredUserId(uid) {
|
|
|
|
this._justRegisteredUserId = uid;
|
|
|
|
}
|
|
|
|
|
2019-09-18 16:27:43 +08:00
|
|
|
/**
|
2019-06-14 22:31:19 +08:00
|
|
|
* Returns true if the current user has just been registered by this
|
|
|
|
* client as determined by setJustRegisteredUserId()
|
|
|
|
*
|
|
|
|
* @returns {bool} True if user has just been registered
|
|
|
|
*/
|
|
|
|
currentUserIsJustRegistered() {
|
|
|
|
return (
|
|
|
|
this.matrixClient &&
|
|
|
|
this.matrixClient.credentials.userId === this._justRegisteredUserId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-18 16:27:43 +08:00
|
|
|
/*
|
2016-07-22 22:47:47 +08:00
|
|
|
* Replace this MatrixClientPeg's client with a client instance that has
|
2019-02-01 08:52:39 +08:00
|
|
|
* homeserver / identity server URLs and active credentials
|
2016-07-22 22:47:47 +08:00
|
|
|
*/
|
2016-08-03 17:46:42 +08:00
|
|
|
replaceUsingCreds(creds: MatrixClientCreds) {
|
2018-10-04 20:40:56 +08:00
|
|
|
this._currentClientCreds = creds;
|
2016-08-11 20:50:38 +08:00
|
|
|
this._createClient(creds);
|
2016-07-25 23:28:28 +08:00
|
|
|
}
|
|
|
|
|
2019-07-05 06:45:40 +08:00
|
|
|
async assign() {
|
2018-10-04 20:40:56 +08:00
|
|
|
for (const dbType of ['indexeddb', 'memory']) {
|
|
|
|
try {
|
|
|
|
const promise = this.matrixClient.store.startup();
|
|
|
|
console.log("MatrixClientPeg: waiting for MatrixClient store to initialise");
|
|
|
|
await promise;
|
|
|
|
break;
|
|
|
|
} catch (err) {
|
|
|
|
if (dbType === 'indexeddb') {
|
|
|
|
console.error('Error starting matrixclient store - falling back to memory store', err);
|
2019-09-18 16:27:43 +08:00
|
|
|
this.matrixClient.store = new MemoryStore({
|
2019-07-05 06:45:40 +08:00
|
|
|
localStorage: global.localStorage,
|
2018-10-04 20:40:56 +08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.error('Failed to start memory store!', err);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 18:38:27 +08:00
|
|
|
StorageManager.trackStores(this.matrixClient);
|
|
|
|
|
2017-07-19 06:46:03 +08:00
|
|
|
// try to initialise e2e on the new client
|
|
|
|
try {
|
|
|
|
// check that we have a version of the js-sdk which includes initCrypto
|
2019-02-09 00:44:03 +08:00
|
|
|
if (!SettingsStore.getValue("lowBandwidth") && this.matrixClient.initCrypto) {
|
2017-07-19 06:46:03 +08:00
|
|
|
await this.matrixClient.initCrypto();
|
2019-05-15 20:47:48 +08:00
|
|
|
StorageManager.setCryptoInitialised(true);
|
2017-07-19 06:46:03 +08:00
|
|
|
}
|
2017-11-16 21:19:36 +08:00
|
|
|
} catch (e) {
|
2019-04-02 19:31:18 +08:00
|
|
|
if (e && e.name === 'InvalidCryptoStoreError') {
|
2018-11-16 19:31:46 +08:00
|
|
|
// The js-sdk found a crypto DB too new for it to use
|
|
|
|
const CryptoStoreTooNewDialog =
|
|
|
|
sdk.getComponent("views.dialogs.CryptoStoreTooNewDialog");
|
|
|
|
Modal.createDialog(CryptoStoreTooNewDialog, {
|
|
|
|
host: window.location.host,
|
|
|
|
});
|
|
|
|
}
|
2017-07-19 06:46:03 +08:00
|
|
|
// this can happen for a number of reasons, the most likely being
|
|
|
|
// that the olm library was missing. It's not fatal.
|
2019-04-02 19:31:18 +08:00
|
|
|
console.warn("Unable to initialise e2e", e);
|
2017-07-19 06:46:03 +08:00
|
|
|
}
|
|
|
|
|
2016-08-03 23:45:23 +08:00
|
|
|
const opts = utils.deepCopy(this.opts);
|
2016-08-04 00:23:09 +08:00
|
|
|
// the react sdk doesn't work without this, so don't allow
|
2016-08-03 23:39:47 +08:00
|
|
|
opts.pendingEventOrdering = "detached";
|
2019-02-08 02:24:07 +08:00
|
|
|
opts.lazyLoadMembers = true;
|
2017-02-02 22:27:27 +08:00
|
|
|
|
2019-02-23 07:33:20 +08:00
|
|
|
// Connect the matrix client to the dispatcher and setting handlers
|
2017-12-08 01:10:45 +08:00
|
|
|
MatrixActionCreators.start(this.matrixClient);
|
2019-02-23 07:33:20 +08:00
|
|
|
MatrixClientBackedSettingsHandler.matrixClient = this.matrixClient;
|
2017-12-08 01:10:45 +08:00
|
|
|
|
2019-07-05 06:45:40 +08:00
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
async start() {
|
|
|
|
const opts = await this.assign();
|
|
|
|
|
2017-07-05 23:20:21 +08:00
|
|
|
console.log(`MatrixClientPeg: really starting MatrixClient`);
|
2018-08-03 01:38:41 +08:00
|
|
|
await this.get().startClient(opts);
|
2017-07-05 23:20:21 +08:00
|
|
|
console.log(`MatrixClientPeg: MatrixClient started`);
|
2016-08-03 23:41:22 +08:00
|
|
|
}
|
2016-08-03 23:39:47 +08:00
|
|
|
|
2016-08-03 17:46:42 +08:00
|
|
|
getCredentials(): MatrixClientCreds {
|
2016-08-02 21:04:20 +08:00
|
|
|
return {
|
|
|
|
homeserverUrl: this.matrixClient.baseUrl,
|
|
|
|
identityServerUrl: this.matrixClient.idBaseUrl,
|
|
|
|
userId: this.matrixClient.credentials.userId,
|
2016-08-11 21:21:52 +08:00
|
|
|
deviceId: this.matrixClient.getDeviceId(),
|
2016-08-02 21:04:20 +08:00
|
|
|
accessToken: this.matrixClient.getAccessToken(),
|
|
|
|
guest: this.matrixClient.isGuest(),
|
|
|
|
};
|
2016-07-22 00:57:55 +08:00
|
|
|
}
|
|
|
|
|
2019-09-18 16:27:43 +08:00
|
|
|
/*
|
2019-02-01 08:52:39 +08:00
|
|
|
* Return the server name of the user's homeserver
|
|
|
|
* Throws an error if unable to deduce the homeserver name
|
2016-09-28 02:38:10 +08:00
|
|
|
* (eg. if the user is not logged in)
|
|
|
|
*/
|
2019-07-06 04:35:21 +08:00
|
|
|
getHomeserverName() {
|
2016-09-28 02:38:10 +08:00
|
|
|
const matches = /^@.+:(.+)$/.exec(this.matrixClient.credentials.userId);
|
|
|
|
if (matches === null || matches.length < 1) {
|
2019-02-01 08:52:39 +08:00
|
|
|
throw new Error("Failed to derive homeserver name from user ID!");
|
2016-09-28 02:38:10 +08:00
|
|
|
}
|
|
|
|
return matches[1];
|
|
|
|
}
|
|
|
|
|
2019-03-26 00:22:53 +08:00
|
|
|
_createClient(creds: MatrixClientCreds) {
|
2017-10-12 00:56:17 +08:00
|
|
|
const opts = {
|
2016-08-11 20:50:38 +08:00
|
|
|
baseUrl: creds.homeserverUrl,
|
|
|
|
idBaseUrl: creds.identityServerUrl,
|
|
|
|
accessToken: creds.accessToken,
|
|
|
|
userId: creds.userId,
|
2016-08-11 21:21:52 +08:00
|
|
|
deviceId: creds.deviceId,
|
2016-07-22 00:57:55 +08:00
|
|
|
timelineSupport: true,
|
2019-02-13 19:51:32 +08:00
|
|
|
forceTURN: !SettingsStore.getValue('webRtcAllowPeerToPeer', false),
|
2019-08-14 21:02:25 +08:00
|
|
|
fallbackICEServerAllowed: !!SettingsStore.getValue('fallbackICEServerAllowed'),
|
2019-05-08 22:06:48 +08:00
|
|
|
verificationMethods: [verificationMethods.SAS],
|
2019-07-17 23:56:15 +08:00
|
|
|
unstableClientRelationAggregation: true,
|
2019-08-23 18:17:51 +08:00
|
|
|
identityServer: new IdentityAuthClient(),
|
2016-07-22 00:57:55 +08:00
|
|
|
};
|
|
|
|
|
2019-12-05 23:20:30 +08:00
|
|
|
opts.cryptoCallbacks = {};
|
2019-11-15 18:57:20 +08:00
|
|
|
if (SettingsStore.isFeatureEnabled("feature_cross_signing")) {
|
2019-12-11 23:05:03 +08:00
|
|
|
Object.assign(opts.cryptoCallbacks, crossSigningCallbacks);
|
2019-11-15 18:57:20 +08:00
|
|
|
}
|
|
|
|
|
2019-03-26 00:22:53 +08:00
|
|
|
this.matrixClient = createMatrixClient(opts);
|
2016-07-22 00:57:55 +08: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 23:20:03 +08:00
|
|
|
|
2016-08-11 20:50:38 +08:00
|
|
|
this.matrixClient.setGuest(Boolean(creds.guest));
|
2016-09-08 10:02:26 +08:00
|
|
|
|
2017-10-12 00:56:17 +08:00
|
|
|
const notifTimelineSet = new EventTimelineSet(null, {
|
|
|
|
timelineSupport: true,
|
2016-09-08 10:02:26 +08: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-22 00:57:55 +08:00
|
|
|
}
|
2015-09-29 00:46:49 +08:00
|
|
|
}
|
2015-06-10 00:40:42 +08:00
|
|
|
|
2016-07-22 00:57:55 +08:00
|
|
|
if (!global.mxMatrixClientPeg) {
|
2016-07-22 22:47:47 +08:00
|
|
|
global.mxMatrixClientPeg = new MatrixClientPeg();
|
2015-09-29 00:46:49 +08:00
|
|
|
}
|
2018-02-28 22:52:53 +08:00
|
|
|
export default global.mxMatrixClientPeg;
|