Skip to content

Commit

Permalink
feat: Sync in the same time bitwarden and cozy ciphers
Browse files Browse the repository at this point in the history
Instead of doing 1 replace for bitwarden ciphers and 2 upsert for
cozy ciphers, we update syncCiphers method to accept our cozy
ciphers. So 1 replace will be enough for bitwarden and cozy ciphers
and it is faster.
  • Loading branch information
zatteo committed Apr 26, 2024
1 parent d6531bf commit e4470bb
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions libs/common/src/vault/services/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,19 @@ export class SyncService implements SyncServiceAbstraction {

const [papersPromise, contactsPromise] = await Promise.allSettled(fetchPromises);

/*
Because syncCiphers replace previous ciphers, we need to do syncCiphers :
- after we converted papers and contacts because papers and contacts conversion
can try to reuse previous ciphers
- before we upsert papers and contacts because we want to have
bitwarden ciphers and papers and contacts ciphers
*/
await this.syncCiphers(response.ciphers);
let cozyCiphers: CipherData[] = [];

if (papersPromise.status === "fulfilled") {
console.log(`${papersPromise.value.length} contacts ciphers will be added`);

await this.cipherService.upsert(papersPromise.value);
cozyCiphers = cozyCiphers.concat(papersPromise.value);
}

if (contactsPromise.status === "fulfilled") {
console.log(`${contactsPromise.value.length} papers ciphers will be added`);

await this.cipherService.upsert(contactsPromise.value);
cozyCiphers = cozyCiphers.concat(contactsPromise.value);
}

await this.syncCiphers(response.ciphers, cozyCiphers);
// Cozy customization end

await this.syncSends(response.sends);
Expand Down Expand Up @@ -405,13 +398,27 @@ export class SyncService implements SyncServiceAbstraction {
return await this.collectionService.replace(collections);
}

// Cozy customization, sync in the same time bitwarden ciphers (response) and cozy ciphers (data)
//*
private async syncCiphers(response: CipherResponse[], data: CipherData[]) {
const ciphers: { [id: string]: CipherData } = {};
response.forEach((c) => {
ciphers[c.id] = new CipherData(c);
});
data.forEach((c) => {
ciphers[c.id] = c;
});
return await this.cipherService.replace(ciphers);
}
/*/
private async syncCiphers(response: CipherResponse[]) {
const ciphers: { [id: string]: CipherData } = {};
response.forEach((c) => {
ciphers[c.id] = new CipherData(c);
});
return await this.cipherService.replace(ciphers);
}
//*/

private async syncSends(response: SendResponse[]) {
const sends: { [id: string]: SendData } = {};
Expand Down

0 comments on commit e4470bb

Please sign in to comment.