2015-06-23 23:41:25 +08:00
|
|
|
/*
|
2016-01-07 12:06:39 +08:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2017-02-02 22:27:27 +08:00
|
|
|
import q from "q";
|
2016-07-22 00:57:55 +08:00
|
|
|
import Matrix from 'matrix-js-sdk';
|
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';
|
2015-06-10 00:40:42 +08:00
|
|
|
|
2016-07-22 00:57:55 +08:00
|
|
|
const localStorage = window.localStorage;
|
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;
|
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,
|
|
|
|
};
|
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;
|
2015-09-29 00:46:49 +08:00
|
|
|
}
|
2015-09-16 20:48:24 +08:00
|
|
|
|
2016-07-22 22:47:47 +08:00
|
|
|
/**
|
|
|
|
* Replace this MatrixClientPeg's client with a client instance that has
|
|
|
|
* Home Server / Identity Server URLs and active credentials
|
|
|
|
*/
|
2016-08-03 17:46:42 +08:00
|
|
|
replaceUsingCreds(creds: MatrixClientCreds) {
|
2016-08-11 20:50:38 +08:00
|
|
|
this._createClient(creds);
|
2016-07-25 23:28:28 +08:00
|
|
|
}
|
|
|
|
|
2016-08-03 23:39:47 +08:00
|
|
|
start() {
|
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";
|
2017-02-02 22:27:27 +08:00
|
|
|
|
2017-02-17 18:43:55 +08:00
|
|
|
let promise = this.matrixClient.store.startup();
|
|
|
|
// log any errors when starting up the database (if one exists)
|
2017-02-10 22:22:54 +08:00
|
|
|
promise.catch((err) => { console.error(err); });
|
2017-02-02 22:27:27 +08:00
|
|
|
|
2017-02-11 00:19:39 +08:00
|
|
|
// regardless of errors, start the client. If we did error out, we'll
|
|
|
|
// just end up doing a full initial /sync.
|
2017-02-02 22:27:27 +08:00
|
|
|
promise.finally(() => {
|
|
|
|
this.get().startClient(opts);
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
2016-09-28 02:38:10 +08: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 17:17:47 +08:00
|
|
|
throw new Error("Failed to derive home server name from user ID!");
|
2016-09-28 02:38:10 +08:00
|
|
|
}
|
|
|
|
return matches[1];
|
|
|
|
}
|
|
|
|
|
2016-08-11 20:50:38 +08:00
|
|
|
_createClient(creds: MatrixClientCreds) {
|
2016-07-22 00:57:55 +08:00
|
|
|
var 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,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (localStorage) {
|
|
|
|
opts.sessionStore = new Matrix.WebStorageSessionStore(localStorage);
|
|
|
|
}
|
2017-02-02 22:27:27 +08:00
|
|
|
if (window.indexedDB && localStorage) {
|
2017-02-21 23:44:57 +08:00
|
|
|
// FIXME: bodge to remove old database. Remove this after a few weeks.
|
|
|
|
window.indexedDB.deleteDatabase("matrix-js-sdk:default");
|
|
|
|
|
2017-03-31 21:55:33 +08:00
|
|
|
opts.store = new Matrix.IndexedDBStore({
|
|
|
|
indexedDB: window.indexedDB,
|
|
|
|
dbName: "riot-web-sync",
|
|
|
|
localStorage: localStorage,
|
|
|
|
});
|
2017-02-02 22:27:27 +08:00
|
|
|
}
|
2016-07-22 00:57:55 +08:00
|
|
|
|
|
|
|
this.matrixClient = Matrix.createClient(opts);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2016-09-09 05:48:44 +08:00
|
|
|
var notifTimelineSet = new EventTimelineSet(null, {
|
2016-09-08 10:02:26 +08:00
|
|
|
timelineSupport: true
|
|
|
|
});
|
|
|
|
// 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
|
|
|
}
|
2016-07-22 00:57:55 +08:00
|
|
|
module.exports = global.mxMatrixClientPeg;
|