Skip to content

armgit / ente

Public Imported from GitHub
0

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

Sketch 2

Manav Rathi8585f3881c26
diff --git a/web/packages/media/collection.ts b/web/packages/media/collection.ts
index 16cd248b1e..6faa204e41 100644
--- a/web/packages/media/collection.ts
+++ b/web/packages/media/collection.ts
@@ -425,6 +425,28 @@ export const decryptRemoteCollection = (
user: CollectionDecryptionUser,
): Promise<Collection> => {};
+/**
+ * Decrypt a remote collection using the provided key.
+ *
+ * This is the lower level implementation of {@link decryptRemoteCollection} for
+ * use by code that already has determined which key should be used for
+ * decrypting the collection.
+ *
+ * @param remoteCollection The collection to decrypt.
+ *
+ * @param key The base64 encoded key to use for decrypting the various encrypted
+ * fields in {@link remoteCollection}.
+ *
+ * @returns A decrypted collection ({@link Collection2}).
+ */
+export const decryptRemoteCollectionUsingKey = (
+ remoteCollection: RemoteCollection,
+ key: string,
+): Promise<Collection2> => {
+ // eslint-disable-next-line @typescript-eslint/no-base-to-string
+ throw new Error("TODO" + remoteCollection.toString() + key.toString());
+};
+
/**
* Additional context stored as part of the collection's magic metadata to
* augment the {@link type} associated with a {@link Collection}.
diff --git a/web/packages/new/photos/services/collection.ts b/web/packages/new/photos/services/collection.ts
index 6697850801..cda3f50bb7 100644
--- a/web/packages/new/photos/services/collection.ts
+++ b/web/packages/new/photos/services/collection.ts
@@ -1,17 +1,27 @@
import type { User } from "ente-accounts/services/user";
-import { boxSeal, encryptBox } from "ente-base/crypto";
+import { boxSeal, encryptBox, generateKey } from "ente-base/crypto";
import { authenticatedRequestHeaders, ensureOk } from "ente-base/http";
import { apiURL } from "ente-base/origins";
+import { ensureMasterKeyFromSession } from "ente-base/session";
import {
CollectionSubType,
+ decryptRemoteCollectionUsingKey,
+ RemoteCollectionSchema,
type Collection,
+ type Collection2,
type CollectionNewParticipantRole,
type CollectionPrivateMagicMetadata,
type CollectionType,
+ type RemoteCollection,
} from "ente-media/collection";
import { type EnteFile } from "ente-media/file";
import { ItemVisibility } from "ente-media/file-metadata";
+import {
+ createMagicMetadataEnvelope,
+ encryptMagicMetadata,
+} from "ente-media/magic-metadata";
import { batch } from "ente-utils/array";
+import { z } from "zod/v4";
import { getPublicKey } from "./user";
/**
@@ -95,11 +105,51 @@ export const getCollectionUserFacingName = (collection: Collection) => {
* @param magicMetadata Optional metadata to use as the collection's private
* mutable metadata when creating the new collection.
*/
-export const createCollection = (
+export const createCollection2 = async (
name: string,
type: CollectionType,
magicMetadata?: CollectionPrivateMagicMetadata,
-): Promise<Collection> => {};
+): Promise<Collection2> => {
+ const masterKey = await ensureMasterKeyFromSession();
+ const collectionKey = await generateKey();
+ const { encryptedData: encryptedKey, nonce: keyDecryptionNonce } =
+ await encryptBox(collectionKey, masterKey);
+ const { encryptedData: encryptedName, nonce: nameDecryptionNonce } =
+ await encryptBox(new TextEncoder().encode(name), collectionKey);
+ const remoteMagicMetadata = await encryptMagicMetadata(
+ createMagicMetadataEnvelope(magicMetadata),
+ collectionKey,
+ );
+
+ const remoteCollection = await postCollections({
+ encryptedKey,
+ keyDecryptionNonce,
+ encryptedName,
+ nameDecryptionNonce,
+ type,
+ ...(remoteMagicMetadata ? { magicMetadata: remoteMagicMetadata } : {}),
+ });
+
+ return decryptRemoteCollectionUsingKey(remoteCollection, masterKey);
+};
+
+/**
+ * Create a collection on remote with the provided data, and return the new
+ * remote collection object returned by remote on success.
+ */
+const postCollections = async (
+ collectionData: Partial<RemoteCollection>,
+): Promise<RemoteCollection> => {
+ const res = await fetch(await apiURL("/collections"), {
+ method: "POST",
+ headers: await authenticatedRequestHeaders(),
+ body: JSON.stringify(collectionData),
+ });
+ ensureOk(res);
+ return z
+ .object({ collection: RemoteCollectionSchema })
+ .parse(await res.json()).collection;
+};
/**
* Return a map of the (user-facing) collection name, indexed by collection ID.