1 import { CommitVersion, DocumentUpdateVersion, CreateDocumentUpdate, CreateCommit } from '@proton/docs-proto'
2 import type { UseCaseInterface } from '../Domain/UseCase/UseCaseInterface'
3 import { Result } from '@proton/docs-shared'
4 import type { DocsApi } from '../Api/DocsApi'
5 import type { EncryptMessage } from './EncryptMessage'
6 import type { DocumentKeys, NodeMeta } from '@proton/drive-store'
7 import { GenerateUUID } from '../Util/GenerateUuid'
9 type SeedInitialCommitResult = { commitId: string; linkId: string; volumeId: string }
12 * Allows the client to create an initial commit. This used by the Duplicate function to allow us to seed the document
13 * with an initial commit value equaling the source document's.
15 export class SeedInitialCommit implements UseCaseInterface<SeedInitialCommitResult> {
17 private docsApi: DocsApi,
18 private encryptMessage: EncryptMessage,
21 async execute(nodeMeta: NodeMeta, state: Uint8Array, keys: DocumentKeys): Promise<Result<SeedInitialCommitResult>> {
23 version: DocumentUpdateVersion.V1,
24 authorAddress: keys.userOwnAddress,
25 timestamp: Date.now(),
27 const encryptedUpdate = await this.encryptMessage.execute(state, metadata, keys)
28 if (encryptedUpdate.isFailed()) {
29 return Result.fail<SeedInitialCommitResult>(encryptedUpdate.getError())
32 const update = CreateDocumentUpdate({
33 content: encryptedUpdate.getValue(),
34 authorAddress: metadata.authorAddress,
35 timestamp: metadata.timestamp,
36 version: metadata.version,
40 const commit = CreateCommit({
42 version: CommitVersion.V1,
46 const commitResult = await this.docsApi.seedInitialCommit(nodeMeta, commit)
48 if (commitResult.isFailed()) {
49 return Result.fail(commitResult.getError().message)
52 const { CommitID: commitId, VolumeID: volumeId, LinkID: linkId } = commitResult.getValue()
54 return Result.ok<SeedInitialCommitResult>({ commitId, volumeId, linkId })