| 1 | import type { AccountType } from '@yikart/common' |
| 2 | import type { Request, Response } from 'express' |
| 3 | import { All, Controller, Get, Param, Req, Res } from '@nestjs/common' |
| 4 | import { ApiTags } from '@nestjs/swagger' |
| 5 | import { Public } from '@yikart/aitoearn-auth' |
| 6 | import { ApiDoc, SkipResponseInterceptor } from '@yikart/common' |
| 7 | import { PlatformIntegrationRegistry } from './platforms.registry' |
| 8 | import { PlatformsService } from './platforms.service' |
| 9 | import { PlatformMetadataVo, PublishOptionSourceVo } from './platforms.vo' |
| 10 | |
| 11 | @ApiTags('Channels/Platforms') |
| 12 | @Controller({ path: '/channels/platforms', version: '2' }) |
| 13 | export class PlatformsController { |
| 14 | constructor( |
| 15 | private readonly registry: PlatformIntegrationRegistry, |
| 16 | private readonly platformsService: PlatformsService, |
| 17 | ) {} |
| 18 | |
| 19 | @ApiDoc({ |
| 20 | summary: '获取所有平台元数据', |
| 21 | description: '返回已注册平台的元数据列表,包括展示名、logo、能力声明等', |
| 22 | response: [PlatformMetadataVo], |
| 23 | }) |
| 24 | @Public() |
| 25 | @Get('/') |
| 26 | listPlatforms() { |
| 27 | return this.registry.listMetadata().map(metadata => PlatformMetadataVo.create(metadata)) |
| 28 | } |
| 29 | |
| 30 | @ApiDoc({ |
| 31 | summary: '获取平台动态发布选项', |
| 32 | description: '返回该平台发布选项字段的动态取值来源', |
| 33 | response: [PublishOptionSourceVo], |
| 34 | }) |
| 35 | @Get('/:platform/publish-options') |
| 36 | listPublishOptions(@Param('platform') platform: AccountType) { |
| 37 | return this.platformsService.listSources(platform) |
| 38 | .map(source => PublishOptionSourceVo.create(source)) |
| 39 | } |
| 40 | |
| 41 | @ApiDoc({ |
| 42 | summary: '统一平台 Webhook 入口', |
| 43 | description: '接收各平台 webhook 请求,由 platform 参数分发给对应平台 provider 自行处理', |
| 44 | }) |
| 45 | @Public() |
| 46 | @SkipResponseInterceptor() |
| 47 | @All('/:platform/webhooks') |
| 48 | async handleWebhook( |
| 49 | @Param('platform') platform: AccountType, |
| 50 | @Req() req: Request, |
| 51 | @Res() res: Response, |
| 52 | ) { |
| 53 | await this.platformsService.dispatchWebhook(platform, req, res) |
| 54 | } |
| 55 | } |
| 56 |