From cb66f7493b24e860f0d7e850afe007eecc9046b7 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Thu, 14 Jan 2021 15:58:26 +0000 Subject: [PATCH 1/5] Skip account settings for guest users Homeservers don't allow this currently, so we should use something else instead for such cases. --- src/settings/handlers/AccountSettingsHandler.ts | 2 +- src/settings/handlers/RoomAccountSettingsHandler.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 From 658a8dfa9954f4c1e45d46c1ff924f14684b7923 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Thu, 14 Jan 2021 17:30:25 +0000 Subject: [PATCH 2/5] Use device storage for allowed widgets if account data not supported With guest accounts, account data is not available, so we use device storage to hold allowed widgets as a good enough place. Fixes https://github.com/vector-im/element-web/issues/16145 --- .../views/context_menus/WidgetContextMenu.tsx | 3 ++- src/components/views/elements/AppTile.js | 3 ++- src/settings/Settings.ts | 3 ++- src/settings/SettingsStore.ts | 25 +++++++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/components/views/context_menus/WidgetContextMenu.tsx b/src/components/views/context_menus/WidgetContextMenu.tsx index 8026942038..3c7bf0ee0e 100644 --- a/src/components/views/context_menus/WidgetContextMenu.tsx +++ b/src/components/views/context_menus/WidgetContextMenu.tsx @@ -127,7 +127,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..65bc0ac6f1 100644 --- a/src/components/views/elements/AppTile.js +++ b/src/components/views/elements/AppTile.js @@ -240,7 +240,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..2d024ba3f8 100644 --- a/src/settings/SettingsStore.ts +++ b/src/settings/SettingsStore.ts @@ -467,6 +467,31 @@ 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; + } + } + /** * Debugging function for reading explicit setting values without going through the * complicated/biased functions in the SettingsStore. This will print information to From accbe78d9a3a14599d09d7814b4db70072ffbdce Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Thu, 14 Jan 2021 17:37:18 +0000 Subject: [PATCH 3/5] Add null path --- src/settings/SettingsStore.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/settings/SettingsStore.ts b/src/settings/SettingsStore.ts index 2d024ba3f8..1b718a72b3 100644 --- a/src/settings/SettingsStore.ts +++ b/src/settings/SettingsStore.ts @@ -490,6 +490,7 @@ export default class SettingsStore { if (!handler) continue; return level; } + return null; } /** From 6d932e135290d9f982f8d7b1c3dec1b0ae08917c Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Thu, 14 Jan 2021 17:38:06 +0000 Subject: [PATCH 4/5] Fix lint errors --- src/components/views/context_menus/WidgetContextMenu.tsx | 1 - src/components/views/elements/AppTile.js | 1 - 2 files changed, 2 deletions(-) diff --git a/src/components/views/context_menus/WidgetContextMenu.tsx b/src/components/views/context_menus/WidgetContextMenu.tsx index 3c7bf0ee0e..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"; diff --git a/src/components/views/elements/AppTile.js b/src/components/views/elements/AppTile.js index 65bc0ac6f1..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"; From 97a551c04ae294201de4eff49a21c0f4c452fc6c Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Thu, 14 Jan 2021 17:39:58 +0000 Subject: [PATCH 5/5] Fix test --- test/components/views/messages/TextualBody-test.js | 1 + 1 file changed, 1 insertion(+) 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({