Skip to content

armgit / ente

Public Imported from GitHub
0

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

Sketch

Manav Rathi3da404a714f2
diff --git a/web/packages/media/magic-metadata.ts b/web/packages/media/magic-metadata.ts
index 5d9ed2c1c7..b2c6c0057f 100644
--- a/web/packages/media/magic-metadata.ts
+++ b/web/packages/media/magic-metadata.ts
@@ -36,16 +36,28 @@ export interface RemoteMagicMetadata {
*/
version: number;
/**
- * The number of keys with non-null (and non-undefined) values in the
- * encrypted JSON object that the encrypted metadata blob contains.
+ * The number of keys with non-nullish (i.e. not null and not undefined)
+ * values in the encrypted JSON object that the encrypted metadata blob
+ * contains.
*
- * During edits and updates, this number should be greater than or equal to
- * the previous version.
+ * [Note: Magic metadata data cannot have nullish values]
*
- * > Clients are expected to retain the magic metadata verbatim so that they
- * > don't accidentally overwrite fields that they might not understand.
- * >
- * > See: [Note: Use looseObject for metadata Zod schemas]
+ * Clients are expected to retain the magic metadata verbatim so that they
+ * don't accidentally overwrite fields that they might not understand. See:
+ * [Note: Use looseObject for metadata Zod schemas]
+ *
+ * Remote enforces this by requiring that during edits and updates, this
+ * number should be greater than or equal to the previous version.
+ *
+ * Locally we enforce this in the {@link createMagicMetadata} function
+ * (which is used for both creation and updates), which trims off any
+ * entries in the data object whose value is `null` or `undefined` before
+ * arriving at the final object to use.
+ *
+ * This has the side effect that metadata entries cannot be deleted by
+ * setting their value to nullish. However usually the specific field
+ * semantics allow a reset to default behaviour by setting the value of the
+ * entry to the corresponding empty primitive (e.g. 0).
*/
count: number;
/**
@@ -151,11 +163,11 @@ export const encryptMagicMetadata = async (
* envelope used for the various magic metadata fields.
*
* A trimmed JSON object is obtained from the provided JSON object by removing
- * any entries whose values is `undefined` or `null`.
+ * any entries whose values is nullish (`null` or `undefined`).
*
* Then,
*
- * - The `version` is set to 1.
+ * - The `version` is set to provided value (or 1).
* - The `count` is set to the number of entries in the trimmed JSON object.
* - The `data` is set to the trimmed JSON object.
*
@@ -166,18 +178,26 @@ export const encryptMagicMetadata = async (
*
* @param data A JSON object. Since TypeScript does not have a JSON type, this
* uses the `unknown` type.
+ *
+ * @param version An optional version number to use. This is useful when
+ * updating existing magic metadata, where the version in the update that we
+ * send to remote must match the existing version.
+ *
+ * If not provided, the 1 is used as the version.
*/
-export const createMagicMetadata = (data: unknown) => {
- // Discard any entries that are `undefined` or `null`.
+export const createMagicMetadata = (data: unknown, version?: number) => {
+ // Discard any entries that with nullish (`null` or `undefined`) values.
+ //
+ // See: [Note: Magic metadata data cannot have nullish values]
const jsonObject = Object.fromEntries(
Object.entries(data ?? {}).filter(
- ([, v]) => v !== undefined && v !== null,
+ ([, v]) => v !== null && v !== undefined,
),
);
const count = Object.keys(jsonObject).length;
- return { version: 1, count, data: jsonObject };
+ return { version: version ?? 1, count, data: jsonObject };
};
/**
diff --git a/web/packages/new/photos/services/collection.ts b/web/packages/new/photos/services/collection.ts
index 37280cd713..6b60e31a80 100644
--- a/web/packages/new/photos/services/collection.ts
+++ b/web/packages/new/photos/services/collection.ts
@@ -14,6 +14,7 @@ import {
decryptRemoteCollection,
RemoteCollection,
type Collection,
+ type Collection2,
type CollectionNewParticipantRole,
type CollectionPrivateMagicMetadataData,
type CollectionType,
@@ -223,7 +224,7 @@ export interface MoveToCollectionRequest {
* Make a remote request to add the given {@link files} to the given
* {@link collection}.
*
- * Does not modify local state.
+ * Remote only, does not modify local state.
*/
export const addToCollection = async (
collection: Collection,
@@ -251,7 +252,7 @@ export const addToCollection = async (
* Make a remote request to restore the given {@link files} to the given
* {@link collection}.
*
- * Does not modify local state.
+ * Remote only, does not modify local state.
*/
export const restoreToCollection = async (
collection: Collection,
@@ -280,7 +281,7 @@ export const restoreToCollection = async (
* identified by its {@link fromCollectionID}) to the given
* {@link toCollection}.
*
- * Does not modify local state.
+ * Remote only, does not modify local state.
*/
export const moveToCollection = async (
fromCollectionID: number,
@@ -333,7 +334,7 @@ const encryptWithCollectionKey = async (
* both a file ID and a collection ID, but there should be at most one entry for
* a particular fileID in this array.
*
- * Does not modify local state.
+ * Remote only, does not modify local state.
*/
export const moveToTrash = async (files: EnteFile[]) => {
for (const batchFiles of batch(files, requestBatchSize)) {
@@ -355,7 +356,7 @@ export const moveToTrash = async (files: EnteFile[]) => {
/**
* Make a remote request to delete the given {@link fileIDs} from trash.
*
- * Does not modify local state.
+ * Remote only, does not modify local state.
*/
export const deleteFromTrash = async (fileIDs: number[]) => {
for (const batchIDs of batch(fileIDs, requestBatchSize)) {
@@ -369,6 +370,28 @@ export const deleteFromTrash = async (fileIDs: number[]) => {
}
};
+/**
+ * Update the private magic metadata contents of a collection on remote.
+ *
+ * Remote only, does not modify local state.
+ *
+ * @param collection The collection whose magic metadata we want to update. In
+ * particular, the existing magic metadata of this collection is used both to
+ * obtain the current magic metadata version, and the existing contents on top
+ * of which the updates are applied.
+ *
+ * @param updates A non-empty subset of
+ * {@link CollectionPrivateMagicMetadataData} entries.
+ *
+ * See: [Note: Magic metadata data cannot have nullish values]
+ */
+export const updateCollectionPrivateMagicMetadata = async (
+ collection: Collection2,
+ updates: CollectionPrivateMagicMetadataData,
+) => {
+ throw new Error("TODO");
+};
+
/**
* Share the provided collection with another Ente user.
*