1 import type { HttpHeaders } from '../Types/HttpHeaders'
2 import type { EditCommentResponse } from '../Types/EditCommentResponse'
3 import type { ApiResult } from '@proton/docs-shared'
4 import { DocsApiPrivateRouteBuilder } from '../Routes/DocsApiPrivateRouteBuilder'
5 import { DocsApiPublicRouteBuilder } from '../Routes/DocsApiPublicRouteBuilder'
6 import type { RouteExecutor } from '../RouteExecutor'
7 import type { PublicDocumentEntitlements } from '../../Types/DocumentEntitlements'
8 import { isPublicDocumentEntitlements } from '../../Types/DocumentEntitlements'
9 import type { DocumentEntitlements } from '../../Types/DocumentEntitlements'
11 export type EditCommentDTO = {
14 encryptedContent: string
17 export class ApiEditComment {
19 private readonly executor: RouteExecutor,
20 private publicContextHeaders: HttpHeaders | undefined,
25 entitlements: PublicDocumentEntitlements | DocumentEntitlements,
26 ): Promise<ApiResult<EditCommentResponse>> {
27 if (isPublicDocumentEntitlements(entitlements)) {
28 return this.executePublic(dto, entitlements)
30 return this.executePrivate(dto, entitlements)
34 private async executePrivate(
36 entitlements: DocumentEntitlements,
37 ): Promise<ApiResult<EditCommentResponse>> {
38 const { volumeId, linkId } = entitlements.nodeMeta
39 const { threadId, commentId, encryptedContent } = dto
40 const authorEmail = entitlements.keys.userOwnAddress
42 const route = new DocsApiPrivateRouteBuilder({ volumeId, linkId }).editComment({
46 Content: encryptedContent,
47 AuthorEmail: authorEmail,
51 return this.executor.execute(route)
54 private async executePublic(
56 entitlements: PublicDocumentEntitlements,
57 ): Promise<ApiResult<EditCommentResponse>> {
58 if (!this.publicContextHeaders) {
59 throw new Error('Public context headers not set')
62 const { token, linkId } = entitlements.nodeMeta
64 const route = new DocsApiPublicRouteBuilder({
67 headers: this.publicContextHeaders,
69 threadId: dto.threadId,
70 commentId: dto.commentId,
72 Content: dto.encryptedContent,
76 return this.executor.execute(route)