1 import type { ApiResult, CommentType } from '@proton/docs-shared'
2 import type { HttpHeaders } from '../Types/HttpHeaders'
3 import type { AddCommentToThreadResponse } from '../Types/AddCommentToThreadResponse'
4 import { DocsApiPrivateRouteBuilder } from '../Routes/DocsApiPrivateRouteBuilder'
5 import { DocsApiPublicRouteBuilder } from '../Routes/DocsApiPublicRouteBuilder'
6 import type { RouteExecutor } from '../RouteExecutor'
7 import type { DocumentEntitlements } from '../../Types/DocumentEntitlements'
8 import { isPublicDocumentEntitlements } from '../../Types/DocumentEntitlements'
9 import type { PublicDocumentEntitlements } from '../../Types/DocumentEntitlements'
11 export type AddCommentToThreadDTO = {
13 encryptedContent: string
14 parentCommentId: string | null
16 decryptedDocumentName: string | null
19 export class ApiAddCommentToThread {
21 private readonly executor: RouteExecutor,
22 private publicContextHeaders: HttpHeaders | undefined,
26 common: AddCommentToThreadDTO,
27 entitlements: PublicDocumentEntitlements | DocumentEntitlements,
28 ): Promise<ApiResult<AddCommentToThreadResponse>> {
29 if (isPublicDocumentEntitlements(entitlements)) {
30 return this.executePublic(common, entitlements)
32 return this.executePrivate(common, entitlements)
36 private async executePrivate(
37 dto: AddCommentToThreadDTO,
38 entitlements: DocumentEntitlements,
39 ): Promise<ApiResult<AddCommentToThreadResponse>> {
40 const route = new DocsApiPrivateRouteBuilder({
41 volumeId: entitlements.nodeMeta.volumeId,
42 linkId: entitlements.nodeMeta.linkId,
44 threadId: dto.threadId,
47 ParentCommentId: dto.parentCommentId,
48 DocumentName: dto.decryptedDocumentName,
49 Content: dto.encryptedContent,
50 AuthorEmail: entitlements.keys.userOwnAddress,
54 return this.executor.execute(route)
57 private async executePublic(
58 dto: AddCommentToThreadDTO,
59 entitlements: PublicDocumentEntitlements,
60 ): Promise<ApiResult<AddCommentToThreadResponse>> {
61 if (!this.publicContextHeaders) {
62 throw new Error('Public context headers not set')
65 const route = new DocsApiPublicRouteBuilder({
66 token: entitlements.nodeMeta.token,
67 linkId: entitlements.nodeMeta.linkId,
68 headers: this.publicContextHeaders,
70 threadId: dto.threadId,
73 ParentCommentId: dto.parentCommentId,
74 DocumentName: dto.decryptedDocumentName,
75 Content: dto.encryptedContent,
79 return this.executor.execute(route)