Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / docs-core / lib / Application / Dependencies / AppDependencies.ts
blob3c3635de32086dd9fb0e3002b533fed396ff2bd6
1 import type { LoggerInterface } from '@proton/utils/logs'
2 import { Logger } from '@proton/utils/logs'
3 import { DOCS_DEBUG_KEY, DependencyContainer } from '@proton/docs-shared'
4 import { App_TYPES } from './Types'
5 import type { HttpHeaders } from '../../Api/Types/HttpHeaders'
6 import { DocsApi } from '../../Api/DocsApi'
7 import { SquashDocument } from '../../UseCase/SquashDocument'
8 import { EncryptMessage } from '../../UseCase/EncryptMessage'
9 import { DocLoader } from '../../Services/DocumentLoader/DocLoader'
10 import type { InternalEventBusInterface, SyncedEditorState } from '@proton/docs-shared'
11 import { InternalEventBus } from '@proton/docs-shared'
12 import { DecryptMessage } from '../../UseCase/DecryptMessage'
13 import type { Api } from '@proton/shared/lib/interfaces'
14 import { EncryptionService } from '../../Services/Encryption/EncryptionService'
15 import { EncryptComment } from '../../UseCase/EncryptComment'
16 import { DecryptComment } from '../../UseCase/DecryptComment'
17 import { DuplicateDocument } from '../../UseCase/DuplicateDocument'
18 import { CreateNewDocument } from '../../UseCase/CreateNewDocument'
19 import { CreateEmptyDocumentForConversion } from '../../UseCase/CreateEmptyDocumentForConversion'
20 import { GetRealtimeUrlAndToken } from '../../UseCase/CreateRealtimeValetToken'
21 import { GetDocumentMeta } from '../../UseCase/GetDocumentMeta'
22 import { LoadDocument } from '../../UseCase/LoadDocument'
23 import { GetCommitData } from '../../UseCase/GetCommitData'
24 import { SeedInitialCommit } from '../../UseCase/SeedInitialCommit'
25 import { EncryptionContext } from '../../Services/Encryption/EncryptionContext'
26 import { VerifyCommit } from '../../UseCase/VerifyCommit'
27 import { DecryptCommit } from '../../UseCase/DecryptCommit'
28 import { SquashAlgorithm } from '../../UseCase/SquashAlgorithm'
29 import { HandleRealtimeCommentsEvent } from '../../UseCase/HandleRealtimeCommentsEvent'
30 import { CreateComment } from '../../UseCase/CreateComment'
31 import { CreateThread } from '../../UseCase/CreateThread'
32 import { LoadThreads } from '../../UseCase/LoadThreads'
33 import { WebsocketService } from '../../Services/Websockets/WebsocketService'
34 import { VerifyMessages } from '../../UseCase/VerifyMessages'
35 import { LoadCommit } from '../../UseCase/LoadCommit'
36 import { ExportAndDownload } from '../../UseCase/ExportAndDownload'
37 import type { ImageProxyParams } from '../../Api/Types/ImageProxyParams'
38 import { MetricService } from '../../Services/Metrics/MetricService'
39 import { RecentDocumentsService } from '../../Services/RecentDocuments/RecentDocumentsService'
40 import { GetNode } from '../../UseCase/GetNode'
41 import type { DriveCompatWrapper } from '@proton/drive-store/lib/DriveCompatWrapper'
42 import { PublicDocLoader } from '../../Services/DocumentLoader/PublicDocLoader'
43 import type { UnleashClient } from '@proton/unleash'
44 import type { UserState } from '../../State/UserState'
45 import { ApiEditComment } from '../../Api/Requests/ApiEditComment'
46 import { ApiAddCommentToThread } from '../../Api/Requests/ApiAddCommentToThread'
47 import { ApiCreateThread } from '../../Api/Requests/ApiCreateThread'
48 import { ApiGetThread } from '../../Api/Requests/ApiGetThread'
49 import { RouteExecutor } from '../../Api/RouteExecutor'
51 export class AppDependencies extends DependencyContainer {
52   constructor(
53     api: Api,
54     imageProxyParams: ImageProxyParams | undefined,
55     publicContextHeaders: HttpHeaders | undefined,
56     compatWrapper: DriveCompatWrapper,
57     userState: UserState,
58     appVersion: string,
59     unleashClient: UnleashClient,
60     syncedEditorState: SyncedEditorState,
61   ) {
62     super()
64     this.bind(App_TYPES.Logger, () => {
65       return new Logger('proton-docs', DOCS_DEBUG_KEY)
66     })
68     this.bind(App_TYPES.EventBus, () => {
69       return new InternalEventBus()
70     })
72     this.bind(App_TYPES.MetricService, () => {
73       return new MetricService(api)
74     })
76     this.bind(App_TYPES.RealtimeEncryptionService, () => {
77       return new EncryptionService(EncryptionContext.RealtimeMessage, compatWrapper)
78     })
80     this.bind(App_TYPES.CommentsEncryptionService, () => {
81       return new EncryptionService(EncryptionContext.PersistentComment, compatWrapper)
82     })
84     this.bind(App_TYPES.EncryptComment, () => {
85       return new EncryptComment(
86         this.get<EncryptionService<EncryptionContext.PersistentComment>>(App_TYPES.CommentsEncryptionService),
87       )
88     })
90     this.bind(App_TYPES.DecryptComment, () => {
91       return new DecryptComment(
92         this.get<EncryptionService<EncryptionContext.PersistentComment>>(App_TYPES.CommentsEncryptionService),
93       )
94     })
96     this.bind(App_TYPES.RouteExecutor, () => {
97       return new RouteExecutor(api)
98     })
100     this.bind(App_TYPES.DocsApi, () => {
101       return new DocsApi(
102         this.get<RouteExecutor>(App_TYPES.RouteExecutor),
103         publicContextHeaders,
104         imageProxyParams,
105         this.get<ApiCreateThread>(App_TYPES.ApiCreateThread),
106         this.get<ApiAddCommentToThread>(App_TYPES.ApiAddCommentToThread),
107         this.get<ApiGetThread>(App_TYPES.ApiGetThread),
108         this.get<ApiEditComment>(App_TYPES.ApiEditComment),
109       )
110     })
112     this.bind(App_TYPES.ApiEditComment, () => {
113       return new ApiEditComment(this.get<RouteExecutor>(App_TYPES.RouteExecutor), publicContextHeaders)
114     })
116     this.bind(App_TYPES.ApiAddCommentToThread, () => {
117       return new ApiAddCommentToThread(this.get<RouteExecutor>(App_TYPES.RouteExecutor), publicContextHeaders)
118     })
120     this.bind(App_TYPES.ApiCreateThread, () => {
121       return new ApiCreateThread(this.get<RouteExecutor>(App_TYPES.RouteExecutor), publicContextHeaders)
122     })
124     this.bind(App_TYPES.ApiGetThread, () => {
125       return new ApiGetThread(this.get<RouteExecutor>(App_TYPES.RouteExecutor), publicContextHeaders)
126     })
128     this.bind(App_TYPES.EncryptMessage, () => {
129       return new EncryptMessage(
130         this.get<EncryptionService<EncryptionContext.RealtimeMessage>>(App_TYPES.RealtimeEncryptionService),
131       )
132     })
134     this.bind(App_TYPES.DecryptMessage, () => {
135       return new DecryptMessage(
136         this.get<EncryptionService<EncryptionContext.RealtimeMessage>>(App_TYPES.RealtimeEncryptionService),
137       )
138     })
140     this.bind(App_TYPES.DecryptCommit, () => {
141       return new DecryptCommit(this.get<DecryptMessage>(App_TYPES.DecryptMessage))
142     })
144     this.bind(App_TYPES.CreateEmptyDocumentForConversion, () => {
145       if (!compatWrapper.userCompat) {
146         throw new Error('User compat not available')
147       }
148       return new CreateEmptyDocumentForConversion(
149         compatWrapper.userCompat,
150         this.get<GetDocumentMeta>(App_TYPES.GetDocumentMeta),
151       )
152     })
154     this.bind(App_TYPES.SquashAlgorithm, () => {
155       return new SquashAlgorithm(this.get<LoggerInterface>(App_TYPES.Logger))
156     })
158     this.bind(App_TYPES.SquashDocument, () => {
159       return new SquashDocument(
160         this.get<DocsApi>(App_TYPES.DocsApi),
161         this.get<EncryptMessage>(App_TYPES.EncryptMessage),
162         this.get<DecryptCommit>(App_TYPES.DecryptCommit),
163         this.get<VerifyCommit>(App_TYPES.VerifyCommit),
164         this.get<SquashAlgorithm>(App_TYPES.SquashAlgorithm),
165         this.get<LoggerInterface>(App_TYPES.Logger),
166       )
167     })
169     this.bind(App_TYPES.GetCommitData, () => {
170       return new GetCommitData(this.get<DocsApi>(App_TYPES.DocsApi))
171     })
173     this.bind(App_TYPES.VerifyCommit, () => {
174       return new VerifyCommit(this.get<VerifyMessages>(App_TYPES.VerifyMessages))
175     })
177     this.bind(App_TYPES.VerifyMessages, () => {
178       return new VerifyMessages(
179         this.get<EncryptionService<EncryptionContext.RealtimeMessage>>(App_TYPES.RealtimeEncryptionService),
180       )
181     })
183     this.bind(App_TYPES.LoadDocument, () => {
184       return new LoadDocument(
185         compatWrapper,
186         this.get<GetDocumentMeta>(App_TYPES.GetDocumentMeta),
187         this.get<GetNode>(App_TYPES.GetNode),
188         this.get<LoadCommit>(App_TYPES.LoadCommit),
189         this.get<LoggerInterface>(App_TYPES.Logger),
190       )
191     })
193     this.bind(App_TYPES.GetNode, () => {
194       return new GetNode(compatWrapper)
195     })
197     this.bind(App_TYPES.LoadCommit, () => {
198       return new LoadCommit(
199         this.get<GetCommitData>(App_TYPES.GetCommitData),
200         this.get<DecryptCommit>(App_TYPES.DecryptCommit),
201       )
202     })
204     this.bind(App_TYPES.GetDocumentMeta, () => {
205       return new GetDocumentMeta(this.get<DocsApi>(App_TYPES.DocsApi))
206     })
208     this.bind(App_TYPES.DuplicateDocument, () => {
209       if (!compatWrapper.userCompat) {
210         throw new Error('User compat not available')
211       }
212       return new DuplicateDocument(
213         compatWrapper.userCompat,
214         this.get<GetDocumentMeta>(App_TYPES.GetDocumentMeta),
215         this.get<SeedInitialCommit>(App_TYPES.CreateInitialCommit),
216       )
217     })
219     this.bind(App_TYPES.CreateInitialCommit, () => {
220       return new SeedInitialCommit(
221         this.get<DocsApi>(App_TYPES.DocsApi),
222         this.get<EncryptMessage>(App_TYPES.EncryptMessage),
223       )
224     })
226     this.bind(App_TYPES.CreateNewDocument, () => {
227       if (!compatWrapper.userCompat) {
228         throw new Error('User compat not available')
229       }
230       return new CreateNewDocument(compatWrapper.userCompat, this.get<GetDocumentMeta>(App_TYPES.GetDocumentMeta))
231     })
233     this.bind(App_TYPES.CreateRealtimeValetToken, () => {
234       return new GetRealtimeUrlAndToken(this.get<DocsApi>(App_TYPES.DocsApi))
235     })
237     this.bind(App_TYPES.ExportAndDownload, () => {
238       return new ExportAndDownload()
239     })
241     this.bind(App_TYPES.PublicDocLoader, () => {
242       if (!compatWrapper.publicCompat) {
243         throw new Error('Public compat not available')
244       }
246       return new PublicDocLoader(
247         compatWrapper.publicCompat,
248         this.get<WebsocketService>(App_TYPES.WebsocketService),
249         this.get<DocsApi>(App_TYPES.DocsApi),
250         this.get<LoadDocument>(App_TYPES.LoadDocument),
251         this.get<ExportAndDownload>(App_TYPES.ExportAndDownload),
252         this.get<InternalEventBusInterface>(App_TYPES.EventBus),
253         this.get<LoadCommit>(App_TYPES.LoadCommit),
254         this.get<GetDocumentMeta>(App_TYPES.GetDocumentMeta),
255         this.get<LoggerInterface>(App_TYPES.Logger),
256         unleashClient,
257         syncedEditorState,
258         this.get<EncryptComment>(App_TYPES.EncryptComment),
259         this.get<CreateComment>(App_TYPES.CreateComment),
260         this.get<CreateThread>(App_TYPES.CreateThread),
261         this.get<LoadThreads>(App_TYPES.LoadThreads),
262         this.get<HandleRealtimeCommentsEvent>(App_TYPES.HandleRealtimeCommentsEvent),
263         this.get<MetricService>(App_TYPES.MetricService),
264       )
265     })
267     this.bind(App_TYPES.DocLoader, () => {
268       if (!compatWrapper.userCompat) {
269         throw new Error('User compat not available')
270       }
271       return new DocLoader(
272         userState,
273         this.get<WebsocketService>(App_TYPES.WebsocketService),
274         compatWrapper.userCompat,
275         this.get<MetricService>(App_TYPES.MetricService),
276         this.get<DocsApi>(App_TYPES.DocsApi),
277         this.get<SquashDocument>(App_TYPES.SquashDocument),
278         this.get<SeedInitialCommit>(App_TYPES.CreateInitialCommit),
279         this.get<LoadDocument>(App_TYPES.LoadDocument),
280         this.get<LoadCommit>(App_TYPES.LoadCommit),
281         this.get<EncryptComment>(App_TYPES.EncryptComment),
282         this.get<CreateComment>(App_TYPES.CreateComment),
283         this.get<CreateThread>(App_TYPES.CreateThread),
284         this.get<LoadThreads>(App_TYPES.LoadThreads),
285         this.get<HandleRealtimeCommentsEvent>(App_TYPES.HandleRealtimeCommentsEvent),
286         this.get<DuplicateDocument>(App_TYPES.DuplicateDocument),
287         this.get<CreateNewDocument>(App_TYPES.CreateNewDocument),
288         this.get<GetDocumentMeta>(App_TYPES.GetDocumentMeta),
289         this.get<ExportAndDownload>(App_TYPES.ExportAndDownload),
290         this.get<GetNode>(App_TYPES.GetNode),
291         this.get<InternalEventBusInterface>(App_TYPES.EventBus),
292         this.get<LoggerInterface>(App_TYPES.Logger),
293       )
294     })
296     this.bind(App_TYPES.WebsocketService, () => {
297       return new WebsocketService(
298         userState,
299         this.get<GetRealtimeUrlAndToken>(App_TYPES.CreateRealtimeValetToken),
300         this.get<EncryptMessage>(App_TYPES.EncryptMessage),
301         this.get<DecryptMessage>(App_TYPES.DecryptMessage),
302         this.get<LoggerInterface>(App_TYPES.Logger),
303         this.get<InternalEventBusInterface>(App_TYPES.EventBus),
304         this.get<MetricService>(App_TYPES.MetricService),
305         appVersion,
306       )
307     })
309     this.bind(App_TYPES.CreateComment, () => {
310       return new CreateComment(this.get<DocsApi>(App_TYPES.DocsApi), this.get<EncryptComment>(App_TYPES.EncryptComment))
311     })
313     this.bind(App_TYPES.CreateThread, () => {
314       return new CreateThread(
315         this.get<DocsApi>(App_TYPES.DocsApi),
316         this.get<EncryptComment>(App_TYPES.EncryptComment),
317         this.get<DecryptComment>(App_TYPES.DecryptComment),
318         this.get<InternalEventBusInterface>(App_TYPES.EventBus),
319         this.get<LoggerInterface>(App_TYPES.Logger),
320       )
321     })
323     this.bind(App_TYPES.LoadThreads, () => {
324       return new LoadThreads(
325         this.get<DocsApi>(App_TYPES.DocsApi),
326         this.get<DecryptComment>(App_TYPES.DecryptComment),
327         this.get<LoggerInterface>(App_TYPES.Logger),
328       )
329     })
331     this.bind(App_TYPES.HandleRealtimeCommentsEvent, () => {
332       return new HandleRealtimeCommentsEvent()
333     })
335     this.bind(App_TYPES.RecentDocumentsService, () => {
336       if (!compatWrapper.userCompat) {
337         throw new Error('User compat not available')
338       }
339       return new RecentDocumentsService(
340         this.get<InternalEventBus>(App_TYPES.EventBus),
341         compatWrapper.userCompat,
342         this.get<DocsApi>(App_TYPES.DocsApi),
343         this.get<LoggerInterface>(App_TYPES.Logger),
344       )
345     })
346   }