From 3b6bee6042b95d3215f0787f5a5afd8e4711aaa5 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Thu, 26 Sep 2024 10:47:42 +0530 Subject: [PATCH] Only one diff sync per set --- .../new/photos/services/user-entity/index.ts | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) 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}.