From 71643862c04b93976c3cc77b3d848e2d4fb4b30c Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Mon, 17 Aug 2020 13:24:55 -0600
Subject: [PATCH] Implement new config style for features

---
 src/settings/SettingsStore.ts                  |  2 +-
 src/settings/handlers/ConfigSettingsHandler.ts | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/settings/SettingsStore.ts b/src/settings/SettingsStore.ts
index ea7d965720..dd75f8d1c9 100644
--- a/src/settings/SettingsStore.ts
+++ b/src/settings/SettingsStore.ts
@@ -53,7 +53,7 @@ const LEVEL_HANDLERS = {
     [SettingLevel.ROOM_ACCOUNT]: new RoomAccountSettingsHandler(defaultWatchManager),
     [SettingLevel.ACCOUNT]: new AccountSettingsHandler(defaultWatchManager),
     [SettingLevel.ROOM]: new RoomSettingsHandler(defaultWatchManager),
-    [SettingLevel.CONFIG]: new ConfigSettingsHandler(),
+    [SettingLevel.CONFIG]: new ConfigSettingsHandler(featureNames),
     [SettingLevel.DEFAULT]: new DefaultSettingsHandler(defaultSettings, invertedDefaultSettings),
 };
 
diff --git a/src/settings/handlers/ConfigSettingsHandler.ts b/src/settings/handlers/ConfigSettingsHandler.ts
index 3e8b1724c1..791c655a0d 100644
--- a/src/settings/handlers/ConfigSettingsHandler.ts
+++ b/src/settings/handlers/ConfigSettingsHandler.ts
@@ -24,9 +24,24 @@ import {isNullOrUndefined} from "matrix-js-sdk/src/utils";
  * roomId parameter.
  */
 export default class ConfigSettingsHandler extends SettingsHandler {
+    public constructor(private featureNames: string[]) {
+        super();
+    }
+
     public getValue(settingName: string, roomId: string): any {
         const config = SdkConfig.get() || {};
 
+        if (this.featureNames.includes(settingName)) {
+            const labsConfig = config["features"] || {};
+            const val = labsConfig[settingName];
+            if (isNullOrUndefined(val)) return null; // no definition at this level
+            if (val === true || val === false) return val; // new style: mapped as a boolean
+            if (val === "enable") return true; // backwards compat
+            if (val === "disable") return false; // backwards compat
+            if (val === "labs") return null; // backwards compat, no override
+            return null; // fallback in the case of invalid input
+        }
+
         // Special case themes
         if (settingName === "theme") {
             return config["default_theme"];