Migrate the hidden read receipts flag to new "send read receipts" option (#9141)

* Migrate the hidden read receipts flag to new "send read receipts" option

For safety.

* Appease linter & ignore guests

* `void`
This commit is contained in:
Travis Ralston 2022-08-08 15:48:28 -04:00 committed by GitHub
parent f467d94603
commit 32478db57e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 153 additions and 6 deletions

View file

@ -35,6 +35,9 @@ import SettingsHandler from "./handlers/SettingsHandler";
import { SettingUpdatedPayload } from "../dispatcher/payloads/SettingUpdatedPayload";
import { Action } from "../dispatcher/actions";
import PlatformSettingsHandler from "./handlers/PlatformSettingsHandler";
import dispatcher from "../dispatcher/dispatcher";
import { ActionPayload } from "../dispatcher/payloads";
import { MatrixClientPeg } from "../MatrixClientPeg";
const defaultWatchManager = new WatchManager();
@ -565,6 +568,44 @@ export default class SettingsStore {
return null;
}
/**
* Runs or queues any setting migrations needed.
*/
public static runMigrations(): void {
// Dev notes: to add your migration, just add a new `migrateMyFeature` function, call it, and
// add a comment to note when it can be removed.
SettingsStore.migrateHiddenReadReceipts(); // Can be removed after October 2022.
}
private static migrateHiddenReadReceipts(): void {
if (MatrixClientPeg.get().isGuest()) return; // not worth it
// We wait for the first sync to ensure that the user's existing account data has loaded, as otherwise
// getValue() for an account-level setting like sendReadReceipts will return `null`.
const disRef = dispatcher.register((payload: ActionPayload) => {
if (payload.action === "MatrixActions.sync") {
dispatcher.unregister(disRef);
const rrVal = SettingsStore.getValue("sendReadReceipts", null, true);
if (typeof rrVal !== "boolean") {
// new setting isn't set - see if the labs flag was. We have to manually reach into the
// handler for this because it isn't a setting anymore (`getValue` will yell at us).
const handler = LEVEL_HANDLERS[SettingLevel.DEVICE] as DeviceSettingsHandler;
const labsVal = handler.readFeature("feature_hidden_read_receipts");
if (typeof labsVal === "boolean") {
// Inverse of labs flag because negative->positive language switch in setting name
const newVal = !labsVal;
console.log(`Setting sendReadReceipts to ${newVal} because of previously-set labs flag`);
// noinspection JSIgnoredPromiseFromCall
SettingsStore.setValue("sendReadReceipts", null, SettingLevel.ACCOUNT, newVal);
}
}
}
});
}
/**
* Debugging function for reading explicit setting values without going through the
* complicated/biased functions in the SettingsStore. This will print information to

View file

@ -114,12 +114,16 @@ export default class DeviceSettingsHandler extends AbstractLocalStorageSettingsH
// Note: features intentionally don't use the same key as settings to avoid conflicts
// and to be backwards compatible.
private readFeature(featureName: string): boolean | null {
// public for access to migrations - not exposed from the SettingsHandler interface
public readFeature(featureName: string): boolean | null {
if (MatrixClientPeg.get() && MatrixClientPeg.get().isGuest()) {
// Guests should not have any labs features enabled.
return false;
}
// XXX: This turns they key names into `mx_labs_feature_feature_x` (double feature).
// This is because all feature names start with `feature_` as a matter of policy.
// Oh well.
return this.getBoolean("mx_labs_feature_" + featureName);
}