Skip to content

armgit / ente

Public Imported from GitHub
0

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

Only one diff sync per set

Manav Rathi3b6bee6042b9
diff --git a/web/packages/new/photos/services/user-entity/index.ts b/web/packages/new/photos/services/user-entity/index.ts
index 0186c326fb..a0a0743e65 100644
--- a/web/packages/new/photos/services/user-entity/index.ts
+++ b/web/packages/new/photos/services/user-entity/index.ts
@@ -154,31 +154,26 @@ export const pullUserEntities = async (
const isGzipped = (type: EntityType) => type == "cgroup";
/**
- * Add or update a user entity of the given {@link type}.
+ * Create a new user entity of the given {@link type}.
*
* @param data Arbitrary data associated with the entity. The format of the data
* is specific to each entity type, but the provided data should be JSON
* serializable (Typescript does not have a native JSON type, so we need to
* specify this as an `unknown`).
*
- * @param id If updating an existing entity, set this to the id of the existing
- * entity.
- *
* @param masterKey The user's masterKey, which is is used to encrypt and
* decrypt the entity key.
*/
-export const addOrUpdateUserEntity = async (
+export const addUserEntity = async (
type: EntityType,
data: unknown,
- id: string | undefined,
masterKey: Uint8Array,
) => {
- const encryptedBlob = await encryptedUserEntityData(type, data, masterKey);
-
- await (id
- ? putUserEntity(id, type, encryptedBlob)
- : postUserEntity(type, encryptedBlob));
-
+ // Create it on remote.
+ await postUserEntity(
+ type,
+ await encryptedUserEntityData(type, data, masterKey),
+ );
// Perform a diff sync to update our local state.
return pullUserEntities(type, masterKey);
};
@@ -197,6 +192,30 @@ export const encryptedUserEntityData = async (
return encryptBlobB64(bytes, entityKeyB64);
};
+/**
+ * Update the given user entities (both on remote and locally), creating them if
+ * they don't exist.
+ *
+ * @param masterKey The user's masterKey, which is is used to encrypt and
+ * decrypt the entity key.
+ */
+export const updateOrCreateUserEntities = async (
+ type: EntityType,
+ entities: LocalUserEntity[],
+ masterKey: Uint8Array,
+) => {
+ // PUT all of them.
+ await Promise.all(
+ entities.map(({ id, data }) =>
+ encryptedUserEntityData(type, data, masterKey).then(
+ (encryptedBlob) => putUserEntity(id, type, encryptedBlob),
+ ),
+ ),
+ );
+ // Perform a diff sync to update our local state.
+ return pullUserEntities(type, masterKey);
+};
+
/**
* Return the entity key that can be used to decrypt the encrypted contents of
* user entities of the given {@link type}.