Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Combine

Manav Rathi56bac2160e39
diff --git a/web/apps/photos/src/components/Sidebar/index.tsx b/web/apps/photos/src/components/Sidebar/index.tsx
index d5ca9fb601..a053a42f14 100644
--- a/web/apps/photos/src/components/Sidebar/index.tsx
+++ b/web/apps/photos/src/components/Sidebar/index.tsx
@@ -675,7 +675,7 @@ const DebugSection: React.FC = () => {
);
};
-// TODO: Legacy synchronous check, use the one for feature-flags.ts instead.
+// TODO: Legacy synchronous check, use the one from remote-store.ts instead.
const isInternalUserViaEmailCheck = () => {
const userEmail = getData(LS_KEYS.USER)?.email;
if (!userEmail) return false;
diff --git a/web/apps/photos/src/services/logout.ts b/web/apps/photos/src/services/logout.ts
index fa36e4f8fa..238dbe5565 100644
--- a/web/apps/photos/src/services/logout.ts
+++ b/web/apps/photos/src/services/logout.ts
@@ -1,7 +1,7 @@
import { accountLogout } from "@/accounts/services/logout";
import log from "@/base/log";
import DownloadManager from "@/new/photos/services/download";
-import { clearFeatureFlagSessionState } from "@/new/photos/services/feature-flags";
+import { clearFeatureFlagSessionState } from "@/new/photos/services/remote-store";
import { logoutML, terminateMLWorker } from "@/new/photos/services/ml";
import { logoutSearch } from "@/new/photos/services/search";
import exportService from "./export";
diff --git a/web/apps/photos/src/services/sync.ts b/web/apps/photos/src/services/sync.ts
index 012af2fe11..77ac72081a 100644
--- a/web/apps/photos/src/services/sync.ts
+++ b/web/apps/photos/src/services/sync.ts
@@ -1,4 +1,4 @@
-import { triggerFeatureFlagsFetchIfNeeded } from "@/new/photos/services/feature-flags";
+import { triggerFeatureFlagsFetchIfNeeded } from "@/new/photos/services/remote-store";
import { isMLSupported, mlStatusSync, mlSync } from "@/new/photos/services/ml";
import { searchDataSync } from "@/new/photos/services/search";
import { syncMapEnabled } from "services/userService";
diff --git a/web/packages/new/photos/services/feature-flags.ts b/web/packages/new/photos/services/feature-flags.ts
deleted file mode 100644
index d694522228..0000000000
--- a/web/packages/new/photos/services/feature-flags.ts
+++ /dev/null
@@ -1,128 +0,0 @@
-import { authenticatedRequestHeaders, ensureOk } from "@/base/http";
-import { localUser } from "@/base/local-user";
-import log from "@/base/log";
-import { apiURL } from "@/base/origins";
-import { nullToUndefined } from "@/utils/transform";
-import { z } from "zod";
-
-let _fetchTimeout: ReturnType<typeof setTimeout> | undefined;
-let _haveFetched = false;
-
-/**
- * Fetch feature flags (potentially user specific) from remote and save them in
- * local storage for subsequent lookup.
- *
- * It fetches only once per session, and so is safe to call as arbitrarily many
- * times. Remember to call {@link clearFeatureFlagSessionState} on logout to
- * clear any in memory state so that these can be fetched again on the
- * subsequent login.
- *
- * [Note: Feature Flags]
- *
- * The workflow with feature flags is:
- *
- * 1. On app start feature flags are fetched once and saved in local storage. If
- * this fetch fails, we try again periodically (on every "sync") until
- * success.
- *
- * 2. Attempts to access any individual feature flage (e.g.
- * {@link isInternalUser}) returns the corresponding value from local storage
- * (substituting a default if needed).
- *
- * 3. However, if perchance the fetch-on-app-start hasn't completed yet (or had
- * failed), then a new fetch is tried. If even this fetch fails, we return
- * the default. Otherwise the now fetched result is saved to local storage
- * and the corresponding value returned.
- */
-export const triggerFeatureFlagsFetchIfNeeded = () => {
- if (_haveFetched) return;
- if (_fetchTimeout) return;
- // Not critical, so fetch these after some delay.
- _fetchTimeout = setTimeout(() => {
- _fetchTimeout = undefined;
- void fetchAndSaveFeatureFlags().then(() => {
- _haveFetched = true;
- });
- }, 2000);
-};
-
-export const clearFeatureFlagSessionState = () => {
- if (_fetchTimeout) {
- clearTimeout(_fetchTimeout);
- _fetchTimeout = undefined;
- }
- _haveFetched = false;
-};
-
-/**
- * Fetch feature flags (potentially user specific) from remote and save them in
- * local storage for subsequent lookup.
- */
-const fetchAndSaveFeatureFlags = () =>
- fetchFeatureFlags()
- .then((res) => res.text())
- .then(saveFlagJSONString);
-
-const fetchFeatureFlags = async () => {
- const res = await fetch(await apiURL("/remote-store/feature-flags"), {
- headers: await authenticatedRequestHeaders(),
- });
- ensureOk(res);
- return res;
-};
-
-const saveFlagJSONString = (s: string) =>
- localStorage.setItem("remoteFeatureFlags", s);
-
-const remoteFeatureFlags = () => {
- const s = localStorage.getItem("remoteFeatureFlags");
- if (!s) return undefined;
- return FeatureFlags.parse(JSON.parse(s));
-};
-
-const FeatureFlags = z.object({
- internalUser: z.boolean().nullish().transform(nullToUndefined),
- betaUser: z.boolean().nullish().transform(nullToUndefined),
-});
-
-type FeatureFlags = z.infer<typeof FeatureFlags>;
-
-const remoteFeatureFlagsFetchingIfNeeded = async () => {
- let ff = remoteFeatureFlags();
- if (!ff) {
- try {
- await fetchAndSaveFeatureFlags();
- } catch (e) {
- log.warn("Ignoring error when fetching feature flags", e);
- }
- ff = remoteFeatureFlags();
- }
- return ff;
-};
-
-/**
- * Return `true` if the current user is marked as an "internal" user.
- *
- * 1. Emails that end in `@ente.io` are considered as internal users.
- * 2. If the "internalUser" remote feature flag is set, the user is internal.
- * 3. Otherwise false.
- *
- * See also: [Note: Feature Flags].
- */
-export const isInternalUser = async () => {
- const user = localUser();
- if (user?.email.endsWith("@ente.io")) return true;
-
- const flags = await remoteFeatureFlagsFetchingIfNeeded();
- return flags?.internalUser ?? false;
-};
-
-/**
- * Return `true` if the current user is marked as a "beta" user.
- *
- * See also: [Note: Feature Flags].
- */
-export const isBetaUser = async () => {
- const flags = await remoteFeatureFlagsFetchingIfNeeded();
- return flags?.betaUser ?? false;
-};
diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts
index 5e091f9f6a..73280c0f70 100644
--- a/web/packages/new/photos/services/ml/index.ts
+++ b/web/packages/new/photos/services/ml/index.ts
@@ -41,9 +41,9 @@ import type { CLIPMatches } from "./worker-types";
/**
* Internal state of the ML subsystem.
*
- * This are essentially cached values used by the functions of this module.
+ * These are essentially cached values used by the functions of this module.
*
- * This should be cleared on logout.
+ * They will be cleared on logout.
*/
class MLState {
/**
diff --git a/web/packages/new/photos/services/remote-store.ts b/web/packages/new/photos/services/remote-store.ts
index 875f0dd3bc..d37db83be1 100644
--- a/web/packages/new/photos/services/remote-store.ts
+++ b/web/packages/new/photos/services/remote-store.ts
@@ -1,7 +1,132 @@
import { authenticatedRequestHeaders, ensureOk } from "@/base/http";
+import { localUser } from "@/base/local-user";
+import log from "@/base/log";
import { apiURL } from "@/base/origins";
+import { nullToUndefined } from "@/utils/transform";
import { z } from "zod";
+let _fetchTimeout: ReturnType<typeof setTimeout> | undefined;
+let _haveFetched = false;
+
+/**
+ * Fetch feature flags (potentially user specific) from remote and save them in
+ * local storage for subsequent lookup.
+ *
+ * It fetches only once per session, and so is safe to call as arbitrarily many
+ * times. Remember to call {@link clearFeatureFlagSessionState} on logout to
+ * clear any in memory state so that these can be fetched again on the
+ * subsequent login.
+ *
+ * [Note: Feature Flags]
+ *
+ * The workflow with feature flags is:
+ *
+ * 1. On app start feature flags are fetched once and saved in local storage. If
+ * this fetch fails, we try again periodically (on every "sync") until
+ * success.
+ *
+ * 2. Attempts to access any individual feature flage (e.g.
+ * {@link isInternalUser}) returns the corresponding value from local storage
+ * (substituting a default if needed).
+ *
+ * 3. However, if perchance the fetch-on-app-start hasn't completed yet (or had
+ * failed), then a new fetch is tried. If even this fetch fails, we return
+ * the default. Otherwise the now fetched result is saved to local storage
+ * and the corresponding value returned.
+ */
+export const triggerFeatureFlagsFetchIfNeeded = () => {
+ if (_haveFetched) return;
+ if (_fetchTimeout) return;
+ // Not critical, so fetch these after some delay.
+ _fetchTimeout = setTimeout(() => {
+ _fetchTimeout = undefined;
+ void fetchAndSaveFeatureFlags().then(() => {
+ _haveFetched = true;
+ });
+ }, 2000);
+};
+
+export const clearFeatureFlagSessionState = () => {
+ if (_fetchTimeout) {
+ clearTimeout(_fetchTimeout);
+ _fetchTimeout = undefined;
+ }
+ _haveFetched = false;
+};
+
+/**
+ * Fetch feature flags (potentially user specific) from remote and save them in
+ * local storage for subsequent lookup.
+ */
+const fetchAndSaveFeatureFlags = () =>
+ fetchFeatureFlags()
+ .then((res) => res.text())
+ .then(saveFlagJSONString);
+
+const fetchFeatureFlags = async () => {
+ const res = await fetch(await apiURL("/remote-store/feature-flags"), {
+ headers: await authenticatedRequestHeaders(),
+ });
+ ensureOk(res);
+ return res;
+};
+
+const saveFlagJSONString = (s: string) =>
+ localStorage.setItem("remoteFeatureFlags", s);
+
+const remoteFeatureFlags = () => {
+ const s = localStorage.getItem("remoteFeatureFlags");
+ if (!s) return undefined;
+ return FeatureFlags.parse(JSON.parse(s));
+};
+
+const FeatureFlags = z.object({
+ internalUser: z.boolean().nullish().transform(nullToUndefined),
+ betaUser: z.boolean().nullish().transform(nullToUndefined),
+});
+
+type FeatureFlags = z.infer<typeof FeatureFlags>;
+
+const remoteFeatureFlagsFetchingIfNeeded = async () => {
+ let ff = remoteFeatureFlags();
+ if (!ff) {
+ try {
+ await fetchAndSaveFeatureFlags();
+ } catch (e) {
+ log.warn("Ignoring error when fetching feature flags", e);
+ }
+ ff = remoteFeatureFlags();
+ }
+ return ff;
+};
+
+/**
+ * Return `true` if the current user is marked as an "internal" user.
+ *
+ * 1. Emails that end in `@ente.io` are considered as internal users.
+ * 2. If the "internalUser" remote feature flag is set, the user is internal.
+ * 3. Otherwise false.
+ *
+ * See also: [Note: Feature Flags].
+ */
+export const isInternalUser = async () => {
+ const user = localUser();
+ if (user?.email.endsWith("@ente.io")) return true;
+
+ const flags = await remoteFeatureFlagsFetchingIfNeeded();
+ return flags?.internalUser ?? false;
+};
+
+/**
+ * Return `true` if the current user is marked as a "beta" user.
+ *
+ * See also: [Note: Feature Flags].
+ */
+export const isBetaUser = async () => {
+ const flags = await remoteFeatureFlagsFetchingIfNeeded();
+ return flags?.betaUser ?? false;
+};
+
/**
* Fetch the value for the given {@link key} from remote store.
*