2020-04-07 01:04:41 +03:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import SdkConfig from "../SdkConfig";
|
|
|
|
import {MatrixClientPeg} from "../MatrixClientPeg";
|
|
|
|
|
|
|
|
const JITSI_WK_PROPERTY = "im.vector.riot.jitsi";
|
|
|
|
|
2020-04-10 01:02:49 +03:00
|
|
|
export interface JitsiWidgetData {
|
|
|
|
conferenceId: string;
|
|
|
|
isAudioOnly: boolean;
|
|
|
|
domain: string;
|
|
|
|
}
|
|
|
|
|
2020-04-07 01:04:41 +03:00
|
|
|
export class Jitsi {
|
|
|
|
private static instance: Jitsi;
|
|
|
|
|
|
|
|
private domain: string;
|
|
|
|
|
|
|
|
public get preferredDomain(): string {
|
|
|
|
return this.domain || 'jitsi.riot.im';
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:03:30 +03:00
|
|
|
/**
|
|
|
|
* Checks for auth needed by looking up a well-known file
|
|
|
|
*
|
|
|
|
* If the file does not exist, we assume no auth.
|
|
|
|
*
|
2020-09-07 19:22:40 +03:00
|
|
|
* See https://github.com/matrix-org/prosody-mod-auth-matrix-user-verification
|
2020-09-04 10:03:30 +03:00
|
|
|
*/
|
|
|
|
public async getJitsiAuth(): Promise<string|null> {
|
|
|
|
if (!this.preferredDomain) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let data;
|
|
|
|
try {
|
|
|
|
const response = await fetch(`https://${this.preferredDomain}/.well-known/element/jitsi`);
|
|
|
|
data = await response.json();
|
|
|
|
} catch (error) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (data.auth) {
|
|
|
|
return data.auth;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-03 15:58:17 +03:00
|
|
|
public start() {
|
2020-06-02 00:22:01 +03:00
|
|
|
const cli = MatrixClientPeg.get();
|
2020-06-03 15:58:17 +03:00
|
|
|
cli.on("WellKnown.client", this.update);
|
2020-06-03 16:09:45 +03:00
|
|
|
// call update initially in case we missed the first WellKnown.client event and for if no well-known present
|
|
|
|
this.update(cli.getClientWellKnown());
|
2020-06-03 15:58:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private update = async (discoveryResponse): Promise<any> => {
|
|
|
|
// Start with a default of the config's domain
|
|
|
|
let domain = (SdkConfig.get()['jitsi'] || {})['preferredDomain'] || 'jitsi.riot.im';
|
2020-06-02 00:22:01 +03:00
|
|
|
|
|
|
|
console.log("Attempting to get Jitsi conference information from homeserver");
|
|
|
|
if (discoveryResponse && discoveryResponse[JITSI_WK_PROPERTY]) {
|
|
|
|
const wkPreferredDomain = discoveryResponse[JITSI_WK_PROPERTY]['preferredDomain'];
|
|
|
|
if (wkPreferredDomain) domain = wkPreferredDomain;
|
2020-04-07 01:04:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put the result into memory for us to use later
|
|
|
|
this.domain = domain;
|
|
|
|
console.log("Jitsi conference domain:", this.preferredDomain);
|
2020-06-03 15:58:17 +03:00
|
|
|
};
|
2020-04-07 01:04:41 +03:00
|
|
|
|
2020-04-10 01:02:49 +03:00
|
|
|
/**
|
|
|
|
* Parses the given URL into the data needed for a Jitsi widget, if the widget
|
|
|
|
* URL matches the preferredDomain for the app.
|
|
|
|
* @param {string} url The URL to parse.
|
|
|
|
* @returns {JitsiWidgetData} The widget data if eligible, otherwise null.
|
|
|
|
*/
|
|
|
|
public parsePreferredConferenceUrl(url: string): JitsiWidgetData {
|
|
|
|
const parsed = new URL(url);
|
|
|
|
if (parsed.hostname !== this.preferredDomain) return null; // invalid
|
|
|
|
return {
|
|
|
|
conferenceId: parsed.pathname,
|
|
|
|
domain: parsed.hostname,
|
|
|
|
isAudioOnly: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-07 01:04:41 +03:00
|
|
|
public static getInstance(): Jitsi {
|
|
|
|
if (!Jitsi.instance) {
|
|
|
|
Jitsi.instance = new Jitsi();
|
|
|
|
}
|
|
|
|
return Jitsi.instance;
|
|
|
|
}
|
|
|
|
}
|