From 31c53f80261c8135154606ac60835249c6706c11 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Mon, 24 Mar 2025 22:03:09 +0000 Subject: [PATCH] Add initial support for a account data level key. --- src/settings/Settings.tsx | 11 ++- .../MediaPreviewConfigController.ts | 68 +++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/settings/controllers/MediaPreviewConfigController.ts diff --git a/src/settings/Settings.tsx b/src/settings/Settings.tsx index 6984932fd9..181c7f5cde 100644 --- a/src/settings/Settings.tsx +++ b/src/settings/Settings.tsx @@ -8,7 +8,7 @@ Please see LICENSE files in the repository root for full details. */ import React, { type ReactNode } from "react"; -import { UNSTABLE_MSC4133_EXTENDED_PROFILES } from "matrix-js-sdk/src/matrix"; +import { MediaPreviewConfig, UNSTABLE_MSC4133_EXTENDED_PROFILES } from "matrix-js-sdk/src/matrix"; import { _t, _td, type TranslationKey } from "../languageHandler"; import DeviceIsolationModeController from "./controllers/DeviceIsolationModeController.ts"; @@ -45,6 +45,7 @@ import { type Json, type JsonValue } from "../@types/json.ts"; import { type RecentEmojiData } from "../emojipicker/recent.ts"; import { type Assignable } from "../@types/common.ts"; import { SortingAlgorithm } from "../stores/room-list-v3/skip-list/sorters/index.ts"; +import MediaPreviewConfigController from "./controllers/MediaPreviewConfigController.ts"; export const defaultWatchManager = new WatchManager(); @@ -349,6 +350,7 @@ export interface Settings { "Electron.alwaysShowMenuBar": IBaseSetting; "Electron.showTrayIcon": IBaseSetting; "Electron.enableHardwareAcceleration": IBaseSetting; + "mediaPreviewConfig": IBaseSetting; } export type SettingKey = keyof Settings; @@ -425,6 +427,11 @@ export const SETTINGS: Settings = { supportedLevelsAreOrdered: true, default: false, }, + "mediaPreviewConfig": { + controller: new MediaPreviewConfigController(), + supportedLevels: LEVELS_ROOM_OR_ACCOUNT, + default: MediaPreviewConfig.Private, + }, "feature_report_to_moderators": { isFeature: true, labsGroup: LabGroup.Moderation, @@ -1383,5 +1390,5 @@ export const SETTINGS: Settings = { supportedLevels: [SettingLevel.PLATFORM], displayName: _td("settings|preferences|enable_hardware_acceleration"), default: true, - }, + } }; diff --git a/src/settings/controllers/MediaPreviewConfigController.ts b/src/settings/controllers/MediaPreviewConfigController.ts new file mode 100644 index 0000000000..577191fb24 --- /dev/null +++ b/src/settings/controllers/MediaPreviewConfigController.ts @@ -0,0 +1,68 @@ +/* +Copyright 2025 New Vector Ltd. + +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial +Please see LICENSE files in the repository root for full details. +*/ + +import { ClientEvent, MatrixEvent, MediaPreviewConfig, type MatrixClient } from "matrix-js-sdk/src/matrix"; + +import { SettingLevel } from "../SettingLevel.ts"; +import MatrixClientBackedController from "./MatrixClientBackedController.ts"; + + +const CLIENT_KEY = "m.media_preview_config"; + +/** + * TODO + */ +export default class MediaPreviewConfigController extends MatrixClientBackedController { + private globalSetting: MediaPreviewConfig = MediaPreviewConfig.Private; + + public constructor() { + super(); + } + + private getRoomValue = (roomId: string): MediaPreviewConfig|null => { + return this.client?.getRoom(roomId)?.getAccountData(CLIENT_KEY)?.getContent().value ?? null; + } + + private onAccountData = (event: MatrixEvent): void => { + // TODO: Validate. + const roomId = event.getRoomId(); + if (!roomId) { + this.globalSetting = event.getContent().value; + } + }; + + protected async initMatrixClient(newClient: MatrixClient, oldClient?: MatrixClient): Promise { + oldClient?.off(ClientEvent.AccountData, this.onAccountData); + newClient.on(ClientEvent.AccountData, this.onAccountData); + const accountData = newClient.getAccountData(CLIENT_KEY); + if (accountData) this.onAccountData(accountData); + } + + public getValueOverride(level: SettingLevel, roomId: string | null,): MediaPreviewConfig { + // TODO: Use SettingLevel? + if (roomId) { + return this.getRoomValue(roomId) ?? this.globalSetting; + } + return this.globalSetting; + } + + public get settingDisabled(): boolean | string { + return false; + } + + public onChange(_level: SettingLevel, roomId: string | null, newValue: MediaPreviewConfig): void { + if (roomId) { + this.client?.setRoomAccountData(roomId, "m.media_preview_config", { + value: newValue + }); + return; + } + this.client?.setAccountDataRaw( "m.media_preview_config", { + value: newValue + }); + } +}