Convert RoomSettingsHandler to TS
This commit is contained in:
parent
c7b28b4566
commit
51968a8072
1 changed files with 24 additions and 23 deletions
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Copyright 2017 Travis Ralston
|
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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
@ -19,19 +19,20 @@ import {MatrixClientPeg} from '../../MatrixClientPeg';
|
||||||
import MatrixClientBackedSettingsHandler from "./MatrixClientBackedSettingsHandler";
|
import MatrixClientBackedSettingsHandler from "./MatrixClientBackedSettingsHandler";
|
||||||
import { objectClone, objectKeyChanges } from "../../utils/objects";
|
import { objectClone, objectKeyChanges } from "../../utils/objects";
|
||||||
import { SettingLevel } from "../SettingLevel";
|
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.
|
* Gets and sets settings at the "room" level.
|
||||||
*/
|
*/
|
||||||
export default class RoomSettingsHandler extends MatrixClientBackedSettingsHandler {
|
export default class RoomSettingsHandler extends MatrixClientBackedSettingsHandler {
|
||||||
constructor(watchManager) {
|
constructor(private watchers: WatchManager) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this._watchers = watchManager;
|
|
||||||
this._onEvent = this._onEvent.bind(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
initMatrixClient(oldClient, newClient) {
|
protected initMatrixClient(oldClient: MatrixClient, newClient: MatrixClient) {
|
||||||
if (oldClient) {
|
if (oldClient) {
|
||||||
oldClient.removeListener("RoomState.events", this._onEvent);
|
oldClient.removeListener("RoomState.events", this._onEvent);
|
||||||
}
|
}
|
||||||
|
@ -39,7 +40,7 @@ export default class RoomSettingsHandler extends MatrixClientBackedSettingsHandl
|
||||||
newClient.on("RoomState.events", this._onEvent);
|
newClient.on("RoomState.events", this._onEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onEvent(event, state, prevEvent) {
|
private _onEvent = (event: MatrixEvent, state: RoomState, prevEvent: MatrixEvent) => {
|
||||||
const roomId = event.getRoomId();
|
const roomId = event.getRoomId();
|
||||||
const room = this.client.getRoom(roomId);
|
const room = this.client.getRoom(roomId);
|
||||||
|
|
||||||
|
@ -60,45 +61,45 @@ export default class RoomSettingsHandler extends MatrixClientBackedSettingsHandl
|
||||||
val = !val;
|
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") {
|
} else if (event.getType() === "im.vector.web.settings") {
|
||||||
// Figure out what changed and fire those updates
|
// Figure out what changed and fire those updates
|
||||||
const prevContent = prevEvent ? prevEvent.getContent() : {};
|
const prevContent = prevEvent ? prevEvent.getContent() : {};
|
||||||
const changedSettings = objectKeyChanges(prevContent, event.getContent());
|
const changedSettings = objectKeyChanges(prevContent, event.getContent());
|
||||||
for (const settingName of changedSettings) {
|
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
|
// Special case URL previews
|
||||||
if (settingName === "urlPreviewsEnabled") {
|
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
|
// 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'];
|
return !content['disable'];
|
||||||
}
|
}
|
||||||
|
|
||||||
const settings = this._getSettings(roomId) || {};
|
const settings = this.getSettings(roomId) || {};
|
||||||
return settings[settingName];
|
return settings[settingName];
|
||||||
}
|
}
|
||||||
|
|
||||||
setValue(settingName, roomId, newValue) {
|
public setValue(settingName: string, roomId: string, newValue: any): Promise<void> {
|
||||||
// Special case URL previews
|
// Special case URL previews
|
||||||
if (settingName === "urlPreviewsEnabled") {
|
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;
|
content['disable'] = !newValue;
|
||||||
return MatrixClientPeg.get().sendStateEvent(roomId, "org.matrix.room.preview_urls", content);
|
return MatrixClientPeg.get().sendStateEvent(roomId, "org.matrix.room.preview_urls", content);
|
||||||
}
|
}
|
||||||
|
|
||||||
const content = this._getSettings(roomId) || {};
|
const content = this.getSettings(roomId) || {};
|
||||||
content[settingName] = newValue;
|
content[settingName] = newValue;
|
||||||
return MatrixClientPeg.get().sendStateEvent(roomId, "im.vector.web.settings", content, "");
|
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 cli = MatrixClientPeg.get();
|
||||||
const room = cli.getRoom(roomId);
|
const room = cli.getRoom(roomId);
|
||||||
|
|
||||||
|
@ -109,12 +110,12 @@ export default class RoomSettingsHandler extends MatrixClientBackedSettingsHandl
|
||||||
return room.currentState.maySendStateEvent(eventType, cli.getUserId());
|
return room.currentState.maySendStateEvent(eventType, cli.getUserId());
|
||||||
}
|
}
|
||||||
|
|
||||||
isSupported() {
|
public isSupported(): boolean {
|
||||||
const cli = MatrixClientPeg.get();
|
const cli = MatrixClientPeg.get();
|
||||||
return cli !== undefined && cli !== null;
|
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);
|
const room = MatrixClientPeg.get().getRoom(roomId);
|
||||||
if (!room) return null;
|
if (!room) return null;
|
||||||
|
|
Loading…
Reference in a new issue