mirror of https://github.com/vector-im/riot-web
Convert RoomDeviceSettingsHandler to TS
parent
c4f4e2aa0a
commit
c7b28b4566
|
@ -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.
|
||||
|
@ -16,71 +16,70 @@ limitations under the License.
|
|||
*/
|
||||
|
||||
import SettingsHandler from "./SettingsHandler";
|
||||
import {SettingLevel} from "../SettingLevel";
|
||||
import { SettingLevel } from "../SettingLevel";
|
||||
import { WatchManager } from "../WatchManager";
|
||||
|
||||
/**
|
||||
* Gets and sets settings at the "room-device" level for the current device in a particular
|
||||
* room.
|
||||
*/
|
||||
export default class RoomDeviceSettingsHandler extends SettingsHandler {
|
||||
constructor(watchManager) {
|
||||
constructor(private watchers: WatchManager) {
|
||||
super();
|
||||
|
||||
this._watchers = watchManager;
|
||||
}
|
||||
|
||||
getValue(settingName, roomId) {
|
||||
public getValue(settingName: string, roomId: string): any {
|
||||
// Special case blacklist setting to use legacy values
|
||||
if (settingName === "blacklistUnverifiedDevices") {
|
||||
const value = this._read("mx_local_settings");
|
||||
const value = this.read("mx_local_settings");
|
||||
if (value && value['blacklistUnverifiedDevicesPerRoom']) {
|
||||
return value['blacklistUnverifiedDevicesPerRoom'][roomId];
|
||||
}
|
||||
}
|
||||
|
||||
const value = this._read(this._getKey(settingName, roomId));
|
||||
const value = this.read(this.getKey(settingName, roomId));
|
||||
if (value) return value.value;
|
||||
return null;
|
||||
}
|
||||
|
||||
setValue(settingName, roomId, newValue) {
|
||||
public setValue(settingName: string, roomId: string, newValue: any): Promise<void> {
|
||||
// Special case blacklist setting for legacy structure
|
||||
if (settingName === "blacklistUnverifiedDevices") {
|
||||
let value = this._read("mx_local_settings");
|
||||
let value = this.read("mx_local_settings");
|
||||
if (!value) value = {};
|
||||
if (!value["blacklistUnverifiedDevicesPerRoom"]) value["blacklistUnverifiedDevicesPerRoom"] = {};
|
||||
value["blacklistUnverifiedDevicesPerRoom"][roomId] = newValue;
|
||||
localStorage.setItem("mx_local_settings", JSON.stringify(value));
|
||||
this._watchers.notifyUpdate(settingName, roomId, SettingLevel.ROOM_DEVICE, newValue);
|
||||
this.watchers.notifyUpdate(settingName, roomId, SettingLevel.ROOM_DEVICE, newValue);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
if (newValue === null) {
|
||||
localStorage.removeItem(this._getKey(settingName, roomId));
|
||||
localStorage.removeItem(this.getKey(settingName, roomId));
|
||||
} else {
|
||||
newValue = JSON.stringify({value: newValue});
|
||||
localStorage.setItem(this._getKey(settingName, roomId), newValue);
|
||||
localStorage.setItem(this.getKey(settingName, roomId), newValue);
|
||||
}
|
||||
|
||||
this._watchers.notifyUpdate(settingName, roomId, SettingLevel.ROOM_DEVICE, newValue);
|
||||
this.watchers.notifyUpdate(settingName, roomId, SettingLevel.ROOM_DEVICE, newValue);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
canSetValue(settingName, roomId) {
|
||||
public canSetValue(settingName: string, roomId: string): boolean {
|
||||
return true; // It's their device, so they should be able to
|
||||
}
|
||||
|
||||
isSupported() {
|
||||
public isSupported(): boolean {
|
||||
return localStorage !== undefined && localStorage !== null;
|
||||
}
|
||||
|
||||
_read(key) {
|
||||
private read(key: string): any {
|
||||
const rawValue = localStorage.getItem(key);
|
||||
if (!rawValue) return null;
|
||||
return JSON.parse(rawValue);
|
||||
}
|
||||
|
||||
_getKey(settingName, roomId) {
|
||||
private getKey(settingName: string, roomId: string): string {
|
||||
return "mx_setting_" + settingName + "_" + roomId;
|
||||
}
|
||||
}
|
|
@ -44,7 +44,7 @@ export default abstract class SettingsHandler {
|
|||
* @param {*} newValue The new value for the setting, may be null.
|
||||
* @returns {Promise} Resolves when the setting has been saved.
|
||||
*/
|
||||
public setValue(settingName: string, roomId: string, newValue): Promise<void> {
|
||||
public setValue(settingName: string, roomId: string, newValue: any): Promise<void> {
|
||||
console.error("Invalid operation: setValue was not overridden");
|
||||
return Promise.reject(new Error("Invalid operation: setValue was not overridden"));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue