1 import type { DecryptMessage } from './DecryptMessage'
2 import type { Commit } from '@proton/docs-proto'
3 import type { UseCaseInterface } from '../Domain/UseCase/UseCaseInterface'
4 import { Result } from '@proton/docs-shared'
5 import type { DecryptedMessage } from '@proton/docs-shared'
6 import { DecryptedCommit } from '../Models/DecryptedCommit'
7 import metrics from '@proton/metrics'
8 import type { SessionKey } from '@proton/crypto/lib'
10 type DecryptCommitDTO = {
13 documentContentKey: SessionKey
16 const VERIFY_VALUE_FALSE_DUE_TO_COMMIT_HAVING_SEPARATE_VERIFICATION = false
18 export class DecryptCommit implements UseCaseInterface<DecryptedCommit> {
19 constructor(private decryptMessage: DecryptMessage) {}
21 async execute(dto: DecryptCommitDTO): Promise<Result<DecryptedCommit>> {
22 const updates = dto.commit.updates.documentUpdates
24 const decryptedResults: Result<DecryptedMessage>[] = await Promise.all(
25 updates.map((update) =>
26 this.decryptMessage.execute({
28 documentContentKey: dto.documentContentKey,
29 verify: VERIFY_VALUE_FALSE_DUE_TO_COMMIT_HAVING_SEPARATE_VERIFICATION,
34 const failedResults = decryptedResults.filter((result) => result.isFailed())
35 if (failedResults.length > 0) {
36 for (const _ of failedResults) {
37 metrics.docs_document_updates_decryption_error_total.increment({
42 return Result.fail(failedResults[0].getError())
45 const commit = new DecryptedCommit(
47 decryptedResults.map((result) => result.getValue()),
50 return Result.ok(commit)