Skip to content

Let's use AI to Earn!

feat(publish): youtube publish add parameter verification (#398)

nevin499c45e19b45
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/dto/publish.dto.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/dto/publish.dto.ts
index b9fce5b8..66c8465f 100644
--- a/project/backend/apps/aitoearn-channel/src/core/publishing/dto/publish.dto.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/dto/publish.dto.ts
@@ -67,7 +67,7 @@ export const YouTubePublishOptionSchema = z.object({
YouTubePrivacyStatus.Public,
YouTubePrivacyStatus.Unlisted,
YouTubePrivacyStatus.Private,
- ]),
+ ]).optional().default(YouTubePrivacyStatus.Public),
license: z.enum(YouTubeLicense).optional(),
categoryId: z.string(),
notifySubscribers: z.boolean().optional().default(false),
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/providers/base.service.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/providers/base.service.ts
index 951f0a09..69a4152f 100644
--- a/project/backend/apps/aitoearn-channel/src/core/publishing/providers/base.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/providers/base.service.ts
@@ -1,6 +1,5 @@
import { Inject, Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
-
import { QueueService } from '@yikart/aitoearn-queue'
import { PublishRecord } from '@yikart/aitoearn-server-client'
import { Model } from 'mongoose'
@@ -10,6 +9,7 @@ import {
PublishTask,
} from '../../../libs/database/schema/publishTask.schema'
import { PublishRecordService } from '../../account/publish-record.service'
+import { CreatePublishDto } from '../dto/publish.dto'
import { MediaStagingService } from '../media-staging.service'
import { PublishingException } from '../publishing.exception'
import { MediaProcessingStatus, MediaProcessingStatusResult, PublishingTaskResult } from '../publishing.interface'
@@ -260,4 +260,20 @@ export abstract class PublishService {
{ status, errorMsg: errMsg },
).exec()
}
+
+ async validatePublishParams(publishTask: CreatePublishDto): Promise<{
+ success: boolean
+ message?: string
+ }> {
+ if (!publishTask.accountType) {
+ return {
+ success: false,
+ message: 'Account type is required',
+ }
+ }
+ return {
+ success: true,
+ message: 'Publish params are valid',
+ }
+ }
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/providers/youtube.service.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/providers/youtube.service.ts
index c975a7ba..f50424fb 100644
--- a/project/backend/apps/aitoearn-channel/src/core/publishing/providers/youtube.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/providers/youtube.service.ts
@@ -1,15 +1,35 @@
import { Injectable, Logger } from '@nestjs/common'
import { youtube_v3 } from 'googleapis'
+import { z } from 'zod'
import { chunkedDownloadFile, getFileSizeFromUrl } from '../../../common'
import {
PublishStatus,
PublishTask,
} from '../../../libs/database/schema/publishTask.schema'
import { YoutubeService } from '../../platforms/youtube/youtube.service'
+import { CreatePublishDto } from '../dto/publish.dto'
import { PublishingException } from '../publishing.exception'
import { PublishingTaskResult } from '../publishing.interface'
import { PublishService } from './base.service'
+// YouTube 发布参数验证 Schema
+const youtubePublishSchema = z.object({
+ title: z.string()
+ .min(1, 'Title is required')
+ .max(100, 'Title must be 100 characters or less'),
+ desc: z.string()
+ .min(1, 'Description is required')
+ .max(5000, 'Description must be 5000 characters or less'),
+ videoUrl: z.string()
+ .min(1, 'Video URL is required')
+ .url('Video URL must be a valid URL'),
+ option: z.object({
+ youtube: z.object({
+ categoryId: z.string().min(1, 'Category is required'),
+ }).passthrough(),
+ }).passthrough(),
+}).passthrough()
+
@Injectable()
export class YoutubePubService extends PublishService {
private readonly logger = new Logger(YoutubePubService.name)
@@ -34,7 +54,7 @@ export class YoutubePubService extends PublishService {
publishTask.topics,
publishTask?.option?.youtube?.license || 'youtube',
publishTask?.option?.youtube?.categoryId || '22',
- publishTask?.option?.youtube?.privacyStatus || 'private',
+ publishTask?.option?.youtube?.privacyStatus || 'public',
publishTask?.option?.youtube?.notifySubscribers || false,
publishTask?.option?.youtube?.embeddable || false,
publishTask?.option?.youtube?.selfDeclaredMadeForKids || false,
@@ -104,4 +124,26 @@ export class YoutubePubService extends PublishService {
status: PublishStatus.PUBLISHED,
}
}
+
+ override async validatePublishParams(publishTask: CreatePublishDto): Promise<{
+ success: boolean
+ message?: string
+ }> {
+ const result = youtubePublishSchema.safeParse(publishTask)
+
+ if (result.success) {
+ return {
+ success: true,
+ message: 'Publish params are valid',
+ }
+ }
+
+ // 返回第一个验证错误
+ const errors = result.error.issues
+ const { message, path } = errors[0]
+ return {
+ success: false,
+ message: message ? `${message} ${path.join('.')}` : 'Validation failed',
+ }
+ }
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/publishing.service.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/publishing.service.ts
index d27ece64..96eb7e04 100644
--- a/project/backend/apps/aitoearn-channel/src/core/publishing/publishing.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/publishing.service.ts
@@ -1,4 +1,4 @@
-import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common'
+import { Inject, Injectable, Logger, OnModuleDestroy } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { QueueService } from '@yikart/aitoearn-queue'
import { AccountType, PublishRecord, PublishStatus } from '@yikart/aitoearn-server-client'
@@ -14,6 +14,7 @@ import { YoutubeService } from '../platforms/youtube/youtube.service'
import { IMMEDIATE_PUBLISH_TOLERANCE_SECONDS } from './constant'
import { CreatePublishDto, PublishRecordListFilterDto, UpdatePublishTaskDto } from './dto/publish.dto'
import { TiktokWebhookDto } from './dto/tiktok-webhook.dto'
+import { PublishService } from './providers/base.service'
import { TiktokPubService } from './providers/tiktok.service'
import { generatePostMessage } from './util'
@@ -30,6 +31,8 @@ export class PublishingService implements OnModuleDestroy {
private readonly publishRecordService: PublishRecordService,
private readonly youtubeService: YoutubeService,
private readonly facebookService: FacebookService,
+ @Inject('PUBLISHING_PROVIDERS')
+ private readonly publishingProviders: Record<AccountType, PublishService>,
) {}
private determineMetaPostCategory(data: CreatePublishDto) {
@@ -65,6 +68,11 @@ export class PublishingService implements OnModuleDestroy {
}
async createPublishingTask(taskInfo: CreatePublishDto) {
+ const validateResult = await this.publishingProviders[taskInfo.accountType].validatePublishParams(taskInfo)
+ if (!validateResult.success) {
+ throw new AppException(ResponseCode.PublishTaskInvalid, validateResult.message)
+ }
+
const isTaskExists = await this.publishTaskModel.findOne({
flowId: taskInfo.flowId,
})
@@ -342,6 +350,7 @@ export class PublishingService implements OnModuleDestroy {
publishTime?: Date
queued?: boolean
inQueue?: boolean
+ // errorData?: PublishErrorData
},
): Promise<boolean> {
const res = await this.publishTaskModel.updateOne({ _id: id }, newData)
diff --git a/project/backend/libs/common/src/enums/response-code.enum.ts b/project/backend/libs/common/src/enums/response-code.enum.ts
index 2b61e723..9d47c4f2 100755
--- a/project/backend/libs/common/src/enums/response-code.enum.ts
+++ b/project/backend/libs/common/src/enums/response-code.enum.ts
@@ -102,6 +102,7 @@ export enum ResponseCode {
PlatformNotSupported = 15032,
PublishTaskUpdateFailed = 15033,
DeletePostFailed = 15034,
+ PublishTaskInvalid = 15035,
// 16000-16099: aitoearn-mcp(MCP 服务)
ApiKeyNotFound = 16000,
diff --git a/project/backend/libs/common/src/i18n/messages.ts b/project/backend/libs/common/src/i18n/messages.ts
index 108bdbd6..1f64afe2 100644
--- a/project/backend/libs/common/src/i18n/messages.ts
+++ b/project/backend/libs/common/src/i18n/messages.ts
@@ -310,6 +310,10 @@ export const messages: Record<ResponseCode, Record<Locale, MessageValue>> = {
'en-US': 'Failed to delete post',
'zh-CN': '删除作品失败',
},
+ [ResponseCode.PublishTaskInvalid]: {
+ 'en-US': 'Publish task invalid',
+ 'zh-CN': '发布任务无效',
+ },
[ResponseCode.ApiKeyNotFound]: {
'en-US': 'API key not found',
'zh-CN': 'API 密钥未找到',
diff --git a/project/backend/libs/mongodb/src/schemas/publishRecord.schema.ts b/project/backend/libs/mongodb/src/schemas/publishRecord.schema.ts
index 6ae4c5fa..82cb78b9 100644
--- a/project/backend/libs/mongodb/src/schemas/publishRecord.schema.ts
+++ b/project/backend/libs/mongodb/src/schemas/publishRecord.schema.ts
@@ -9,6 +9,7 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { AccountType } from '@yikart/common'
import mongoose from 'mongoose'
import { PublishStatus, PublishType } from '../enums'
+import { PublishErrorData } from './publishing-task-meta.schema'
import { WithTimestampSchema } from './timestamp.schema'
@Schema({
@@ -119,33 +120,28 @@ export class PublishRecord extends WithTimestampSchema {
})
status: PublishStatus
- // 错误信息
+ @Prop({
+ required: false,
+ type: PublishErrorData,
+ })
+ errorData?: PublishErrorData
+
@Prop({
required: false,
})
errorMsg?: string
- // 队列 ID
@Prop({
required: false,
})
queueId?: string
- // 此任务是否进入队列
@Prop({
required: true,
default: false,
})
inQueue: boolean
- /**
- * 任意对象值
- * bilibili: {
- * tid: number; 分区
- * copyright: number; 1-原创,2-转载(转载时source必填)
- * source: string; 如果copyright为转载,则此字段表示转载来源;
- * }
- */
@Prop({
required: false,
type: mongoose.Schema.Types.Mixed,
@@ -158,15 +154,14 @@ export class PublishRecord extends WithTimestampSchema {
type: String,
default: '',
})
- dataId: string // 微信公众号-publish_id
+ dataId: string
@Prop({
required: false,
type: String,
})
- workLink?: string // 作品链接
+ workLink?: string
- // 数据补充内容
@Prop({
required: false,
type: mongoose.Schema.Types.Mixed,
diff --git a/project/backend/libs/mongodb/src/schemas/publishing-task-meta.schema.ts b/project/backend/libs/mongodb/src/schemas/publishing-task-meta.schema.ts
index 73436401..0afb9566 100644
--- a/project/backend/libs/mongodb/src/schemas/publishing-task-meta.schema.ts
+++ b/project/backend/libs/mongodb/src/schemas/publishing-task-meta.schema.ts
@@ -122,3 +122,18 @@ export class PublishingTaskMeta {
@Prop({ required: false })
wxGzh?: WxGzhPublishTaskMeta
}
+
+@Schema({})
+export class PublishErrorData {
+ @Prop({ required: true })
+ type: string
+
+ @Prop({ required: true })
+ code: string
+
+ @Prop({ required: true, default: '' })
+ message: string
+
+ @Prop({ required: false })
+ originalData?: any
+}
diff --git a/project/backend/libs/mongodb/src/schemas/publishing-task.schema.ts b/project/backend/libs/mongodb/src/schemas/publishing-task.schema.ts
index 4882761b..2b410754 100644
--- a/project/backend/libs/mongodb/src/schemas/publishing-task.schema.ts
+++ b/project/backend/libs/mongodb/src/schemas/publishing-task.schema.ts
@@ -1,7 +1,7 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { AccountType } from '@yikart/common'
import { PublishingTaskType, PublishStatus } from '../enums'
-import { PublishingTaskMeta } from './publishing-task-meta.schema'
+import { PublishErrorData, PublishingTaskMeta } from './publishing-task-meta.schema'
import { WithTimestampSchema } from './timestamp.schema'
@Schema({
@@ -128,6 +128,12 @@ export class PublishTask extends WithTimestampSchema {
})
queued: boolean
+ @Prop({
+ required: false,
+ type: PublishErrorData,
+ })
+ errorData?: PublishErrorData
+
@Prop({
required: false,
})