From 7e8d7f6874bb1db55ee6feb9e128c684a6900189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20S=C3=B3jko?= Date: Fri, 29 Dec 2023 07:55:16 +0100 Subject: [PATCH] fix: cleanup revision requests --- packages/auth/bin/revisions_cleanup.ts | 71 ------------------- .../docker/entrypoint-revisions-cleanup.js | 11 --- packages/auth/docker/entrypoint.sh | 4 -- .../src/Domain/Event/DomainEventFactory.ts | 17 ----- .../Event/DomainEventFactoryInterface.ts | 2 - .../Event/RevisionsCleanupRequestedEvent.ts | 7 -- .../RevisionsCleanupRequestedEventPayload.ts | 3 - packages/domain-events/src/Domain/index.ts | 2 - .../syncing-server/src/Bootstrap/Container.ts | 12 ---- .../syncing-server/src/Bootstrap/Types.ts | 1 - .../RevisionsCleanupRequestedEventHandler.ts | 56 --------------- 11 files changed, 186 deletions(-) delete mode 100644 packages/auth/bin/revisions_cleanup.ts delete mode 100644 packages/auth/docker/entrypoint-revisions-cleanup.js delete mode 100644 packages/domain-events/src/Domain/Event/RevisionsCleanupRequestedEvent.ts delete mode 100644 packages/domain-events/src/Domain/Event/RevisionsCleanupRequestedEventPayload.ts delete mode 100644 packages/syncing-server/src/Domain/Handler/RevisionsCleanupRequestedEventHandler.ts diff --git a/packages/auth/bin/revisions_cleanup.ts b/packages/auth/bin/revisions_cleanup.ts deleted file mode 100644 index bfa72198a..000000000 --- a/packages/auth/bin/revisions_cleanup.ts +++ /dev/null @@ -1,71 +0,0 @@ -import 'reflect-metadata' - -import { Logger } from 'winston' - -import { ContainerConfigLoader } from '../src/Bootstrap/Container' -import TYPES from '../src/Bootstrap/Types' -import { Env } from '../src/Bootstrap/Env' -import { DomainEventPublisherInterface } from '@standardnotes/domain-events' -import { DomainEventFactoryInterface } from '../src/Domain/Event/DomainEventFactoryInterface' -import { UserSubscriptionRepositoryInterface } from '../src/Domain/Subscription/UserSubscriptionRepositoryInterface' -import { SubscriptionPlanName } from '@standardnotes/domain-core' - -const requestCleanup = async ( - userSubscriptionRepository: UserSubscriptionRepositoryInterface, - domainEventFactory: DomainEventFactoryInterface, - domainEventPublisher: DomainEventPublisherInterface, -): Promise => { - const proSubscriptionPlan = SubscriptionPlanName.create(SubscriptionPlanName.NAMES.ProPlan).getValue() - const plusSubscriptionPlan = SubscriptionPlanName.create(SubscriptionPlanName.NAMES.PlusPlan).getValue() - - const totalSubscriptions = await userSubscriptionRepository.countByPlanName([ - proSubscriptionPlan, - plusSubscriptionPlan, - ]) - - const limitPerPage = 100 - const numberOfPages = Math.ceil(totalSubscriptions / limitPerPage) - for (let i = 0; i < numberOfPages; i++) { - const subscriptions = await userSubscriptionRepository.findByPlanName( - [proSubscriptionPlan, plusSubscriptionPlan], - i * limitPerPage, - limitPerPage, - ) - - for (const subscription of subscriptions) { - await domainEventPublisher.publish( - domainEventFactory.createRevisionsCleanupRequestedEvent({ - userUuid: subscription.userUuid, - }), - ) - } - } -} - -const container = new ContainerConfigLoader('worker') -void container.load().then((container) => { - const env: Env = new Env() - env.load() - - const logger: Logger = container.get(TYPES.Auth_Logger) - - logger.info('Starting revisions cleanup triggering...') - - const domainEventFactory = container.get(TYPES.Auth_DomainEventFactory) - const domainEventPublisher = container.get(TYPES.Auth_DomainEventPublisher) - const userSubscriptionRepository = container.get( - TYPES.Auth_UserSubscriptionRepository, - ) - - Promise.resolve(requestCleanup(userSubscriptionRepository, domainEventFactory, domainEventPublisher)) - .then(() => { - logger.info('Finished revisions cleanup triggering') - - process.exit(0) - }) - .catch((error) => { - logger.error(`Failed to trigger revisions cleanup: ${(error as Error).message}`) - - process.exit(1) - }) -}) diff --git a/packages/auth/docker/entrypoint-revisions-cleanup.js b/packages/auth/docker/entrypoint-revisions-cleanup.js deleted file mode 100644 index 282fe2ce6..000000000 --- a/packages/auth/docker/entrypoint-revisions-cleanup.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict' - -const path = require('path') - -const pnp = require(path.normalize(path.resolve(__dirname, '../../..', '.pnp.cjs'))).setup() - -const index = require(path.normalize(path.resolve(__dirname, '../dist/bin/revisions_cleanup.js'))) - -Object.defineProperty(exports, '__esModule', { value: true }) - -exports.default = index diff --git a/packages/auth/docker/entrypoint.sh b/packages/auth/docker/entrypoint.sh index c69d46782..98f81114f 100755 --- a/packages/auth/docker/entrypoint.sh +++ b/packages/auth/docker/entrypoint.sh @@ -44,10 +44,6 @@ case "$COMMAND" in exec node docker/entrypoint-delete-accounts.js $FILE_NAME $MODE ;; - 'revisions-cleanup' ) - exec node docker/entrypoint-revisions-cleanup.js - ;; - * ) echo "[Docker] Unknown command" ;; diff --git a/packages/auth/src/Domain/Event/DomainEventFactory.ts b/packages/auth/src/Domain/Event/DomainEventFactory.ts index 539c8fe7c..f32146be9 100644 --- a/packages/auth/src/Domain/Event/DomainEventFactory.ts +++ b/packages/auth/src/Domain/Event/DomainEventFactory.ts @@ -22,7 +22,6 @@ import { SessionRefreshedEvent, AccountDeletionVerificationRequestedEvent, FileQuotaRecalculationRequestedEvent, - RevisionsCleanupRequestedEvent, } from '@standardnotes/domain-events' import { Predicate, PredicateVerificationResult } from '@standardnotes/predicates' import { TimerInterface } from '@standardnotes/time' @@ -35,22 +34,6 @@ import { KeyParamsData } from '@standardnotes/responses' @injectable() export class DomainEventFactory implements DomainEventFactoryInterface { constructor(@inject(TYPES.Auth_Timer) private timer: TimerInterface) {} - - createRevisionsCleanupRequestedEvent(dto: { userUuid: string }): RevisionsCleanupRequestedEvent { - return { - type: 'REVISIONS_CLEANUP_REQUESTED', - createdAt: this.timer.getUTCDate(), - meta: { - correlation: { - userIdentifier: dto.userUuid, - userIdentifierType: 'uuid', - }, - origin: DomainEventService.Auth, - }, - payload: dto, - } - } - createFileQuotaRecalculationRequestedEvent(dto: { userUuid: string }): FileQuotaRecalculationRequestedEvent { return { type: 'FILE_QUOTA_RECALCULATION_REQUESTED', diff --git a/packages/auth/src/Domain/Event/DomainEventFactoryInterface.ts b/packages/auth/src/Domain/Event/DomainEventFactoryInterface.ts index e8e7a53ee..9e3247439 100644 --- a/packages/auth/src/Domain/Event/DomainEventFactoryInterface.ts +++ b/packages/auth/src/Domain/Event/DomainEventFactoryInterface.ts @@ -20,13 +20,11 @@ import { SessionRefreshedEvent, AccountDeletionVerificationRequestedEvent, FileQuotaRecalculationRequestedEvent, - RevisionsCleanupRequestedEvent, } from '@standardnotes/domain-events' import { InviteeIdentifierType } from '../SharedSubscription/InviteeIdentifierType' import { KeyParamsData } from '@standardnotes/responses' export interface DomainEventFactoryInterface { - createRevisionsCleanupRequestedEvent(dto: { userUuid: string }): RevisionsCleanupRequestedEvent createFileQuotaRecalculationRequestedEvent(dto: { userUuid: string }): FileQuotaRecalculationRequestedEvent createWebSocketMessageRequestedEvent(dto: { userUuid: string; message: JSONString }): WebSocketMessageRequestedEvent createEmailRequestedEvent(dto: { diff --git a/packages/domain-events/src/Domain/Event/RevisionsCleanupRequestedEvent.ts b/packages/domain-events/src/Domain/Event/RevisionsCleanupRequestedEvent.ts deleted file mode 100644 index b8ea81b05..000000000 --- a/packages/domain-events/src/Domain/Event/RevisionsCleanupRequestedEvent.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { DomainEventInterface } from './DomainEventInterface' -import { RevisionsCleanupRequestedEventPayload } from './RevisionsCleanupRequestedEventPayload' - -export interface RevisionsCleanupRequestedEvent extends DomainEventInterface { - type: 'REVISIONS_CLEANUP_REQUESTED' - payload: RevisionsCleanupRequestedEventPayload -} diff --git a/packages/domain-events/src/Domain/Event/RevisionsCleanupRequestedEventPayload.ts b/packages/domain-events/src/Domain/Event/RevisionsCleanupRequestedEventPayload.ts deleted file mode 100644 index 7e7a3773f..000000000 --- a/packages/domain-events/src/Domain/Event/RevisionsCleanupRequestedEventPayload.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface RevisionsCleanupRequestedEventPayload { - userUuid: string -} diff --git a/packages/domain-events/src/Domain/index.ts b/packages/domain-events/src/Domain/index.ts index 42322824e..737f6e2f7 100644 --- a/packages/domain-events/src/Domain/index.ts +++ b/packages/domain-events/src/Domain/index.ts @@ -72,8 +72,6 @@ export * from './Event/PredicateVerifiedEvent' export * from './Event/PredicateVerifiedEventPayload' export * from './Event/RefundProcessedEvent' export * from './Event/RefundProcessedEventPayload' -export * from './Event/RevisionsCleanupRequestedEvent' -export * from './Event/RevisionsCleanupRequestedEventPayload' export * from './Event/RevisionsCopyRequestedEvent' export * from './Event/RevisionsCopyRequestedEventPayload' export * from './Event/SessionCreatedEvent' diff --git a/packages/syncing-server/src/Bootstrap/Container.ts b/packages/syncing-server/src/Bootstrap/Container.ts index 1ee4f3440..c5dc7ec2e 100644 --- a/packages/syncing-server/src/Bootstrap/Container.ts +++ b/packages/syncing-server/src/Bootstrap/Container.ts @@ -162,7 +162,6 @@ import { SyncResponse } from '@standardnotes/grpc' import { SyncResponseGRPCMapper } from '../Mapping/gRPC/SyncResponseGRPCMapper' import { AccountDeletionVerificationRequestedEventHandler } from '../Domain/Handler/AccountDeletionVerificationRequestedEventHandler' import { SendEventToClients } from '../Domain/UseCase/Syncing/SendEventToClients/SendEventToClients' -import { RevisionsCleanupRequestedEventHandler } from '../Domain/Handler/RevisionsCleanupRequestedEventHandler' export class ContainerConfigLoader { private readonly DEFAULT_CONTENT_SIZE_TRANSFER_LIMIT = 10_000_000 @@ -981,16 +980,6 @@ export class ContainerConfigLoader { container.get(TYPES.Sync_Logger), ), ) - container - .bind(TYPES.Sync_RevisionsCleanupRequestedEventHandler) - .toConstantValue( - new RevisionsCleanupRequestedEventHandler( - container.get(TYPES.Sync_SQLItemRepository), - container.get(TYPES.Sync_DomainEventFactory), - container.get(TYPES.Sync_DomainEventPublisher), - container.get(TYPES.Sync_Logger), - ), - ) // Services container.bind(TYPES.Sync_ContentDecoder).toDynamicValue(() => new ContentDecoder()) @@ -1019,7 +1008,6 @@ export class ContainerConfigLoader { 'SHARED_VAULT_REMOVED', container.get(TYPES.Sync_SharedVaultRemovedEventHandler), ], - ['REVISIONS_CLEANUP_REQUESTED', container.get(TYPES.Sync_RevisionsCleanupRequestedEventHandler)], ]) if (!isConfiguredForHomeServer) { container diff --git a/packages/syncing-server/src/Bootstrap/Types.ts b/packages/syncing-server/src/Bootstrap/Types.ts index a11b45e3f..a96d327cc 100644 --- a/packages/syncing-server/src/Bootstrap/Types.ts +++ b/packages/syncing-server/src/Bootstrap/Types.ts @@ -97,7 +97,6 @@ const TYPES = { Sync_SharedVaultFileUploadedEventHandler: Symbol.for('Sync_SharedVaultFileUploadedEventHandler'), Sync_SharedVaultFileMovedEventHandler: Symbol.for('Sync_SharedVaultFileMovedEventHandler'), Sync_SharedVaultRemovedEventHandler: Symbol.for('Sync_SharedVaultRemovedEventHandler'), - Sync_RevisionsCleanupRequestedEventHandler: Symbol.for('Sync_RevisionsCleanupRequestedEventHandler'), // Services Sync_ContentDecoder: Symbol.for('Sync_ContentDecoder'), Sync_DomainEventPublisher: Symbol.for('Sync_DomainEventPublisher'), diff --git a/packages/syncing-server/src/Domain/Handler/RevisionsCleanupRequestedEventHandler.ts b/packages/syncing-server/src/Domain/Handler/RevisionsCleanupRequestedEventHandler.ts deleted file mode 100644 index ce266ae16..000000000 --- a/packages/syncing-server/src/Domain/Handler/RevisionsCleanupRequestedEventHandler.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { - RevisionsCleanupRequestedEvent, - DomainEventHandlerInterface, - DomainEventPublisherInterface, -} from '@standardnotes/domain-events' - -import { Logger } from 'winston' -import { ItemRepositoryInterface } from '../Item/ItemRepositoryInterface' -import { DomainEventFactoryInterface } from '../Event/DomainEventFactoryInterface' - -export class RevisionsCleanupRequestedEventHandler implements DomainEventHandlerInterface { - constructor( - private itemRepository: ItemRepositoryInterface, - private domainEventFactory: DomainEventFactoryInterface, - private domainEventPublisher: DomainEventPublisherInterface, - private logger: Logger, - ) {} - - async handle(event: RevisionsCleanupRequestedEvent): Promise { - const totalDeletedItems = await this.itemRepository.countAll({ - userUuid: event.payload.userUuid, - deleted: true, - }) - - this.logger.info(`Found ${totalDeletedItems} deleted items`, { - userId: event.payload.userUuid, - }) - - const limitPerPage = 100 - const numberOfPages = Math.ceil(totalDeletedItems / limitPerPage) - - for (let i = 0; i < numberOfPages; i++) { - const items = await this.itemRepository.findAll({ - userUuid: event.payload.userUuid, - deleted: true, - offset: i * limitPerPage, - limit: limitPerPage, - sortOrder: 'ASC', - sortBy: 'created_at_timestamp', - }) - - for (const item of items) { - await this.domainEventPublisher.publish( - this.domainEventFactory.createItemDeletedEvent({ - itemUuid: item.id.toString(), - userUuid: item.props.userUuid.value, - }), - ) - } - } - - this.logger.info(`Finished processing ${totalDeletedItems} deleted items`, { - userId: event.payload.userUuid, - }) - } -}