| 1 | import type { |
| 2 | CallChannelEngagementFunctionParams, |
| 3 | ChannelPlatformParams, |
| 4 | ChannelPublishTaskParams, |
| 5 | ChannelWorkParams, |
| 6 | CreateChannelPublishFlowParams, |
| 7 | GetChannelAccountAnalyticsParams, |
| 8 | GetChannelPublishRecordByFlowIdParams, |
| 9 | GetChannelPublishRecordByRecordIdParams, |
| 10 | GetChannelPublishRecordByTaskIdParams, |
| 11 | GetChannelWorkLinkInfoParams, |
| 12 | ListBilibiliChannelPlatformCategoriesParams, |
| 13 | ListChannelEngagementCommentsParams, |
| 14 | ListChannelPublishRecordsParams, |
| 15 | ListChannelWorksParams, |
| 16 | ListYoutubeChannelPlatformCategoriesParams, |
| 17 | RequestChannelPublishUpdateParams, |
| 18 | SubmitChannelEngagementCommentParams, |
| 19 | UpdateChannelPublishAtParams, |
| 20 | VerifyChannelWorkOwnershipParams, |
| 21 | } from './channels-mcp.schema' |
| 22 | import { Injectable } from '@nestjs/common' |
| 23 | import { AppException, ResponseCode } from '@yikart/common' |
| 24 | import { PublishRecordSource } from '@yikart/mongodb' |
| 25 | import { AnalyticsService } from '../analytics/analytics.service' |
| 26 | import { EngagementService } from '../engagement/engagement.service' |
| 27 | import { PlatformIntegrationRegistry } from '../platforms/platforms.registry' |
| 28 | import { PlatformsService } from '../platforms/platforms.service' |
| 29 | import { PublishFlowService } from '../publish/flows/publish-flow.service' |
| 30 | import { PublishRecordReadService } from '../publish/records/publish-record-read.service' |
| 31 | import { PublishTaskService } from '../publish/tasks/publish-task.service' |
| 32 | import { WorkService } from '../works/work.service' |
| 33 | |
| 34 | @Injectable() |
| 35 | export class ChannelsMcpService { |
| 36 | constructor( |
| 37 | private readonly publishFlowService: PublishFlowService, |
| 38 | private readonly publishTaskService: PublishTaskService, |
| 39 | private readonly recordReadService: PublishRecordReadService, |
| 40 | private readonly analyticsService: AnalyticsService, |
| 41 | private readonly engagementService: EngagementService, |
| 42 | private readonly workService: WorkService, |
| 43 | private readonly registry: PlatformIntegrationRegistry, |
| 44 | private readonly platformsService: PlatformsService, |
| 45 | ) {} |
| 46 | |
| 47 | async createChannelPublishFlow(userId: string, params: CreateChannelPublishFlowParams) { |
| 48 | return this.publishFlowService.createFlow(userId, { |
| 49 | ...params, |
| 50 | context: { |
| 51 | ...params.context, |
| 52 | source: PublishRecordSource.Mcp, |
| 53 | }, |
| 54 | }) |
| 55 | } |
| 56 | |
| 57 | async listChannelPublishRecords(userId: string, params: ListChannelPublishRecordsParams) { |
| 58 | return this.recordReadService.listByUserId(userId, params) |
| 59 | } |
| 60 | |
| 61 | async getChannelPublishRecordByRecordId(userId: string, params: GetChannelPublishRecordByRecordIdParams) { |
| 62 | const record = await this.recordReadService.getDetail(params.recordId, userId) |
| 63 | if (!record) { |
| 64 | throw new AppException(ResponseCode.PublishRecordNotFound) |
| 65 | } |
| 66 | return record |
| 67 | } |
| 68 | |
| 69 | async getChannelPublishRecordByTaskId(userId: string, params: GetChannelPublishRecordByTaskIdParams) { |
| 70 | return this.recordReadService.getByTaskId(userId, params.taskId) |
| 71 | } |
| 72 | |
| 73 | async getChannelPublishRecordByFlowId(userId: string, params: GetChannelPublishRecordByFlowIdParams) { |
| 74 | return this.recordReadService.getByFlowId(userId, params.flowId) |
| 75 | } |
| 76 | |
| 77 | async publishChannelTaskNow(userId: string, params: ChannelPublishTaskParams) { |
| 78 | await this.publishTaskService.publishNow(userId, params.taskId) |
| 79 | return { taskId: params.taskId } |
| 80 | } |
| 81 | |
| 82 | async cancelChannelPublishTask(userId: string, params: ChannelPublishTaskParams) { |
| 83 | await this.publishTaskService.cancelTask(userId, params.taskId) |
| 84 | return { taskId: params.taskId } |
| 85 | } |
| 86 | |
| 87 | async updateChannelPublishAt(userId: string, params: UpdateChannelPublishAtParams) { |
| 88 | await this.publishTaskService.updatePublishAt(userId, params.taskId, params.publishAt) |
| 89 | return { taskId: params.taskId, publishAt: params.publishAt } |
| 90 | } |
| 91 | |
| 92 | async requestChannelPublishUpdate(userId: string, params: RequestChannelPublishUpdateParams) { |
| 93 | await this.publishTaskService.requestUpdate(userId, params.taskId, params.data) |
| 94 | return { taskId: params.taskId } |
| 95 | } |
| 96 | |
| 97 | async listChannelPlatforms() { |
| 98 | return this.registry.listMetadata() |
| 99 | } |
| 100 | |
| 101 | async getChannelPlatform(params: ChannelPlatformParams) { |
| 102 | const platform = this.registry.listMetadata() |
| 103 | .find(item => item.platform === params.platform) |
| 104 | if (!platform) { |
| 105 | throw new AppException(ResponseCode.PlatformNotSupported, { platform: params.platform }) |
| 106 | } |
| 107 | return platform |
| 108 | } |
| 109 | |
| 110 | async listBilibiliChannelPlatformCategories(userId: string, params: ListBilibiliChannelPlatformCategoriesParams) { |
| 111 | return this.platformsService.getAccountValues( |
| 112 | userId, |
| 113 | params.accountId, |
| 114 | 'tid', |
| 115 | ) |
| 116 | } |
| 117 | |
| 118 | async listYoutubeChannelPlatformCategories(userId: string, params: ListYoutubeChannelPlatformCategoriesParams) { |
| 119 | return this.platformsService.getAccountValues( |
| 120 | userId, |
| 121 | params.accountId, |
| 122 | 'categoryId', |
| 123 | { |
| 124 | id: params.id, |
| 125 | regionCode: params.regionCode, |
| 126 | }, |
| 127 | ) |
| 128 | } |
| 129 | |
| 130 | async getChannelWorkLinkInfo(userId: string, params: GetChannelWorkLinkInfoParams) { |
| 131 | return this.workService.getLinkInfo(userId, params.platform, params.link, params.accountId) |
| 132 | } |
| 133 | |
| 134 | async listChannelWorks(userId: string, params: ListChannelWorksParams) { |
| 135 | return this.workService.listWorks(userId, params.platform, params.accountId, params.pagination) |
| 136 | } |
| 137 | |
| 138 | async getChannelWorkDetail(userId: string, params: ChannelWorkParams) { |
| 139 | return this.workService.getDetail(userId, params.platform, params.platformWorkId, params.accountId) |
| 140 | } |
| 141 | |
| 142 | async verifyChannelWorkOwnership(userId: string, params: VerifyChannelWorkOwnershipParams) { |
| 143 | return this.workService.verifyOwnership(userId, params.platform, params.platformWorkId, params.candidateAccountId) |
| 144 | } |
| 145 | |
| 146 | async getChannelAccountAnalytics(userId: string, params: GetChannelAccountAnalyticsParams) { |
| 147 | return this.analyticsService.fetchAccountAnalytics(userId, params.accountId, { |
| 148 | since: params.since, |
| 149 | until: params.until, |
| 150 | }) |
| 151 | } |
| 152 | |
| 153 | async getChannelWorkAnalytics(userId: string, params: ChannelWorkParams) { |
| 154 | return this.analyticsService.fetchWorkAnalytics(userId, params.platform, params.platformWorkId, params.accountId) |
| 155 | } |
| 156 | |
| 157 | async listChannelEngagementComments(userId: string, params: ListChannelEngagementCommentsParams) { |
| 158 | return this.engagementService.listComments(userId, params.platform, params.platformWorkId, params.accountId, params.pagination) |
| 159 | } |
| 160 | |
| 161 | async submitChannelEngagementComment(userId: string, params: SubmitChannelEngagementCommentParams) { |
| 162 | return this.engagementService.createComment( |
| 163 | userId, |
| 164 | params.accountId, |
| 165 | params.platform, |
| 166 | params.platformWorkId, |
| 167 | params.content, |
| 168 | params.parentCommentId, |
| 169 | ) |
| 170 | } |
| 171 | |
| 172 | async callChannelEngagementFunction(userId: string, params: CallChannelEngagementFunctionParams) { |
| 173 | return this.engagementService.callFunction(userId, params.accountId, { |
| 174 | platform: params.platform, |
| 175 | name: params.name, |
| 176 | data: params.data, |
| 177 | }) |
| 178 | } |
| 179 | } |
| 180 |