mirror of https://github.com/vector-im/riot-web
Merge pull request #4638 from matrix-org/travis/matrix-client-peg-ts
Convert MatrixClientPeg to TypeScriptpull/21833/head
commit
3f2ab5a3a9
|
@ -15,10 +15,12 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as ModernizrStatic from "modernizr";
|
import * as ModernizrStatic from "modernizr";
|
||||||
|
import { IMatrixClientPeg } from "../MatrixClientPeg";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
Modernizr: ModernizrStatic;
|
Modernizr: ModernizrStatic;
|
||||||
|
mxMatrixClientPeg: IMatrixClientPeg;
|
||||||
Olm: {
|
Olm: {
|
||||||
init: () => Promise<void>;
|
init: () => Promise<void>;
|
||||||
};
|
};
|
||||||
|
|
|
@ -17,8 +17,8 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {MatrixClient, MemoryStore} from 'matrix-js-sdk';
|
import {MatrixClient} from 'matrix-js-sdk/src/client';
|
||||||
|
import {MemoryStore} from 'matrix-js-sdk/src/store/memory';
|
||||||
import * as utils from 'matrix-js-sdk/src/utils';
|
import * as utils from 'matrix-js-sdk/src/utils';
|
||||||
import {EventTimeline} from 'matrix-js-sdk/src/models/event-timeline';
|
import {EventTimeline} from 'matrix-js-sdk/src/models/event-timeline';
|
||||||
import {EventTimelineSet} from 'matrix-js-sdk/src/models/event-timeline-set';
|
import {EventTimelineSet} from 'matrix-js-sdk/src/models/event-timeline-set';
|
||||||
|
@ -34,7 +34,7 @@ import IdentityAuthClient from './IdentityAuthClient';
|
||||||
import { crossSigningCallbacks } from './CrossSigningManager';
|
import { crossSigningCallbacks } from './CrossSigningManager';
|
||||||
import {SHOW_QR_CODE_METHOD} from "matrix-js-sdk/src/crypto/verification/QRCode";
|
import {SHOW_QR_CODE_METHOD} from "matrix-js-sdk/src/crypto/verification/QRCode";
|
||||||
|
|
||||||
interface MatrixClientCreds {
|
export interface IMatrixClientCreds {
|
||||||
homeserverUrl: string,
|
homeserverUrl: string,
|
||||||
identityServerUrl: string,
|
identityServerUrl: string,
|
||||||
userId: string,
|
userId: string,
|
||||||
|
@ -43,28 +43,15 @@ interface MatrixClientCreds {
|
||||||
guest: boolean,
|
guest: boolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// TODO: Move this to the js-sdk
|
||||||
* Wrapper object for handling the js-sdk Matrix Client object in the react-sdk
|
export interface IOpts {
|
||||||
* Handles the creation/initialisation of client objects.
|
initialSyncLimit?: number;
|
||||||
* This module provides a singleton instance of this class so the 'current'
|
pendingEventOrdering?: "detached" | "chronological";
|
||||||
* Matrix Client object is available easily.
|
lazyLoadMembers?: boolean;
|
||||||
*/
|
}
|
||||||
class _MatrixClientPeg {
|
|
||||||
constructor() {
|
|
||||||
this.matrixClient = null;
|
|
||||||
this._justRegisteredUserId = null;
|
|
||||||
|
|
||||||
// These are the default options used when when the
|
export interface IMatrixClientPeg {
|
||||||
// client is started in 'start'. These can be altered
|
opts: IOpts;
|
||||||
// at any time up to after the 'will_start_client'
|
|
||||||
// event is finished processing.
|
|
||||||
this.opts = {
|
|
||||||
initialSyncLimit: 20,
|
|
||||||
};
|
|
||||||
// the credentials used to init the current client object.
|
|
||||||
// used if we tear it down & recreate it with a different store
|
|
||||||
this._currentClientCreds = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the script href passed to the IndexedDB web worker
|
* Sets the script href passed to the IndexedDB web worker
|
||||||
|
@ -73,19 +60,23 @@ class _MatrixClientPeg {
|
||||||
*
|
*
|
||||||
* @param {string} script href to the script to be passed to the web worker
|
* @param {string} script href to the script to be passed to the web worker
|
||||||
*/
|
*/
|
||||||
setIndexedDbWorkerScript(script) {
|
setIndexedDbWorkerScript(script: string): void;
|
||||||
createMatrixClient.indexedDbWorkerScript = script;
|
|
||||||
}
|
|
||||||
|
|
||||||
get(): MatrixClient {
|
/**
|
||||||
return this.matrixClient;
|
* Return the server name of the user's homeserver
|
||||||
}
|
* Throws an error if unable to deduce the homeserver name
|
||||||
|
* (eg. if the user is not logged in)
|
||||||
|
*
|
||||||
|
* @returns {string} The homeserver name, if present.
|
||||||
|
*/
|
||||||
|
getHomeserverName(): string;
|
||||||
|
|
||||||
unset() {
|
get(): MatrixClient;
|
||||||
this.matrixClient = null;
|
unset(): void;
|
||||||
|
assign(): Promise<any>;
|
||||||
|
start(): Promise<any>;
|
||||||
|
|
||||||
MatrixActionCreators.stop();
|
getCredentials(): IMatrixClientCreds;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If we've registered a user ID we set this to the ID of the
|
* If we've registered a user ID we set this to the ID of the
|
||||||
|
@ -95,9 +86,7 @@ class _MatrixClientPeg {
|
||||||
*
|
*
|
||||||
* @param {string} uid The user ID of the user we've just registered
|
* @param {string} uid The user ID of the user we've just registered
|
||||||
*/
|
*/
|
||||||
setJustRegisteredUserId(uid) {
|
setJustRegisteredUserId(uid: string): void;
|
||||||
this._justRegisteredUserId = uid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the current user has just been registered by this
|
* Returns true if the current user has just been registered by this
|
||||||
|
@ -105,23 +94,73 @@ class _MatrixClientPeg {
|
||||||
*
|
*
|
||||||
* @returns {bool} True if user has just been registered
|
* @returns {bool} True if user has just been registered
|
||||||
*/
|
*/
|
||||||
currentUserIsJustRegistered() {
|
currentUserIsJustRegistered(): boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace this MatrixClientPeg's client with a client instance that has
|
||||||
|
* homeserver / identity server URLs and active credentials
|
||||||
|
*
|
||||||
|
* @param {IMatrixClientCreds} creds The new credentials to use.
|
||||||
|
*/
|
||||||
|
replaceUsingCreds(creds: IMatrixClientCreds): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
class _MatrixClientPeg implements IMatrixClientPeg {
|
||||||
|
// 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.
|
||||||
|
public opts: IOpts = {
|
||||||
|
initialSyncLimit: 20,
|
||||||
|
};
|
||||||
|
|
||||||
|
private matrixClient: MatrixClient = null;
|
||||||
|
private justRegisteredUserId: string;
|
||||||
|
|
||||||
|
// the credentials used to init the current client object.
|
||||||
|
// used if we tear it down & recreate it with a different store
|
||||||
|
private currentClientCreds: IMatrixClientCreds;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public setIndexedDbWorkerScript(script: string): void {
|
||||||
|
createMatrixClient.indexedDbWorkerScript = script;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get(): MatrixClient {
|
||||||
|
return this.matrixClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public unset(): void {
|
||||||
|
this.matrixClient = null;
|
||||||
|
|
||||||
|
MatrixActionCreators.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
public setJustRegisteredUserId(uid: string): void {
|
||||||
|
this.justRegisteredUserId = uid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public currentUserIsJustRegistered(): boolean {
|
||||||
return (
|
return (
|
||||||
this.matrixClient &&
|
this.matrixClient &&
|
||||||
this.matrixClient.credentials.userId === this._justRegisteredUserId
|
this.matrixClient.credentials.userId === this.justRegisteredUserId
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
public replaceUsingCreds(creds: IMatrixClientCreds): void {
|
||||||
* Replace this MatrixClientPeg's client with a client instance that has
|
this.currentClientCreds = creds;
|
||||||
* homeserver / identity server URLs and active credentials
|
this.createClient(creds);
|
||||||
*/
|
|
||||||
replaceUsingCreds(creds: MatrixClientCreds) {
|
|
||||||
this._currentClientCreds = creds;
|
|
||||||
this._createClient(creds);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async assign() {
|
public async assign(): Promise<any> {
|
||||||
for (const dbType of ['indexeddb', 'memory']) {
|
for (const dbType of ['indexeddb', 'memory']) {
|
||||||
try {
|
try {
|
||||||
const promise = this.matrixClient.store.startup();
|
const promise = this.matrixClient.store.startup();
|
||||||
|
@ -132,7 +171,7 @@ class _MatrixClientPeg {
|
||||||
if (dbType === 'indexeddb') {
|
if (dbType === 'indexeddb') {
|
||||||
console.error('Error starting matrixclient store - falling back to memory store', err);
|
console.error('Error starting matrixclient store - falling back to memory store', err);
|
||||||
this.matrixClient.store = new MemoryStore({
|
this.matrixClient.store = new MemoryStore({
|
||||||
localStorage: global.localStorage,
|
localStorage: localStorage,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.error('Failed to start memory store!', err);
|
console.error('Failed to start memory store!', err);
|
||||||
|
@ -179,7 +218,7 @@ class _MatrixClientPeg {
|
||||||
return opts;
|
return opts;
|
||||||
}
|
}
|
||||||
|
|
||||||
async start() {
|
public async start(): Promise<any> {
|
||||||
const opts = await this.assign();
|
const opts = await this.assign();
|
||||||
|
|
||||||
console.log(`MatrixClientPeg: really starting MatrixClient`);
|
console.log(`MatrixClientPeg: really starting MatrixClient`);
|
||||||
|
@ -187,7 +226,7 @@ class _MatrixClientPeg {
|
||||||
console.log(`MatrixClientPeg: MatrixClient started`);
|
console.log(`MatrixClientPeg: MatrixClient started`);
|
||||||
}
|
}
|
||||||
|
|
||||||
getCredentials(): MatrixClientCreds {
|
public getCredentials(): IMatrixClientCreds {
|
||||||
return {
|
return {
|
||||||
homeserverUrl: this.matrixClient.baseUrl,
|
homeserverUrl: this.matrixClient.baseUrl,
|
||||||
identityServerUrl: this.matrixClient.idBaseUrl,
|
identityServerUrl: this.matrixClient.idBaseUrl,
|
||||||
|
@ -198,12 +237,7 @@ class _MatrixClientPeg {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
public getHomeserverName(): string {
|
||||||
* Return the server name of the user's homeserver
|
|
||||||
* Throws an error if unable to deduce the homeserver name
|
|
||||||
* (eg. if the user is not logged in)
|
|
||||||
*/
|
|
||||||
getHomeserverName() {
|
|
||||||
const matches = /^@.+:(.+)$/.exec(this.matrixClient.credentials.userId);
|
const matches = /^@.+:(.+)$/.exec(this.matrixClient.credentials.userId);
|
||||||
if (matches === null || matches.length < 1) {
|
if (matches === null || matches.length < 1) {
|
||||||
throw new Error("Failed to derive homeserver name from user ID!");
|
throw new Error("Failed to derive homeserver name from user ID!");
|
||||||
|
@ -211,7 +245,8 @@ class _MatrixClientPeg {
|
||||||
return matches[1];
|
return matches[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
_createClient(creds: MatrixClientCreds) {
|
private createClient(creds: IMatrixClientCreds): void {
|
||||||
|
// TODO: Make these opts typesafe with the js-sdk
|
||||||
const opts = {
|
const opts = {
|
||||||
baseUrl: creds.homeserverUrl,
|
baseUrl: creds.homeserverUrl,
|
||||||
idBaseUrl: creds.identityServerUrl,
|
idBaseUrl: creds.identityServerUrl,
|
||||||
|
@ -228,9 +263,9 @@ class _MatrixClientPeg {
|
||||||
],
|
],
|
||||||
unstableClientRelationAggregation: true,
|
unstableClientRelationAggregation: true,
|
||||||
identityServer: new IdentityAuthClient(),
|
identityServer: new IdentityAuthClient(),
|
||||||
|
cryptoCallbacks: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
opts.cryptoCallbacks = {};
|
|
||||||
// These are always installed regardless of the labs flag so that
|
// These are always installed regardless of the labs flag so that
|
||||||
// cross-signing features can toggle on without reloading and also be
|
// cross-signing features can toggle on without reloading and also be
|
||||||
// accessed immediately after login.
|
// accessed immediately after login.
|
||||||
|
@ -253,8 +288,8 @@ class _MatrixClientPeg {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!global.mxMatrixClientPeg) {
|
if (!window.mxMatrixClientPeg) {
|
||||||
global.mxMatrixClientPeg = new _MatrixClientPeg();
|
window.mxMatrixClientPeg = new _MatrixClientPeg();
|
||||||
}
|
}
|
||||||
|
|
||||||
export const MatrixClientPeg = global.mxMatrixClientPeg;
|
export const MatrixClientPeg = window.mxMatrixClientPeg;
|
|
@ -29,7 +29,7 @@ import { fixupColorFonts } from '../../utils/FontManager';
|
||||||
import * as sdk from '../../index';
|
import * as sdk from '../../index';
|
||||||
import dis from '../../dispatcher/dispatcher';
|
import dis from '../../dispatcher/dispatcher';
|
||||||
import sessionStore from '../../stores/SessionStore';
|
import sessionStore from '../../stores/SessionStore';
|
||||||
import {MatrixClientPeg, MatrixClientCreds} from '../../MatrixClientPeg';
|
import {MatrixClientPeg, IMatrixClientCreds} from '../../MatrixClientPeg';
|
||||||
import SettingsStore from "../../settings/SettingsStore";
|
import SettingsStore from "../../settings/SettingsStore";
|
||||||
|
|
||||||
import TagOrderActions from '../../actions/TagOrderActions';
|
import TagOrderActions from '../../actions/TagOrderActions';
|
||||||
|
@ -57,7 +57,7 @@ function canElementReceiveInput(el) {
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
matrixClient: MatrixClient;
|
matrixClient: MatrixClient;
|
||||||
onRegistered: (credentials: MatrixClientCreds) => Promise<MatrixClient>;
|
onRegistered: (credentials: IMatrixClientCreds) => Promise<MatrixClient>;
|
||||||
viaServers?: string[];
|
viaServers?: string[];
|
||||||
hideToSRUsers: boolean;
|
hideToSRUsers: boolean;
|
||||||
resizeNotifier: ResizeNotifier;
|
resizeNotifier: ResizeNotifier;
|
||||||
|
|
Loading…
Reference in New Issue