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

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
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,12 +78,8 @@ export class IntegrationManagers {
} }
} }
async _setupHomeserverManagers() { async _setupHomeserverManagers(discoveryResponse) {
if (!MatrixClientPeg.get()) return;
try {
console.log("Updating homeserver-configured integration managers..."); console.log("Updating homeserver-configured integration managers...");
const homeserverDomain = MatrixClientPeg.getHomeserverName();
const discoveryResponse = await AutoDiscovery.getRawClientConfig(homeserverDomain);
if (discoveryResponse && discoveryResponse['m.integrations']) { if (discoveryResponse && discoveryResponse['m.integrations']) {
let managers = discoveryResponse['m.integrations']['managers']; let managers = discoveryResponse['m.integrations']['managers'];
if (!Array.isArray(managers)) managers = []; // make it an array so we can wipe the HS managers if (!Array.isArray(managers)) managers = []; // make it an array so we can wipe the HS managers
@ -112,10 +104,6 @@ export class IntegrationManagers {
} else { } else {
console.log("Homeserver has no integration managers"); console.log("Homeserver has no integration managers");
} }
} catch (e) {
console.error(e);
// Errors during discovery are non-fatal
}
} }
_setupAccountManagers() { _setupAccountManagers() {

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,34 +34,22 @@ 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);
}
console.log("Attempting to get Jitsi conference information from homeserver");
if (discoveryResponse && discoveryResponse[JITSI_WK_PROPERTY]) { if (discoveryResponse && discoveryResponse[JITSI_WK_PROPERTY]) {
const wkPreferredDomain = discoveryResponse[JITSI_WK_PROPERTY]['preferredDomain']; const wkPreferredDomain = discoveryResponse[JITSI_WK_PROPERTY]['preferredDomain'];
if (wkPreferredDomain) domain = wkPreferredDomain; if (wkPreferredDomain) domain = wkPreferredDomain;
} }
} catch (e) {
// These are non-fatal errors
console.error(e);
}
}
// Put the result into memory for us to use later // Put the result into memory for us to use later
this.domain = domain; this.domain = domain;