Skip to content
Snippets Groups Projects
Verified Commit 76ac3298 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Fix initialisation of matrix backend

parent 9a6dbbfc
No related branches found
No related tags found
No related merge requests found
Pipeline #7315 failed
......@@ -15,12 +15,21 @@
import {BaseBackend} from './base';
import * as sdk from 'matrix-js-sdk';
import * as utils from '../utils';
class MatrixBackend extends BaseBackend {
constructor(userId, homeServerUrl) {
constructor(userId) {
super(userId);
}
set userId(value) {
if (value !== undefined) {
const matrixId = utils.splitMatrixId(value);
const homeServerUrl = 'https://' + matrixId.homeServer
this.api = sdk.createClient(homeServerUrl);
this.api = sdk.createClient(homeServerUrl);
this.userId = value;
}
}
}
......
/* Copyright 2021 Dominik George <nik@naturalnet.de>
*
* 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.
*/
/**
* Split a Matrix ID into local part and homeserver
*
* @param {string} matrixId
*
* @example
* splitMatrixId('@foo:example.com')
* // => {'localPart': 'foo', 'homeServer': 'example.com'}
*/
export function splitMatrixId(matrixId) {
if (typeof matrixId !== 'string') {
throw 'Passed Matrix ID must be a string';
} else if (matrixId.substr(0, 1) !== '@') {
throw 'Passed Matrix ID must begin with @';
} else if (matrixId.indexOf(':') < 2) {
throw 'Passed Matrix ID must contain : to separate local part and home server';
}
const localPart = matrixId.substr(1).substr(0, matrixId.indexOf(':'));
const homeServer = matrixId.substr(matrixId.indexOf(':') + 1);
return {
'localPart': localPart,
'homeServer': homeServer
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment