Add js-sdk mechanism for polling client well-known for config

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
pull/21833/head
Michael Telatynski 2020-06-01 22:22:01 +01:00
parent 8848a2ea64
commit cbc2aee746
3 changed files with 35 additions and 59 deletions

View File

@ -49,6 +49,7 @@ export interface IOpts {
initialSyncLimit?: number; initialSyncLimit?: number;
pendingEventOrdering?: "detached" | "chronological"; pendingEventOrdering?: "detached" | "chronological";
lazyLoadMembers?: boolean; lazyLoadMembers?: boolean;
clientWellKnownPollPeriod?: number;
} }
export interface IMatrixClientPeg { export interface IMatrixClientPeg {
@ -209,6 +210,7 @@ class _MatrixClientPeg implements IMatrixClientPeg {
// the react sdk doesn't work without this, so don't allow // the react sdk doesn't work without this, so don't allow
opts.pendingEventOrdering = "detached"; opts.pendingEventOrdering = "detached";
opts.lazyLoadMembers = true; opts.lazyLoadMembers = true;
opts.clientWellKnownPollPeriod = 4 * 60 * 60; // 4 hours
// Connect the matrix client to the dispatcher and setting handlers // Connect the matrix client to the dispatcher and setting handlers
MatrixActionCreators.start(this.matrixClient); MatrixActionCreators.start(this.matrixClient);

View File

@ -21,10 +21,8 @@ import {IntegrationManagerInstance, KIND_ACCOUNT, KIND_CONFIG, KIND_HOMESERVER}
import type {MatrixClient, MatrixEvent, Room} from "matrix-js-sdk"; import type {MatrixClient, MatrixEvent, Room} from "matrix-js-sdk";
import WidgetUtils from "../utils/WidgetUtils"; import WidgetUtils from "../utils/WidgetUtils";
import {MatrixClientPeg} from "../MatrixClientPeg"; import {MatrixClientPeg} from "../MatrixClientPeg";
import {AutoDiscovery} from "matrix-js-sdk";
import SettingsStore from "../settings/SettingsStore"; import SettingsStore from "../settings/SettingsStore";
const HS_MANAGERS_REFRESH_INTERVAL = 8 * 60 * 60 * 1000; // 8 hours
const KIND_PREFERENCE = [ const KIND_PREFERENCE = [
// Ordered: first is most preferred, last is least preferred. // Ordered: first is most preferred, last is least preferred.
KIND_ACCOUNT, KIND_ACCOUNT,
@ -44,7 +42,6 @@ export class IntegrationManagers {
_managers: IntegrationManagerInstance[] = []; _managers: IntegrationManagerInstance[] = [];
_client: MatrixClient; _client: MatrixClient;
_wellknownRefreshTimerId: number = null;
_primaryManager: IntegrationManagerInstance; _primaryManager: IntegrationManagerInstance;
constructor() { constructor() {
@ -55,20 +52,19 @@ export class IntegrationManagers {
this.stopWatching(); this.stopWatching();
this._client = MatrixClientPeg.get(); this._client = MatrixClientPeg.get();
this._client.on("accountData", this._onAccountData); this._client.on("accountData", this._onAccountData);
this._client.on("WellKnown.client", this._setupHomeserverManagers);
this._compileManagers(); this._compileManagers();
setInterval(() => this._setupHomeserverManagers(), HS_MANAGERS_REFRESH_INTERVAL);
} }
stopWatching(): void { stopWatching(): void {
if (!this._client) return; if (!this._client) return;
this._client.removeListener("accountData", this._onAccountData); this._client.removeListener("accountData", this._onAccountData);
if (this._wellknownRefreshTimerId !== null) clearInterval(this._wellknownRefreshTimerId); this._client.removeListener("WellKnown.client", this._setupHomeserverManagers);
} }
_compileManagers() { _compileManagers() {
this._managers = []; this._managers = [];
this._setupConfiguredManager(); this._setupConfiguredManager();
this._setupHomeserverManagers();
this._setupAccountManagers(); this._setupAccountManagers();
} }
@ -82,39 +78,31 @@ export class IntegrationManagers {
} }
} }
async _setupHomeserverManagers() { async _setupHomeserverManagers(discoveryResponse) {
if (!MatrixClientPeg.get()) return; console.log("Updating homeserver-configured integration managers...");
try { if (discoveryResponse && discoveryResponse['m.integrations']) {
console.log("Updating homeserver-configured integration managers..."); let managers = discoveryResponse['m.integrations']['managers'];
const homeserverDomain = MatrixClientPeg.getHomeserverName(); if (!Array.isArray(managers)) managers = []; // make it an array so we can wipe the HS managers
const discoveryResponse = await AutoDiscovery.getRawClientConfig(homeserverDomain);
if (discoveryResponse && discoveryResponse['m.integrations']) {
let managers = discoveryResponse['m.integrations']['managers'];
if (!Array.isArray(managers)) managers = []; // make it an array so we can wipe the HS managers
console.log(`Homeserver has ${managers.length} integration managers`); console.log(`Homeserver has ${managers.length} integration managers`);
// Clear out any known managers for the homeserver // Clear out any known managers for the homeserver
// TODO: Log out of the scalar clients // TODO: Log out of the scalar clients
this._managers = this._managers.filter(m => m.kind !== KIND_HOMESERVER); this._managers = this._managers.filter(m => m.kind !== KIND_HOMESERVER);
// Now add all the managers the homeserver wants us to have // Now add all the managers the homeserver wants us to have
for (const hsManager of managers) { for (const hsManager of managers) {
if (!hsManager["api_url"]) continue; if (!hsManager["api_url"]) continue;
this._managers.push(new IntegrationManagerInstance( this._managers.push(new IntegrationManagerInstance(
KIND_HOMESERVER, KIND_HOMESERVER,
hsManager["api_url"], hsManager["api_url"],
hsManager["ui_url"], // optional hsManager["ui_url"], // optional
)); ));
}
this._primaryManager = null; // reset primary
} else {
console.log("Homeserver has no integration managers");
} }
} catch (e) {
console.error(e); this._primaryManager = null; // reset primary
// Errors during discovery are non-fatal } else {
console.log("Homeserver has no integration managers");
} }
} }

View File

@ -16,10 +16,8 @@ limitations under the License.
import SdkConfig from "../SdkConfig"; import SdkConfig from "../SdkConfig";
import {MatrixClientPeg} from "../MatrixClientPeg"; import {MatrixClientPeg} from "../MatrixClientPeg";
import {AutoDiscovery} from "matrix-js-sdk/src/autodiscovery";
const JITSI_WK_PROPERTY = "im.vector.riot.jitsi"; const JITSI_WK_PROPERTY = "im.vector.riot.jitsi";
const JITSI_WK_CHECK_INTERVAL = 2 * 60 * 60 * 1000; // 2 hours, arbitrarily selected
export interface JitsiWidgetData { export interface JitsiWidgetData {
conferenceId: string; conferenceId: string;
@ -36,33 +34,21 @@ export class Jitsi {
return this.domain || 'jitsi.riot.im'; return this.domain || 'jitsi.riot.im';
} }
constructor() {
// We rely on the first call to be an .update() instead of doing one here. Doing one
// here could result in duplicate calls to the homeserver.
// Start a timer to update the server info regularly
setInterval(() => this.update(), JITSI_WK_CHECK_INTERVAL);
}
public async update(): Promise<any> { public async update(): Promise<any> {
// Start with a default of the config's domain // Start with a default of the config's domain
let domain = (SdkConfig.get()['jitsi'] || {})['preferredDomain'] || 'jitsi.riot.im'; let domain = (SdkConfig.get()['jitsi'] || {})['preferredDomain'] || 'jitsi.riot.im';
// Now request the .well-known config to see if it changed const cli = MatrixClientPeg.get();
if (MatrixClientPeg.get()) { const discoveryResponse = cli && MatrixClientPeg.get().getClientWellKnown();
try {
console.log("Attempting to get Jitsi conference information from homeserver");
const homeserverDomain = MatrixClientPeg.getHomeserverName(); if (cli) {
const discoveryResponse = await AutoDiscovery.getRawClientConfig(homeserverDomain); cli.on("WellKnown.client", this.update);
if (discoveryResponse && discoveryResponse[JITSI_WK_PROPERTY]) { }
const wkPreferredDomain = discoveryResponse[JITSI_WK_PROPERTY]['preferredDomain'];
if (wkPreferredDomain) domain = wkPreferredDomain; console.log("Attempting to get Jitsi conference information from homeserver");
} if (discoveryResponse && discoveryResponse[JITSI_WK_PROPERTY]) {
} catch (e) { const wkPreferredDomain = discoveryResponse[JITSI_WK_PROPERTY]['preferredDomain'];
// These are non-fatal errors if (wkPreferredDomain) domain = wkPreferredDomain;
console.error(e);
}
} }
// Put the result into memory for us to use later // Put the result into memory for us to use later