EmailBackupRequestedEventHandler.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { KeyParamsData } from '@standardnotes/responses'
  2. import {
  3. DomainEventHandlerInterface,
  4. DomainEventPublisherInterface,
  5. EmailBackupRequestedEvent,
  6. } from '@standardnotes/domain-events'
  7. import { inject, injectable } from 'inversify'
  8. import { Logger } from 'winston'
  9. import TYPES from '../../Bootstrap/Types'
  10. import { AuthHttpServiceInterface } from '../Auth/AuthHttpServiceInterface'
  11. import { DomainEventFactoryInterface } from '../Event/DomainEventFactoryInterface'
  12. import { ItemBackupServiceInterface } from '../Item/ItemBackupServiceInterface'
  13. import { ItemRepositoryInterface } from '../Item/ItemRepositoryInterface'
  14. import { ItemTransferCalculatorInterface } from '../Item/ItemTransferCalculatorInterface'
  15. import { ItemQuery } from '../Item/ItemQuery'
  16. @injectable()
  17. export class EmailBackupRequestedEventHandler implements DomainEventHandlerInterface {
  18. constructor(
  19. @inject(TYPES.ItemRepository) private itemRepository: ItemRepositoryInterface,
  20. @inject(TYPES.AuthHttpService) private authHttpService: AuthHttpServiceInterface,
  21. @inject(TYPES.ItemBackupService) private itemBackupService: ItemBackupServiceInterface,
  22. @inject(TYPES.DomainEventPublisher) private domainEventPublisher: DomainEventPublisherInterface,
  23. @inject(TYPES.DomainEventFactory) private domainEventFactory: DomainEventFactoryInterface,
  24. @inject(TYPES.EMAIL_ATTACHMENT_MAX_BYTE_SIZE) private emailAttachmentMaxByteSize: number,
  25. @inject(TYPES.ItemTransferCalculator) private itemTransferCalculator: ItemTransferCalculatorInterface,
  26. @inject(TYPES.Logger) private logger: Logger,
  27. ) {}
  28. async handle(event: EmailBackupRequestedEvent): Promise<void> {
  29. let authParams: KeyParamsData
  30. try {
  31. authParams = await this.authHttpService.getUserKeyParams({
  32. uuid: event.payload.userUuid,
  33. authenticated: false,
  34. })
  35. } catch (error) {
  36. this.logger.warn(`Could not get user key params from auth service: ${(error as Error).message}`)
  37. return
  38. }
  39. const itemQuery: ItemQuery = {
  40. userUuid: event.payload.userUuid,
  41. sortBy: 'updated_at_timestamp',
  42. sortOrder: 'ASC',
  43. deleted: false,
  44. }
  45. const itemUuidBundles = await this.itemTransferCalculator.computeItemUuidBundlesToFetch(
  46. itemQuery,
  47. this.emailAttachmentMaxByteSize,
  48. )
  49. let bundleIndex = 1
  50. for (const itemUuidBundle of itemUuidBundles) {
  51. const items = await this.itemRepository.findAll({
  52. uuids: itemUuidBundle,
  53. sortBy: 'updated_at_timestamp',
  54. sortOrder: 'ASC',
  55. })
  56. const backupFileName = await this.itemBackupService.backup(items, authParams)
  57. this.logger.debug(`Data backed up into: ${backupFileName}`)
  58. if (backupFileName.length !== 0) {
  59. this.logger.debug('Publishing EMAIL_BACKUP_ATTACHMENT_CREATED event')
  60. await this.domainEventPublisher.publish(
  61. this.domainEventFactory.createEmailBackupAttachmentCreatedEvent({
  62. backupFileName,
  63. backupFileIndex: bundleIndex++,
  64. backupFilesTotal: itemUuidBundles.length,
  65. email: authParams.identifier as string,
  66. }),
  67. )
  68. }
  69. }
  70. }
  71. }