diff --git a/src/components/views/context_menus/WidgetContextMenu.tsx b/src/components/views/context_menus/WidgetContextMenu.tsx index 8026942038..4662c74d78 100644 --- a/src/components/views/context_menus/WidgetContextMenu.tsx +++ b/src/components/views/context_menus/WidgetContextMenu.tsx @@ -26,7 +26,6 @@ import {WidgetMessagingStore} from "../../../stores/widgets/WidgetMessagingStore import RoomContext from "../../../contexts/RoomContext"; import dis from "../../../dispatcher/dispatcher"; import SettingsStore from "../../../settings/SettingsStore"; -import {SettingLevel} from "../../../settings/SettingLevel"; import Modal from "../../../Modal"; import QuestionDialog from "../dialogs/QuestionDialog"; import {WidgetType} from "../../../widgets/WidgetType"; @@ -127,7 +126,8 @@ const WidgetContextMenu: React.FC = ({ console.info("Revoking permission for widget to load: " + app.eventId); const current = SettingsStore.getValue("allowedWidgets", roomId); current[app.eventId] = false; - SettingsStore.setValue("allowedWidgets", roomId, SettingLevel.ROOM_ACCOUNT, current).catch(err => { + const level = SettingsStore.firstSupportedLevel("allowedWidgets"); + SettingsStore.setValue("allowedWidgets", roomId, level, current).catch(err => { console.error(err); // We don't really need to do anything about this - the user will just hit the button again. }); diff --git a/src/components/views/elements/AppTile.js b/src/components/views/elements/AppTile.js index 7e0ae965bb..213351889f 100644 --- a/src/components/views/elements/AppTile.js +++ b/src/components/views/elements/AppTile.js @@ -33,7 +33,6 @@ import SettingsStore from "../../../settings/SettingsStore"; import {aboveLeftOf, ContextMenuButton} from "../../structures/ContextMenu"; import PersistedElement, {getPersistKey} from "./PersistedElement"; import {WidgetType} from "../../../widgets/WidgetType"; -import {SettingLevel} from "../../../settings/SettingLevel"; import {StopGapWidget} from "../../../stores/widgets/StopGapWidget"; import {ElementWidgetActions} from "../../../stores/widgets/ElementWidgetActions"; import {MatrixCapabilities} from "matrix-widget-api"; @@ -240,7 +239,8 @@ export default class AppTile extends React.Component { console.info("Granting permission for widget to load: " + this.props.app.eventId); const current = SettingsStore.getValue("allowedWidgets", roomId); current[this.props.app.eventId] = true; - SettingsStore.setValue("allowedWidgets", roomId, SettingLevel.ROOM_ACCOUNT, current).then(() => { + const level = SettingsStore.firstSupportedLevel("allowedWidgets"); + SettingsStore.setValue("allowedWidgets", roomId, level, current).then(() => { this.setState({hasPermissionToLoad: true}); // Fetch a token for the integration manager, now that we're allowed to diff --git a/src/settings/Settings.ts b/src/settings/Settings.ts index b239b809fe..6ca009df61 100644 --- a/src/settings/Settings.ts +++ b/src/settings/Settings.ts @@ -421,7 +421,8 @@ export const SETTINGS: {[setting: string]: ISetting} = { default: true, }, "allowedWidgets": { - supportedLevels: [SettingLevel.ROOM_ACCOUNT], + supportedLevels: [SettingLevel.ROOM_ACCOUNT, SettingLevel.ROOM_DEVICE], + supportedLevelsAreOrdered: true, default: {}, // none allowed }, "analyticsOptIn": { diff --git a/src/settings/SettingsStore.ts b/src/settings/SettingsStore.ts index 82f169f498..1b718a72b3 100644 --- a/src/settings/SettingsStore.ts +++ b/src/settings/SettingsStore.ts @@ -467,6 +467,32 @@ export default class SettingsStore { return LEVEL_HANDLERS[level].isSupported(); } + /** + * Determines the first supported level out of all the levels that can be used for a + * specific setting. + * @param {string} settingName The setting name. + * @return {SettingLevel} + */ + public static firstSupportedLevel(settingName: string): SettingLevel { + // Verify that the setting is actually a setting + const setting = SETTINGS[settingName]; + if (!setting) { + throw new Error("Setting '" + settingName + "' does not appear to be a setting."); + } + + const levelOrder = (setting.supportedLevelsAreOrdered ? setting.supportedLevels : LEVEL_ORDER); + if (!levelOrder.includes(SettingLevel.DEFAULT)) levelOrder.push(SettingLevel.DEFAULT); // always include default + + const handlers = SettingsStore.getHandlers(settingName); + + for (const level of levelOrder) { + const handler = handlers[level]; + if (!handler) continue; + return level; + } + return null; + } + /** * Debugging function for reading explicit setting values without going through the * complicated/biased functions in the SettingsStore. This will print information to diff --git a/src/settings/handlers/AccountSettingsHandler.ts b/src/settings/handlers/AccountSettingsHandler.ts index d609fd3231..e75faa49c1 100644 --- a/src/settings/handlers/AccountSettingsHandler.ts +++ b/src/settings/handlers/AccountSettingsHandler.ts @@ -169,7 +169,7 @@ export default class AccountSettingsHandler extends MatrixClientBackedSettingsHa public isSupported(): boolean { const cli = MatrixClientPeg.get(); - return cli !== undefined && cli !== null; + return cli !== undefined && cli !== null && !cli.isGuest(); } private getSettings(eventType = "im.vector.web.settings"): any { // TODO: [TS] Types on return diff --git a/src/settings/handlers/RoomAccountSettingsHandler.ts b/src/settings/handlers/RoomAccountSettingsHandler.ts index 53e29653f8..416bdddb90 100644 --- a/src/settings/handlers/RoomAccountSettingsHandler.ts +++ b/src/settings/handlers/RoomAccountSettingsHandler.ts @@ -129,7 +129,7 @@ export default class RoomAccountSettingsHandler extends MatrixClientBackedSettin public isSupported(): boolean { const cli = MatrixClientPeg.get(); - return cli !== undefined && cli !== null; + return cli !== undefined && cli !== null && !cli.isGuest(); } private getSettings(roomId: string, eventType = "im.vector.web.settings"): any { // TODO: [TS] Type return diff --git a/test/components/views/messages/TextualBody-test.js b/test/components/views/messages/TextualBody-test.js index bf55e9c430..fc627538f0 100644 --- a/test/components/views/messages/TextualBody-test.js +++ b/test/components/views/messages/TextualBody-test.js @@ -222,6 +222,7 @@ describe("", () => { getRoom: () => mkStubRoom("room_id"), getAccountData: () => undefined, getUrlPreview: (url) => new Promise(() => {}), + isGuest: () => false, }; const ev = mkEvent({