From 51968a80726cea02d79d9f882501ab1023e4ed3e Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Tue, 28 Jul 2020 22:16:54 -0600 Subject: [PATCH] Convert RoomSettingsHandler to TS --- ...tingsHandler.js => RoomSettingsHandler.ts} | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) rename src/settings/handlers/{RoomSettingsHandler.js => RoomSettingsHandler.ts} (69%) diff --git a/src/settings/handlers/RoomSettingsHandler.js b/src/settings/handlers/RoomSettingsHandler.ts similarity index 69% rename from src/settings/handlers/RoomSettingsHandler.js rename to src/settings/handlers/RoomSettingsHandler.ts index 321f329ea0..cc9496c82b 100644 --- a/src/settings/handlers/RoomSettingsHandler.js +++ b/src/settings/handlers/RoomSettingsHandler.ts @@ -1,6 +1,6 @@ /* Copyright 2017 Travis Ralston -Copyright 2019 New Vector Ltd. +Copyright 2019, 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. @@ -15,23 +15,24 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {MatrixClientPeg} from '../../MatrixClientPeg'; +import { MatrixClientPeg } from '../../MatrixClientPeg'; import MatrixClientBackedSettingsHandler from "./MatrixClientBackedSettingsHandler"; -import {objectClone, objectKeyChanges} from "../../utils/objects"; -import {SettingLevel} from "../SettingLevel"; +import { objectClone, objectKeyChanges } from "../../utils/objects"; +import { SettingLevel } from "../SettingLevel"; +import { WatchManager } from "../WatchManager"; +import { MatrixClient } from "matrix-js-sdk/src/client"; +import { MatrixEvent } from "matrix-js-sdk/src/models/event"; +import { RoomState } from "matrix-js-sdk/src/models/room-state"; /** * Gets and sets settings at the "room" level. */ export default class RoomSettingsHandler extends MatrixClientBackedSettingsHandler { - constructor(watchManager) { + constructor(private watchers: WatchManager) { super(); - - this._watchers = watchManager; - this._onEvent = this._onEvent.bind(this); } - initMatrixClient(oldClient, newClient) { + protected initMatrixClient(oldClient: MatrixClient, newClient: MatrixClient) { if (oldClient) { oldClient.removeListener("RoomState.events", this._onEvent); } @@ -39,7 +40,7 @@ export default class RoomSettingsHandler extends MatrixClientBackedSettingsHandl newClient.on("RoomState.events", this._onEvent); } - _onEvent(event, state, prevEvent) { + private _onEvent = (event: MatrixEvent, state: RoomState, prevEvent: MatrixEvent) => { const roomId = event.getRoomId(); const room = this.client.getRoom(roomId); @@ -60,45 +61,45 @@ export default class RoomSettingsHandler extends MatrixClientBackedSettingsHandl val = !val; } - this._watchers.notifyUpdate("urlPreviewsEnabled", roomId, SettingLevel.ROOM, val); + this.watchers.notifyUpdate("urlPreviewsEnabled", roomId, SettingLevel.ROOM, val); } else if (event.getType() === "im.vector.web.settings") { // Figure out what changed and fire those updates const prevContent = prevEvent ? prevEvent.getContent() : {}; const changedSettings = objectKeyChanges(prevContent, event.getContent()); for (const settingName of changedSettings) { - this._watchers.notifyUpdate(settingName, roomId, SettingLevel.ROOM, event.getContent()[settingName]); + this.watchers.notifyUpdate(settingName, roomId, SettingLevel.ROOM, event.getContent()[settingName]); } } - } + }; - getValue(settingName, roomId) { + public getValue(settingName: string, roomId: string): any { // Special case URL previews if (settingName === "urlPreviewsEnabled") { - const content = this._getSettings(roomId, "org.matrix.room.preview_urls") || {}; + const content = this.getSettings(roomId, "org.matrix.room.preview_urls") || {}; // Check to make sure that we actually got a boolean - if (typeof(content['disable']) !== "boolean") return null; + if (typeof (content['disable']) !== "boolean") return null; return !content['disable']; } - const settings = this._getSettings(roomId) || {}; + const settings = this.getSettings(roomId) || {}; return settings[settingName]; } - setValue(settingName, roomId, newValue) { + public setValue(settingName: string, roomId: string, newValue: any): Promise { // Special case URL previews if (settingName === "urlPreviewsEnabled") { - const content = this._getSettings(roomId, "org.matrix.room.preview_urls") || {}; + const content = this.getSettings(roomId, "org.matrix.room.preview_urls") || {}; content['disable'] = !newValue; return MatrixClientPeg.get().sendStateEvent(roomId, "org.matrix.room.preview_urls", content); } - const content = this._getSettings(roomId) || {}; + const content = this.getSettings(roomId) || {}; content[settingName] = newValue; return MatrixClientPeg.get().sendStateEvent(roomId, "im.vector.web.settings", content, ""); } - canSetValue(settingName, roomId) { + public canSetValue(settingName: string, roomId: string): boolean { const cli = MatrixClientPeg.get(); const room = cli.getRoom(roomId); @@ -109,12 +110,12 @@ export default class RoomSettingsHandler extends MatrixClientBackedSettingsHandl return room.currentState.maySendStateEvent(eventType, cli.getUserId()); } - isSupported() { + public isSupported(): boolean { const cli = MatrixClientPeg.get(); return cli !== undefined && cli !== null; } - _getSettings(roomId, eventType = "im.vector.web.settings") { + private getSettings(roomId: string, eventType = "im.vector.web.settings"): any { const room = MatrixClientPeg.get().getRoom(roomId); if (!room) return null;