| 1 | import type { DynamicModule } from '@nestjs/common' |
| 2 | import type { PlatformConfigWithStatus } from '../platforms.config' |
| 3 | import { Module } from '@nestjs/common' |
| 4 | import { isAvailablePlatformConfig, isVisiblePlatformConfig } from '../platforms.config' |
| 5 | import { PlatformIntegrationRegistry } from '../platforms.registry' |
| 6 | import { PlatformRegistryModule } from '../platforms.registry.module' |
| 7 | import { RedNoteOfflineQrController } from './offline-qr/rednote-offline-qr.controller' |
| 8 | import { RedNoteOfflineQrService } from './offline-qr/rednote-offline-qr.service' |
| 9 | import { RedNotePublishProvider } from './rednote-publish.provider' |
| 10 | import { RedNoteWorkProvider } from './rednote-work.provider' |
| 11 | import { RednoteConfig } from './rednote.config' |
| 12 | import { REDNOTE_METADATA } from './rednote.constants' |
| 13 | |
| 14 | @Module({}) |
| 15 | export class RedNoteModule { |
| 16 | static forRoot(config: PlatformConfigWithStatus | undefined): DynamicModule { |
| 17 | if (!isVisiblePlatformConfig(config)) { |
| 18 | return { module: RedNoteModule } |
| 19 | } |
| 20 | |
| 21 | const metadata = { ...REDNOTE_METADATA, logoUrl: config.logoUrl } |
| 22 | |
| 23 | if (!isAvailablePlatformConfig(config)) { |
| 24 | return { |
| 25 | module: RedNoteModule, |
| 26 | imports: [PlatformRegistryModule], |
| 27 | providers: [ |
| 28 | { |
| 29 | provide: 'REDNOTE_INTEGRATION', |
| 30 | inject: [PlatformIntegrationRegistry], |
| 31 | useFactory: (registry: PlatformIntegrationRegistry) => { |
| 32 | registry.register({ |
| 33 | platform: REDNOTE_METADATA.platform, |
| 34 | status: config.status, |
| 35 | metadata, |
| 36 | runtime: {}, |
| 37 | }) |
| 38 | }, |
| 39 | }, |
| 40 | ], |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | const availableConfig = config as RednoteConfig |
| 45 | |
| 46 | return { |
| 47 | module: RedNoteModule, |
| 48 | imports: [PlatformRegistryModule], |
| 49 | controllers: [RedNoteOfflineQrController], |
| 50 | providers: [ |
| 51 | { provide: RednoteConfig, useValue: availableConfig }, |
| 52 | RedNoteOfflineQrService, |
| 53 | RedNotePublishProvider, |
| 54 | RedNoteWorkProvider, |
| 55 | { |
| 56 | provide: 'REDNOTE_INTEGRATION', |
| 57 | inject: [PlatformIntegrationRegistry, RedNotePublishProvider, RedNoteWorkProvider], |
| 58 | useFactory: ( |
| 59 | registry: PlatformIntegrationRegistry, |
| 60 | publish: RedNotePublishProvider, |
| 61 | work: RedNoteWorkProvider, |
| 62 | ) => { |
| 63 | registry.register({ |
| 64 | platform: REDNOTE_METADATA.platform, |
| 65 | status: availableConfig.status, |
| 66 | metadata, |
| 67 | runtime: {}, |
| 68 | publish, |
| 69 | work, |
| 70 | }) |
| 71 | }, |
| 72 | }, |
| 73 | ], |
| 74 | exports: [RedNoteOfflineQrService, RedNotePublishProvider, RedNoteWorkProvider], |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 |