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 = {
15 encryptedMainCommentContent: string
16 type: CommentThreadType
17 commentType: CommentType
18 decryptedDocumentName: string | null
21 export class ApiCreateThread {
23 private readonly executor: RouteExecutor,
24 private publicContextHeaders: HttpHeaders | undefined,
29 entitlements: PublicDocumentEntitlements | DocumentEntitlements,
30 ): Promise<ApiResult<CreateThreadResponse>> {
31 if (isPublicDocumentEntitlements(entitlements)) {
32 return this.executePublic(dto, entitlements)
34 return this.executePrivate(dto, entitlements)
38 private async executePrivate(
40 entitlements: DocumentEntitlements,
41 ): Promise<ApiResult<CreateThreadResponse>> {
42 const route = new DocsApiPrivateRouteBuilder({
43 volumeId: entitlements.nodeMeta.volumeId,
44 linkId: entitlements.nodeMeta.linkId,
49 Type: dto.commentType,
50 AuthorEmail: entitlements.keys.userOwnAddress,
51 Content: dto.encryptedMainCommentContent,
52 DocumentName: dto.decryptedDocumentName,
58 return this.executor.execute(route)
61 private async executePublic(
63 entitlements: PublicDocumentEntitlements,
64 ): Promise<ApiResult<CreateThreadResponse>> {
65 if (!this.publicContextHeaders) {
66 throw new Error('Public context headers not set')
69 const route = new DocsApiPublicRouteBuilder({
70 token: entitlements.nodeMeta.token,
71 linkId: entitlements.nodeMeta.linkId,
72 headers: this.publicContextHeaders,
77 Type: dto.commentType,
78 Content: dto.encryptedMainCommentContent,
79 DocumentName: dto.decryptedDocumentName,
85 return this.executor.execute(route)