Skip to content

armgit / ente

Public Imported from GitHub
0

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

Take 1

Manav Rathid59eed697414
diff --git a/web/packages/gallery/services/magic-metadata.ts b/web/packages/gallery/services/magic-metadata.ts
index 3eec2604ea..cb3469738e 100644
--- a/web/packages/gallery/services/magic-metadata.ts
+++ b/web/packages/gallery/services/magic-metadata.ts
@@ -49,9 +49,9 @@ export async function updateMagicMetadata<T>(
if (typeof originalMagicMetadata?.data == "string") {
// TODO: When converting this (and other parses of magic metadata) to
- // use zod, remember to use passthrough.
+ // use zod, remember to use looseObject.
//
- // See: [Note: Use passthrough for metadata Zod schemas]
+ // See: [Note: Use looseObject for metadata Zod schemas]
// @ts-expect-error TODO: Need to use zod here.
originalMagicMetadata.data = await cryptoWorker.decryptMetadataJSON(
{
diff --git a/web/packages/media/collection.ts b/web/packages/media/collection.ts
index ad096b3cbb..30756e2f9e 100644
--- a/web/packages/media/collection.ts
+++ b/web/packages/media/collection.ts
@@ -3,6 +3,8 @@ import {
type MagicMetadataCore,
} from "ente-media/file";
import { ItemVisibility } from "ente-media/file-metadata";
+import { falseyToUndefined } from "ente-utils/transform";
+import { z } from "zod/v4";
// TODO: Audit this file
@@ -109,6 +111,34 @@ export interface CollectionUser {
role?: CollectionParticipantRole;
}
+/**
+ * Zod schema for {@link CollectionUser}.
+ *
+ * [Note: String enums and looseObject for persisted remote objects]
+ *
+ * The zod schema does not enforce the enum types, e.g. in the role field. This
+ * allows the client to gracefully handle future enum values that might be sent
+ * by remote. Such future proofing is not always necessary, but since the
+ * collection type is persisted locally, such flexibility can help.
+ *
+ * This means that there is a translation step. The typed object we get from
+ * remote (and what gets persisted to local storage) does not enforce the enum,
+ * and instead retains the enum as the underlying primitive type (usually a
+ * string), and before using it in our code (as the TypeScript type), we need to
+ * validate the enum's conformance (unknown values are usually converted to
+ * `undefined`).
+ *
+ * In these cases, we also persist the object we get from remote as-is (by using
+ * looseObject), instead of discarding fields that we currently don't know of,
+ * because a future version of the client might gain support for them, and thus
+ * can read the already persisted data instead of requiring a fresh pull.
+ */
+const CollectionUser = z.looseObject({
+ id: z.number(),
+ email: z.string().nullish().transform(falseyToUndefined),
+ role: z.string().nullish().transform(falseyToUndefined),
+});
+
export interface EncryptedCollection {
/**
* The collection's globally unique ID.
diff --git a/web/packages/media/file-metadata.ts b/web/packages/media/file-metadata.ts
index c8d5b7ed9b..e5a0db8adf 100644
--- a/web/packages/media/file-metadata.ts
+++ b/web/packages/media/file-metadata.ts
@@ -318,9 +318,9 @@ export interface PublicMagicMetadata {
*
* ---
*
- * [Note: Use passthrough for metadata Zod schemas]
+ * [Note: Use looseObject for metadata Zod schemas]
*
- * It is important to (recursively) use the {@link passthrough} option when
+ * It is important to (recursively) use the {@link looseObject} option when
* defining Zod schemas for the various metadata types (the plaintext JSON
* objects) because we want to retain all the fields we get from remote. There
* might be other, newer, clients out there adding fields that the current
diff --git a/web/packages/new/photos/services/user-entity/index.ts b/web/packages/new/photos/services/user-entity/index.ts
index 0fa2118ec5..ca9f50ac27 100644
--- a/web/packages/new/photos/services/user-entity/index.ts
+++ b/web/packages/new/photos/services/user-entity/index.ts
@@ -79,7 +79,7 @@ const RemoteFaceCluster = z.looseObject({
*
* See also: {@link CGroupUserEntityData}.
*
- * See: [Note: Use passthrough for metadata Zod schemas].
+ * See: [Note: Use looseObject for metadata Zod schemas].
*/
const RemoteCGroupData = z.looseObject({
name: z.string().nullish().transform(nullToUndefined),
diff --git a/web/packages/utils/transform.ts b/web/packages/utils/transform.ts
index 905977c6d7..11c16b5472 100644
--- a/web/packages/utils/transform.ts
+++ b/web/packages/utils/transform.ts
@@ -4,6 +4,13 @@
export const nullToUndefined = <T>(v: T | null | undefined): T | undefined =>
v === null ? undefined : v;
+/**
+ * Convert any falsey value (including blank strings) to `undefined`;
+ * passthrough everything else unchanged.
+ */
+export const falseyToUndefined = <T>(v: T | null | undefined): T | undefined =>
+ v || undefined;
+
/**
* Convert `null` and `undefined` to `0`, passthrough everything else unchanged.
*/