Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / docs-core / lib / Api / Requests / ApiCreateThread.ts
blob8351203ec5e3f26a2d99ef3d57ca93026ca8d2f1
1 import type { CommentType } from 'packages/docs-shared/lib/CommentType'
2 import type { HttpHeaders } from '../Types/HttpHeaders'
3 import type { CommentThreadType } from 'packages/docs-shared/lib/CommentThreadType'
4 import type { CreateThreadResponse } from '../Types/CreateThreadResponse'
5 import type { ApiResult } from '@proton/docs-shared'
6 import { DocsApiPrivateRouteBuilder } from '../Routes/DocsApiPrivateRouteBuilder'
7 import { DocsApiPublicRouteBuilder } from '../Routes/DocsApiPublicRouteBuilder'
8 import type { RouteExecutor } from '../RouteExecutor'
9 import type { DocumentEntitlements } from '../../Types/DocumentEntitlements'
10 import type { PublicDocumentEntitlements } from '../../Types/DocumentEntitlements'
11 import { isPublicDocumentEntitlements } from '../../Types/DocumentEntitlements'
13 export type CreateThreadDTO = {
14   markId: string
15   encryptedMainCommentContent: string
16   type: CommentThreadType
17   commentType: CommentType
18   decryptedDocumentName: string | null
21 export class ApiCreateThread {
22   constructor(
23     private readonly executor: RouteExecutor,
24     private publicContextHeaders: HttpHeaders | undefined,
25   ) {}
27   async execute(
28     dto: CreateThreadDTO,
29     entitlements: PublicDocumentEntitlements | DocumentEntitlements,
30   ): Promise<ApiResult<CreateThreadResponse>> {
31     if (isPublicDocumentEntitlements(entitlements)) {
32       return this.executePublic(dto, entitlements)
33     } else {
34       return this.executePrivate(dto, entitlements)
35     }
36   }
38   private async executePrivate(
39     dto: CreateThreadDTO,
40     entitlements: DocumentEntitlements,
41   ): Promise<ApiResult<CreateThreadResponse>> {
42     const route = new DocsApiPrivateRouteBuilder({
43       volumeId: entitlements.nodeMeta.volumeId,
44       linkId: entitlements.nodeMeta.linkId,
45     }).createThread({
46       data: {
47         Mark: dto.markId,
48         Comment: {
49           Type: dto.commentType,
50           AuthorEmail: entitlements.keys.userOwnAddress,
51           Content: dto.encryptedMainCommentContent,
52           DocumentName: dto.decryptedDocumentName,
53         },
54         Type: dto.type,
55       },
56     })
58     return this.executor.execute(route)
59   }
61   private async executePublic(
62     dto: CreateThreadDTO,
63     entitlements: PublicDocumentEntitlements,
64   ): Promise<ApiResult<CreateThreadResponse>> {
65     if (!this.publicContextHeaders) {
66       throw new Error('Public context headers not set')
67     }
69     const route = new DocsApiPublicRouteBuilder({
70       token: entitlements.nodeMeta.token,
71       linkId: entitlements.nodeMeta.linkId,
72       headers: this.publicContextHeaders,
73     }).createThread({
74       data: {
75         Mark: dto.markId,
76         Comment: {
77           Type: dto.commentType,
78           Content: dto.encryptedMainCommentContent,
79           DocumentName: dto.decryptedDocumentName,
80         },
81         Type: dto.type,
82       },
83     })
85     return this.executor.execute(route)
86   }