2017-10-28 19:13:06 -06:00
|
|
|
/*
|
|
|
|
|
Copyright 2017 Travis Ralston
|
2020-07-28 21:59:47 -06:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2017-10-28 19:13:06 -06:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-22 17:23:32 -05:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
2023-08-07 09:24:58 +01:00
|
|
|
import { MatrixEvent, Room, RoomEvent } from "matrix-js-sdk/src/matrix";
|
2022-03-03 22:09:06 +00:00
|
|
|
import { defer } from "matrix-js-sdk/src/utils";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2019-02-22 16:33:20 -07:00
|
|
|
import MatrixClientBackedSettingsHandler from "./MatrixClientBackedSettingsHandler";
|
2020-07-28 21:59:47 -06:00
|
|
|
import { objectClone, objectKeyChanges } from "../../utils/objects";
|
|
|
|
|
import { SettingLevel } from "../SettingLevel";
|
|
|
|
|
import { WatchManager } from "../WatchManager";
|
2017-10-28 19:13:06 -06:00
|
|
|
|
2019-10-29 14:35:35 -06:00
|
|
|
const ALLOWED_WIDGETS_EVENT_TYPE = "im.vector.setting.allowed_widgets";
|
2022-03-03 22:09:06 +00:00
|
|
|
const DEFAULT_SETTINGS_EVENT_TYPE = "im.vector.web.settings";
|
2019-10-29 14:35:35 -06:00
|
|
|
|
2017-10-28 19:13:06 -06:00
|
|
|
/**
|
|
|
|
|
* Gets and sets settings at the "room-account" level for the current user.
|
|
|
|
|
*/
|
2019-02-22 16:33:20 -07:00
|
|
|
export default class RoomAccountSettingsHandler extends MatrixClientBackedSettingsHandler {
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor(public readonly watchers: WatchManager) {
|
2019-02-22 16:33:20 -07:00
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
protected initMatrixClient(oldClient: MatrixClient, newClient: MatrixClient): void {
|
2019-02-22 16:33:20 -07:00
|
|
|
if (oldClient) {
|
2022-02-22 12:18:08 +00:00
|
|
|
oldClient.removeListener(RoomEvent.AccountData, this.onAccountData);
|
2019-02-22 16:33:20 -07:00
|
|
|
}
|
|
|
|
|
|
2022-02-22 12:18:08 +00:00
|
|
|
newClient.on(RoomEvent.AccountData, this.onAccountData);
|
2019-02-22 16:33:20 -07:00
|
|
|
}
|
|
|
|
|
|
2023-05-12 09:49:37 +01:00
|
|
|
private onAccountData = (event: MatrixEvent, room: Room, prevEvent?: MatrixEvent): void => {
|
2019-02-22 16:33:20 -07:00
|
|
|
const roomId = room.roomId;
|
|
|
|
|
|
|
|
|
|
if (event.getType() === "org.matrix.room.preview_urls") {
|
|
|
|
|
let val = event.getContent()["disable"];
|
|
|
|
|
if (typeof val !== "boolean") {
|
|
|
|
|
val = null;
|
|
|
|
|
} else {
|
|
|
|
|
val = !val;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 21:59:47 -06:00
|
|
|
this.watchers.notifyUpdate("urlPreviewsEnabled", roomId, SettingLevel.ROOM_ACCOUNT, val);
|
2022-03-03 22:09:06 +00:00
|
|
|
} else if (event.getType() === DEFAULT_SETTINGS_EVENT_TYPE) {
|
2020-06-22 10:18:38 -06:00
|
|
|
// Figure out what changed and fire those updates
|
2023-05-12 09:49:37 +01:00
|
|
|
const prevContent = prevEvent?.getContent() ?? {};
|
2020-08-05 09:13:01 +01:00
|
|
|
const changedSettings = objectKeyChanges<Record<string, any>>(prevContent, event.getContent());
|
2020-06-22 10:18:38 -06:00
|
|
|
for (const settingName of changedSettings) {
|
2019-02-26 12:43:10 -07:00
|
|
|
const val = event.getContent()[settingName];
|
2020-07-28 21:59:47 -06:00
|
|
|
this.watchers.notifyUpdate(settingName, roomId, SettingLevel.ROOM_ACCOUNT, val);
|
2019-02-22 16:33:20 -07:00
|
|
|
}
|
2019-10-29 14:35:35 -06:00
|
|
|
} else if (event.getType() === ALLOWED_WIDGETS_EVENT_TYPE) {
|
2020-07-28 21:59:47 -06:00
|
|
|
this.watchers.notifyUpdate("allowedWidgets", roomId, SettingLevel.ROOM_ACCOUNT, event.getContent());
|
2019-02-22 16:33:20 -07:00
|
|
|
}
|
2020-07-28 21:59:47 -06:00
|
|
|
};
|
2019-02-22 16:33:20 -07:00
|
|
|
|
2020-07-28 21:59:47 -06:00
|
|
|
public getValue(settingName: string, roomId: string): any {
|
2017-10-29 21:48:29 -06:00
|
|
|
// Special case URL previews
|
|
|
|
|
if (settingName === "urlPreviewsEnabled") {
|
2020-07-28 21:59:47 -06:00
|
|
|
const content = this.getSettings(roomId, "org.matrix.room.preview_urls") || {};
|
2017-11-15 19:22:30 -07:00
|
|
|
|
|
|
|
|
// Check to make sure that we actually got a boolean
|
2020-07-28 21:59:47 -06:00
|
|
|
if (typeof content["disable"] !== "boolean") return null;
|
2017-10-29 21:48:29 -06:00
|
|
|
return !content["disable"];
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-29 14:35:35 -06:00
|
|
|
// Special case allowed widgets
|
|
|
|
|
if (settingName === "allowedWidgets") {
|
2020-07-28 21:59:47 -06:00
|
|
|
return this.getSettings(roomId, ALLOWED_WIDGETS_EVENT_TYPE);
|
2019-10-29 14:35:35 -06:00
|
|
|
}
|
|
|
|
|
|
2020-07-28 21:59:47 -06:00
|
|
|
const settings = this.getSettings(roomId) || {};
|
2018-01-17 18:06:19 +00:00
|
|
|
return settings[settingName];
|
2017-10-28 19:13:06 -06:00
|
|
|
}
|
|
|
|
|
|
2022-03-03 22:09:06 +00:00
|
|
|
// helper function to send room account data then await it being echoed back
|
|
|
|
|
private async setRoomAccountData(
|
|
|
|
|
roomId: string,
|
|
|
|
|
eventType: string,
|
|
|
|
|
field: string | null,
|
|
|
|
|
value: any,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
let content: ReturnType<RoomAccountSettingsHandler["getSettings"]>;
|
|
|
|
|
|
|
|
|
|
if (field === null) {
|
|
|
|
|
content = value;
|
|
|
|
|
} else {
|
2022-03-07 17:51:20 +00:00
|
|
|
content = this.getSettings(roomId, eventType) || {};
|
2022-03-03 22:09:06 +00:00
|
|
|
content[field] = value;
|
2017-10-29 21:48:29 -06:00
|
|
|
}
|
|
|
|
|
|
2022-03-03 22:09:06 +00:00
|
|
|
await this.client.setRoomAccountData(roomId, eventType, content);
|
2019-10-29 14:35:35 -06:00
|
|
|
|
2022-03-03 22:09:06 +00:00
|
|
|
const deferred = defer<void>();
|
2023-01-12 13:25:14 +00:00
|
|
|
const handler = (event: MatrixEvent, room: Room): void => {
|
2022-03-07 17:51:20 +00:00
|
|
|
if (room.roomId !== roomId || event.getType() !== eventType) return;
|
2022-03-03 22:09:06 +00:00
|
|
|
if (field !== null && event.getContent()[field] !== value) return;
|
|
|
|
|
this.client.off(RoomEvent.AccountData, handler);
|
|
|
|
|
deferred.resolve();
|
|
|
|
|
};
|
|
|
|
|
this.client.on(RoomEvent.AccountData, handler);
|
|
|
|
|
|
|
|
|
|
await deferred.promise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public setValue(settingName: string, roomId: string, newValue: any): Promise<void> {
|
|
|
|
|
switch (settingName) {
|
|
|
|
|
// Special case URL previews
|
|
|
|
|
case "urlPreviewsEnabled":
|
|
|
|
|
return this.setRoomAccountData(roomId, "org.matrix.room.preview_urls", "disable", !newValue);
|
|
|
|
|
|
|
|
|
|
// Special case allowed widgets
|
|
|
|
|
case "allowedWidgets":
|
|
|
|
|
return this.setRoomAccountData(roomId, ALLOWED_WIDGETS_EVENT_TYPE, null, newValue);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return this.setRoomAccountData(roomId, DEFAULT_SETTINGS_EVENT_TYPE, settingName, newValue);
|
|
|
|
|
}
|
2017-10-28 19:13:06 -06:00
|
|
|
}
|
|
|
|
|
|
2020-07-28 21:59:47 -06:00
|
|
|
public canSetValue(settingName: string, roomId: string): boolean {
|
2017-11-03 23:19:45 -06:00
|
|
|
// If they have the room, they can set their own account data
|
2022-03-03 22:09:06 +00:00
|
|
|
return !!this.client.getRoom(roomId);
|
2017-10-28 19:13:06 -06:00
|
|
|
}
|
|
|
|
|
|
2020-07-28 21:59:47 -06:00
|
|
|
public isSupported(): boolean {
|
2022-03-03 22:09:06 +00:00
|
|
|
return this.client && !this.client.isGuest();
|
2017-10-28 19:13:06 -06:00
|
|
|
}
|
|
|
|
|
|
2022-03-03 22:09:06 +00:00
|
|
|
private getSettings(roomId: string, eventType = DEFAULT_SETTINGS_EVENT_TYPE): any {
|
|
|
|
|
// TODO: [TS] Type return
|
|
|
|
|
const event = this.client.getRoom(roomId)?.getAccountData(eventType);
|
2018-01-17 18:06:19 +00:00
|
|
|
if (!event || !event.getContent()) return null;
|
2020-06-22 14:14:43 -06:00
|
|
|
return objectClone(event.getContent()); // clone to prevent mutation
|
2017-10-28 19:13:06 -06:00
|
|
|
}
|
2017-10-28 19:53:12 -06:00
|
|
|
}
|