Skip to content

Let's use AI to Earn!

chore(publishing): improve publishing workflow and platform integrations (#225)

wheat1efe81641a18
diff --git a/project/backend/apps/aitoearn-channel/package.json b/project/backend/apps/aitoearn-channel/package.json
index 7f1e53e4..7ec0f574 100644
--- a/project/backend/apps/aitoearn-channel/package.json
+++ b/project/backend/apps/aitoearn-channel/package.json
@@ -24,6 +24,7 @@
"@rekog/mcp-nest": "^1.6.2",
"@yikart/aitoearn-queue": "workspace:*",
"@yikart/aitoearn-server-client": "workspace:*",
+ "@yikart/aws-s3": "workspace:*",
"@yikart/common": "workspace:*",
"@yikart/redis": "workspace:*",
"ali-oss": "^6.23.0",
@@ -47,6 +48,7 @@
"qs": "^6.14.0",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
+ "tslib": "^2.3.0",
"uuid": "^11.1.0",
"xml2js": "^0.6.2",
"zod": "^4.1.9"
diff --git a/project/backend/apps/aitoearn-channel/src/config.ts b/project/backend/apps/aitoearn-channel/src/config.ts
index 9530035e..1db5acd1 100644
--- a/project/backend/apps/aitoearn-channel/src/config.ts
+++ b/project/backend/apps/aitoearn-channel/src/config.ts
@@ -1,8 +1,8 @@
import { aitoearnServerClientConfigSchema } from '@yikart/aitoearn-server-client'
+import { s3ConfigSchema } from '@yikart/aws-s3'
import { baseConfig, createZodDto, selectConfig } from '@yikart/common'
import { redisConfigSchema } from '@yikart/redis'
import { z } from 'zod'
-import { s3ConfigSchema } from './libs/aws-s3/s3.config'
// MongoDB配置
const mongoConfigSchema = z.object({
diff --git a/project/backend/apps/aitoearn-channel/src/core/account/account.service.ts b/project/backend/apps/aitoearn-channel/src/core/account/account.service.ts
index 91840004..43e2703d 100644
--- a/project/backend/apps/aitoearn-channel/src/core/account/account.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/account/account.service.ts
@@ -1,8 +1,8 @@
import { Injectable, Logger } from '@nestjs/common'
-import { EventEmitter2 } from '@nestjs/event-emitter'
import { InjectModel } from '@nestjs/mongoose'
+import { QueueService } from '@yikart/aitoearn-queue'
import { AccountStatus, AccountType, AitoearnServerClientService, NewAccount } from '@yikart/aitoearn-server-client'
-import { Model, UpdateQuery } from 'mongoose'
+import { Model } from 'mongoose'
import { TableDto } from '../../common/global/dto/table.dto'
import { Account } from '../../libs/database/schema/account.schema'
@@ -13,8 +13,8 @@ export class AccountService {
@InjectModel(Account.name)
private readonly accountModel: Model<Account>,
private readonly serverClient: AitoearnServerClientService,
- private eventEmitter: EventEmitter2,
- ) {}
+ private readonly queueService: QueueService,
+ ) { }
/**
* 创建账户
@@ -29,79 +29,44 @@ export class AccountService {
},
data: NewAccount,
) {
- this.logger.log({
- msg: '-------- ***** --------',
- data: {
- account,
- data,
- },
- })
- // 查询是否已存在相同账户
- const existAccount = await this.accountModel.findOne({
+ this.logger.log(`createAccount: ${JSON.stringify({ account, data })}`)
+ const loginTime = new Date()
+ const accountData = { userId, ...data, loginTime }
+ const _id = `${account.type}_${account.uid}`
+ await this.accountModel.updateOne({
type: account.type,
uid: account.uid,
- })
- this.logger.log({
- msg: '-------- 00000 --------',
- data: existAccount,
- })
- let newOrUpdatedAccount: Account | null
-
- const newData: UpdateQuery<Account> = { ...data, ...account, userId, loginTime: new Date() }
- if (existAccount) {
- // 已存在,执行更新
- newOrUpdatedAccount = await this.accountModel.findOneAndUpdate(
- { type: account.type, uid: account.uid },
- newData,
- )
-
- this.logger.log({
- msg: '-------- 111111 --------',
- data: newOrUpdatedAccount,
- })
- }
- else {
- // 不存在,创建新账户
- newData['_id'] = `${account.type}_${account.uid}`
- newOrUpdatedAccount = await this.accountModel.create(newData)
-
- this.logger.log({
- msg: '-------- 222222 --------',
- data: newOrUpdatedAccount,
- })
- }
+ }, {
+ _id,
+ ...accountData,
+ }, { upsert: true }).exec()
try {
- const ret = await this.serverClient.account.createAccount(newData)
- this.logger.log({
- msg: '-------- 333333 --------',
- data: ret,
- })
- // 触发账户创建或更新事件
- // this.eventEmitter.emit(`account.create.${newOrUpdatedAccount?.type}`, newOrUpdatedAccount?.id)
+ const result = await this.serverClient.account.createAccount(data)
+ this.logger.log(`create server account success: ${JSON.stringify(result)}`)
+ this.queueService.addDumpSocialMediaAvatarJob({ accountId: result.id })
+ return await this.accountModel.findById(result.id)
}
catch (error) {
- this.logger.error(error)
+ this.logger.error(`create server account error: ${error}`)
return null
}
- this.logger.log({
- msg: '-------- 44444 --------',
- data: newOrUpdatedAccount,
- })
- return newOrUpdatedAccount
}
/**
* 更新账户
* @returns
*/
- async upAccount(accountId: string, data: NewAccount) {
+ async updateAccountInfo(userId: string, accountId: string, data: NewAccount) {
const res = await this.accountModel.updateOne({ _id: accountId }, data)
try {
await this.serverClient.account.updateAccountInfo(
accountId,
- data,
+ {
+ userId,
+ ...data,
+ },
)
}
catch (error) {
diff --git a/project/backend/apps/aitoearn-channel/src/core/engagement/engagement.controller.ts b/project/backend/apps/aitoearn-channel/src/core/engagement/engagement.controller.ts
index cc5c0c5f..80ab8a32 100644
--- a/project/backend/apps/aitoearn-channel/src/core/engagement/engagement.controller.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/engagement/engagement.controller.ts
@@ -1,74 +1,56 @@
import { Body, Controller, Post } from '@nestjs/common'
-import { ApiTags } from '@nestjs/swagger'
-import { ApiDoc } from '@yikart/common'
import { AIGenCommentDto, FetchCommentRepliesRequest, FetchPostCommentsRequest, FetchPostsRequest, PublishCommentReplyRequest, PublishCommentRequest, ReplyToCommentsDto } from './engagement.dto'
import { PublishCommentResponse } from './engagement.interface'
import { EngagementService } from './engagement.service'
-@ApiTags('OpenSource/Core/Engagement/Engagement')
@Controller()
export class EngagementController {
constructor(
private readonly engagementService: EngagementService,
) {}
- @ApiDoc({
- summary: 'List User Posts',
- })
@Post('channel/engagement/list/user/posts')
async fetchUserPosts(@Body() data: FetchPostsRequest) {
return this.engagementService.fetchUserPosts(data)
}
+ @Post('channel/engagement/meta/post/detail')
+ async getMetaPostDetail(@Body() data: { accountId: string, platform: string, postId: string }) {
+ const res = await this.engagementService.getMetaPostDetail(data.accountId, data.platform, data.postId)
+ return res
+ }
+
// @NatsMessagePattern('channel.engagement.list.post.comments')
- @ApiDoc({
- summary: 'List Post Comments',
- })
@Post('channel/engagement/list/post/comments')
async fetchPostComments(@Body() data: FetchPostCommentsRequest) {
return this.engagementService.fetchPostComments(data)
}
// @NatsMessagePattern('channel.engagement.list.comment.replies')
- @ApiDoc({
- summary: 'List Comment Replies',
- })
@Post('channel/engagement/list/comment/replies')
async fetchCommentReplies(@Body() data: FetchCommentRepliesRequest) {
return this.engagementService.fetchCommentReplies(data)
}
// @NatsMessagePattern('channel.engagement.publish.post.comment')
- @ApiDoc({
- summary: 'Publish Post Comment',
- })
@Post('channel/engagement/publish/post/comment')
async commentOnPost(@Body() data: PublishCommentRequest): Promise<PublishCommentResponse> {
return this.engagementService.commentOnPost(data)
}
// @NatsMessagePattern('channel.engagement.publish.comment.reply')
- @ApiDoc({
- summary: 'Publish Comment Reply',
- })
@Post('channel/engagement/publish/comment/reply')
async replyToComment(@Body() data: PublishCommentReplyRequest): Promise<PublishCommentResponse> {
return this.engagementService.replyToComment(data)
}
// @NatsMessagePattern('channel.engagement.ai.generate.replies')
- @ApiDoc({
- summary: 'Generate Comment Replies with AI',
- })
@Post('channel/engagement/ai/generate/replies')
async generateRepliesByAI(@Body() data: AIGenCommentDto) {
return this.engagementService.batchGenReplyContent(data)
}
// @NatsMessagePattern('channel.engagement.ai.reply.to.comments')
- @ApiDoc({
- summary: 'Reply to Comments with AI',
- })
@Post('channel/engagement/ai/reply/to/comments')
async replyToCommentByAI(@Body() data: ReplyToCommentsDto) {
return this.engagementService.ReplyToCommentsByAI(data)
diff --git a/project/backend/apps/aitoearn-channel/src/core/engagement/engagement.interface.ts b/project/backend/apps/aitoearn-channel/src/core/engagement/engagement.interface.ts
index 0be638dc..331f3c87 100644
--- a/project/backend/apps/aitoearn-channel/src/core/engagement/engagement.interface.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/engagement/engagement.interface.ts
@@ -1,4 +1,4 @@
-import { PostsResponseVo } from '@yikart/common'
+import { PostsResponseVo, PostVo } from '@yikart/common'
import { KeysetPagination, OffsetPagination } from './engagement.dto'
/**
@@ -56,6 +56,20 @@ export interface PublishCommentResponse {
* @template T The type of comment data returned by the platform
*/
export interface EngagementProvider {
+ /**
+ * Fetches meta post detail
+ * @param platform - The platform
+ * @param postId - The post ID
+ * @param userId - The user ID
+ * @returns Promise resolving to posts response
+ */
+ getMetaPostDetail: (accountId: string, postId: string) => Promise<PostVo>
+ /**
+ * Fetches user posts
+ * @param accountId - The account ID
+ * @param pagination - The pagination parameters
+ * @returns Promise resolving to posts response
+ */
fetchUserPosts: (accountId: string, pagination: KeysetPagination | OffsetPagination | null) => Promise<PostsResponseVo>
/**
* Fetches comments on a specific post
diff --git a/project/backend/apps/aitoearn-channel/src/core/engagement/engagement.service.ts b/project/backend/apps/aitoearn-channel/src/core/engagement/engagement.service.ts
index ba88db0b..9ca5fcaa 100644
--- a/project/backend/apps/aitoearn-channel/src/core/engagement/engagement.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/engagement/engagement.service.ts
@@ -51,6 +51,11 @@ export class EngagementService {
return provider.fetchUserPosts(data.accountId, pagination)
}
+ async getMetaPostDetail(accountId: string, platform: string, postId: string) {
+ const provider = this.getProvider(platform)
+ return provider.getMetaPostDetail(accountId, postId)
+ }
+
async fetchPostComments(data: FetchPostCommentsRequest) {
const provider = this.getProvider(data.platform)
return provider.fetchPostComments(data.accountId, data.postId, data.pagination || null)
diff --git a/project/backend/apps/aitoearn-channel/src/core/engagement/providers/facebook.provider.ts b/project/backend/apps/aitoearn-channel/src/core/engagement/providers/facebook.provider.ts
index 3b932460..8bdd34b8 100644
--- a/project/backend/apps/aitoearn-channel/src/core/engagement/providers/facebook.provider.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/engagement/providers/facebook.provider.ts
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common'
-import { PostsResponseVo } from '@yikart/common'
+import { PostsResponseVo, PostVo } from '@yikart/common'
import { FacebookService } from '../../../core/platforms/meta/facebook.service'
import { FacebookPagePostRequest, FacebookPostCommentsRequest } from '../../../libs/facebook/facebook.interfaces'
import { KeysetPagination, OffsetPagination } from '../engagement.dto'
@@ -60,6 +60,39 @@ export class FacebookEngagementProvider implements EngagementProvider {
}
}
+ async getMetaPostDetail(accountId: string, postId: string): Promise<PostVo> {
+ const fields = 'id,message,created_time,attachments,is_published,is_expired,permalink_url,shares,comments.summary(true),likes.summary(true),insights.metric(post_impressions,post_clicks,post_reactions_like_total,post_video_views).period(lifetime)'
+ const postDetail = await this.FacebookService.getPostDetail(accountId, postId, fields)
+ const medias = []
+ if (postDetail.attachments?.data) {
+ for (const attachment of postDetail.attachments.data) {
+ if (attachment.media?.image?.src) {
+ medias.push({
+ url: attachment.media.source || attachment.media.image.src,
+ type: attachment.type === 'video_autoplay' || attachment.type === 'video_inline' ? 'video' : 'image',
+ thumbnail: attachment.media.image.src,
+ })
+ }
+ }
+ }
+ return {
+ id: postDetail.id,
+ platform: 'facebook',
+ title: '',
+ content: postDetail.message || '',
+ medias,
+ permalink: postDetail.permalink_url || '',
+ publishTime: new Date(postDetail.created_time).getTime(),
+ viewCount: postDetail.insights?.data?.find(d => d.name === 'post_video_views')?.values[0]?.value || 0,
+ commentCount: postDetail.comments?.summary?.total_count || 0,
+ likeCount: postDetail.likes?.summary?.total_count || 0,
+ shareCount: postDetail.shares?.count || 0,
+ clickCount: postDetail.insights?.data?.find(d => d.name === 'post_clicks')?.values[0]?.value || 0,
+ impressionCount: postDetail.insights?.data?.find(d => d.name === 'post_impressions')?.values[0]?.value || 0,
+ favoriteCount: 0,
+ }
+ }
+
private async fetchFacebookObjectComments(accountId: string, targetId: string, pagination: KeysetPagination | OffsetPagination | null): Promise<FetchPostCommentsResponse> {
const query: FacebookPostCommentsRequest = {
filter: 'toplevel',
diff --git a/project/backend/apps/aitoearn-channel/src/core/engagement/providers/instagram.provider.ts b/project/backend/apps/aitoearn-channel/src/core/engagement/providers/instagram.provider.ts
index 9203b87f..68bf8253 100644
--- a/project/backend/apps/aitoearn-channel/src/core/engagement/providers/instagram.provider.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/engagement/providers/instagram.provider.ts
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common'
-import { PostsResponseVo } from '@yikart/common'
+import { PostsResponseVo, PostVo } from '@yikart/common'
import { InstagramService } from '../../../core/platforms/meta/instagram.service'
import { InstagramMediaType } from '../../../libs/instagram/instagram.enum'
import { IGPostCommentsRequest, InstagramUserPostRequest } from '../../../libs/instagram/instagram.interfaces'
@@ -14,7 +14,7 @@ export class InstagramEngagementProvider implements EngagementProvider {
async fetchUserPosts(accountId: string, pagination: KeysetPagination | OffsetPagination | null): Promise<PostsResponseVo> {
const req: InstagramUserPostRequest = {
- fields: 'fields=id,caption,comments_count,like_count,media_type,media_url,permalink,thumbnail_url,timestamp,view_count,children\{media_url,thumbnail_url,media_type\}',
+ fields: 'id,caption,comments_count,like_count,media_type,media_url,permalink,thumbnail_url,timestamp,view_count,children\{media_url,thumbnail_url,media_type\}',
limit: (pagination as KeysetPagination)?.limit || 50,
before: (pagination as KeysetPagination)?.before,
after: (pagination as KeysetPagination)?.after,
@@ -63,6 +63,46 @@ export class InstagramEngagementProvider implements EngagementProvider {
}
}
+ async getMetaPostDetail(accountId: string, postId: string): Promise<PostVo> {
+ const query = {
+ fields: 'id,caption,comments_count,like_count,media_type,media_url,permalink,thumbnail_url,timestamp,children\{media_url,thumbnail_url,media_type\}',
+ }
+ const postDetail = await this.instagramService.getPostDetail(accountId, postId, query)
+
+ const medias = []
+ if (postDetail.media_type === InstagramMediaType.CAROUSEL_ALBUM && postDetail.children?.data) {
+ for (const child of postDetail.children.data) {
+ medias.push({
+ url: child.media_url,
+ type: child.media_type === 'VIDEO' ? 'video' : 'image',
+ })
+ }
+ }
+ else {
+ medias.push({
+ url: postDetail.media_url,
+ type: postDetail.media_type === 'VIDEO' ? 'video' : 'image',
+ thumbnail: postDetail.thumbnail_url || '',
+ })
+ }
+ return {
+ id: postDetail.id,
+ platform: 'instagram',
+ title: '',
+ content: postDetail.caption || '',
+ medias,
+ permalink: postDetail.permalink || '',
+ publishTime: new Date(postDetail.timestamp).getTime(),
+ viewCount: postDetail.view_count || 0,
+ commentCount: postDetail.comments_count || 0,
+ likeCount: postDetail.like_count || 0,
+ shareCount: 0,
+ clickCount: 0,
+ impressionCount: 0,
+ favoriteCount: 0,
+ }
+ }
+
async fetchPostComments(accountId: string, postId: string, pagination: KeysetPagination | OffsetPagination | null): Promise<FetchPostCommentsResponse> {
const query: IGPostCommentsRequest = {
fields: 'id,text,timestamp,from,replies',
diff --git a/project/backend/apps/aitoearn-channel/src/core/engagement/providers/threads.provider.ts b/project/backend/apps/aitoearn-channel/src/core/engagement/providers/threads.provider.ts
index 2f60f48f..22c36734 100644
--- a/project/backend/apps/aitoearn-channel/src/core/engagement/providers/threads.provider.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/engagement/providers/threads.provider.ts
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common'
-import { PostsResponseVo } from '@yikart/common'
+import { PostsResponseVo, PostVo } from '@yikart/common'
import { ThreadsService } from '../../../core/platforms/meta/threads.service'
import { ThreadsObjectCommentsRequest, ThreadsPostsRequest } from '../../../libs/threads/threads.interfaces'
import { KeysetPagination, OffsetPagination } from '../engagement.dto'
@@ -71,6 +71,45 @@ export class ThreadsEngagementProvider implements EngagementProvider {
}
}
+ async getMetaPostDetail(accountId: string, postId: string): Promise<PostVo> {
+ const query = {
+ fields: 'id,text,children.media_url,media_product_type,media_type,media_url,permalink,text,timestamp,thumbnail_url,insights.metric(likes,views,replies,reposts,quotes,shares)',
+ }
+ const postDetail = await this.threadsService.getPostDetail(accountId, postId, query)
+ const medias = []
+ if (postDetail.media_type === 'CAROUSEL_ALBUM' && postDetail.children?.data) {
+ for (const child of postDetail.children.data) {
+ medias.push({
+ url: child.media_url,
+ type: child.media_type === 'VIDEO' ? 'video' : 'image',
+ })
+ }
+ }
+ else {
+ medias.push({
+ url: postDetail.media_url,
+ type: postDetail.media_type === 'VIDEO' ? 'video' : 'image',
+ thumbnail: postDetail.thumbnail_url || '',
+ })
+ }
+ return {
+ id: postDetail.id,
+ platform: 'threads',
+ title: '',
+ content: postDetail.text || '',
+ medias,
+ permalink: postDetail.permalink || '',
+ publishTime: new Date(postDetail.timestamp).getTime(),
+ viewCount: postDetail.insights?.data?.find(d => d.name === 'views')?.values[0]?.value || 0,
+ commentCount: postDetail.insights?.data?.find(d => d.name === 'replies')?.values[0]?.value || 0,
+ likeCount: postDetail.insights?.data?.find(d => d.name === 'likes')?.values[0]?.value || 0,
+ shareCount: postDetail.insights?.data?.find(d => d.name === 'reposts')?.values[0]?.value || 0,
+ clickCount: 0,
+ impressionCount: 0,
+ favoriteCount: 0,
+ }
+ }
+
private async fetchThreadsComments(accountId: string, targetId: string, pagination: KeysetPagination | OffsetPagination | null): Promise<FetchPostCommentsResponse> {
const query: ThreadsObjectCommentsRequest = {
fields: 'id,text,timestamp,has_replies,username',
diff --git a/project/backend/apps/aitoearn-channel/src/core/engagement/providers/youtube.provider.ts b/project/backend/apps/aitoearn-channel/src/core/engagement/providers/youtube.provider.ts
index 36623bc2..f2061173 100644
--- a/project/backend/apps/aitoearn-channel/src/core/engagement/providers/youtube.provider.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/engagement/providers/youtube.provider.ts
@@ -1,5 +1,5 @@
import { Injectable, Logger } from '@nestjs/common'
-import { PostsResponseVo } from '@yikart/common'
+import { PostsResponseVo, PostVo } from '@yikart/common'
import { YoutubeService } from '../../../core/platforms/youtube/youtube.service'
import { KeysetPagination, OffsetPagination } from '../engagement.dto'
import { EngagementComment, EngagementProvider, FetchPostCommentsResponse, PublishCommentResponse } from '../engagement.interface'
@@ -22,6 +22,25 @@ export class YoutubeEngagementProvider implements EngagementProvider {
}
}
+ async getMetaPostDetail(_accountId: string, _postId: string): Promise<PostVo> {
+ return {
+ id: '',
+ platform: '',
+ title: '',
+ content: '',
+ medias: [],
+ permalink: '',
+ publishTime: 0,
+ viewCount: 0,
+ commentCount: 0,
+ likeCount: 0,
+ shareCount: 0,
+ clickCount: 0,
+ impressionCount: 0,
+ favoriteCount: 0,
+ }
+ }
+
private async fetchYoutubeCommentsThreads(accountId: string, videoId: string, pagination: KeysetPagination | OffsetPagination | null): Promise<FetchPostCommentsResponse> {
const resp = await this.youtubeService.getCommentThreadsList(
accountId,
diff --git a/project/backend/apps/aitoearn-channel/src/core/file/file.module.ts b/project/backend/apps/aitoearn-channel/src/core/file/file.module.ts
index f019c4d2..a9a12c66 100644
--- a/project/backend/apps/aitoearn-channel/src/core/file/file.module.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/file/file.module.ts
@@ -6,8 +6,8 @@
* @Description: 文件存储
*/
import { Global, Module } from '@nestjs/common'
+import { S3Module } from '@yikart/aws-s3'
import { config } from '../../config'
-import { S3Module } from '../../libs/aws-s3/s3.module'
import { FileService } from './file.service'
@Global()
diff --git a/project/backend/apps/aitoearn-channel/src/core/file/file.service.ts b/project/backend/apps/aitoearn-channel/src/core/file/file.service.ts
index f1740be1..a9ce9f0a 100644
--- a/project/backend/apps/aitoearn-channel/src/core/file/file.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/file/file.service.ts
@@ -1,9 +1,9 @@
import { Injectable } from '@nestjs/common'
+import { S3Service } from '@yikart/aws-s3'
import * as mime from 'mime-types'
import moment from 'moment'
import { v4 as uuidv4 } from 'uuid'
import { config } from '../../config'
-import { S3Service } from '../../libs/aws-s3/s3.service'
@Injectable()
export class FileService {
@@ -46,10 +46,9 @@ export class FileService {
permanent,
})
const filePath = `${newPath}/${newFileName}.${mime.extension(file.mimetype)}`
- const res = await this.s3Service.uploadFile(
+ const res = await this.s3Service.putObject(
filePath,
file.buffer,
- file.mimetype,
)
return res
@@ -73,13 +72,12 @@ export class FileService {
): Promise<string> {
const { path, permanent, fileType } = option
const objectName = `${permanent ? '' : 'temp/'}${path || 'nopath'}${`/${moment().format('YYYYMM')}/${uuidv4()}.${fileType}`}`
- const res = await this.s3Service.uploadFile(
+ const res = await this.s3Service.putObject(
objectName,
buffer,
- `application/${fileType}`,
)
- return res.key
+ return res.path
}
/**
@@ -106,13 +104,12 @@ export class FileService {
const fileType = mime.extension(contentType) || 'jpg'
const objectName = `${permanent ? '' : 'temp/'}${path || 'nopath'}${`/${moment().format('YYYYMM')}/${uuidv4()}.${fileType}`}`
- const res = await this.s3Service.uploadFile(
+ const res = await this.s3Service.putObject(
objectName,
buffer,
- contentType,
)
- return res.key
+ return res.path
}
/**
@@ -189,6 +186,6 @@ export class FileService {
filePathToUrl(url: string): string {
if (url.startsWith('http'))
return url
- return `${config.awsS3.hostUrl}/${url}`
+ return `${config.awsS3.endpoint}/${url}`
}
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/bilibili/bilibili.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/bilibili/bilibili.service.ts
index f8bd9381..e7a35113 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/bilibili/bilibili.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/bilibili/bilibili.service.ts
@@ -165,24 +165,6 @@ export class BilibiliService {
= await this.bilibiliApiService.getAccountInfo(accessToken)
if (!bilibiliUserInfo)
return null
-
- try {
- const newPath = await this.fileService.upFileByUrl(
- bilibiliUserInfo.face,
- {
- path: 'account/avatar',
- permanent: true,
- },
- )
-
- bilibiliUserInfo.face = newPath
- }
- catch (error) {
- this.logger.log('---- bilibil getAccountInfo chage face error ', error)
-
- bilibiliUserInfo.face = 'error'
- }
-
return bilibiliUserInfo
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/dump-avatar.consumer.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/dump-avatar.consumer.ts
new file mode 100644
index 00000000..fcba16f7
--- /dev/null
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/dump-avatar.consumer.ts
@@ -0,0 +1,76 @@
+import path from 'node:path'
+import { OnWorkerEvent, Processor, WorkerHost } from '@nestjs/bullmq'
+import { Logger, OnModuleDestroy } from '@nestjs/common'
+import { QueueName } from '@yikart/aitoearn-queue'
+import { S3Service } from '@yikart/aws-s3'
+import { Job, UnrecoverableError } from 'bullmq'
+import { AccountService } from '../account/account.service'
+
+@Processor(QueueName.DumpSocialMediaAvatar, {
+ concurrency: 3,
+ stalledInterval: 15000,
+ maxStalledCount: 1,
+})
+export class DumpAvatarConsumer extends WorkerHost implements OnModuleDestroy {
+ private readonly logger = new Logger(DumpAvatarConsumer.name)
+ constructor(
+ private readonly s3Service: S3Service,
+ private readonly accountService: AccountService,
+ ) {
+ super()
+ }
+
+ private async uploadImageToS3(accountId: string, platform: string, imageUrl: string): Promise<string> {
+ const filename = `${Date.now().toString(36)}-${path.basename(imageUrl.split('?')[0])}`
+ const fullPath = path.join('social/avatar', platform, accountId, filename)
+ const result = await this.s3Service.putObjectFromUrl(imageUrl, fullPath)
+ return result.path
+ }
+
+ async process(
+ job: Job<{
+ accountId: string
+ }>,
+ ): Promise<any> {
+ try {
+ const account = await this.accountService.getAccountInfo(job.data.accountId)
+ const avatar = account?.avatar
+ if (avatar && avatar.startsWith('http')) {
+ const s3Path = await this.uploadImageToS3(
+ job.data.accountId,
+ account.type,
+ avatar,
+ )
+ await this.accountService.updateAccountInfo(account.userId, job.data.accountId, {
+ id: job.data.accountId,
+ avatar: s3Path,
+ })
+ this.logger.log(`[account-${job.data.accountId}] Avatar dumped to S3: ${s3Path}`)
+ return { success: true, path: s3Path }
+ }
+ }
+ catch (error) {
+ this.logger.error(`[account-${job.data.accountId}] Failed to dump avatar: ${error.message}`)
+ throw new UnrecoverableError(error)
+ }
+ }
+
+ @OnWorkerEvent('completed')
+ async onCompleted(job: Job<{
+ accountId: string
+ }>) {
+ const { accountId } = job.data
+ this.logger.log(`[account-${accountId}] Processing completed for job ${job.id}, Attempts: ${job.attemptsMade}`)
+ }
+
+ @OnWorkerEvent('stalled')
+ onStalled(job: Job) {
+ this.logger.error(`Job ${job.id}] is stalled, data ${job.data}`)
+ }
+
+ async onModuleDestroy() {
+ this.logger.log('DumpAvatarConsumer is being destroyed, closing worker...')
+ await this.worker.close()
+ this.logger.log('DumpAvatarConsumer closed successfully')
+ }
+}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/base.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/base.service.ts
index d85eca91..4bb53d65 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/base.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/base.service.ts
@@ -28,6 +28,7 @@ export class MetaBaseService {
}
let credential = await this.redisService.getJson<MetaUserOAuthCredential>(key)
if (!credential) {
+ this.logger.debug(`No access token found for accountId: ${accountId} in redis`)
const oauth2Credential = await this.oAuth2CredentialModel.findOne(
{
accountId,
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/facebook.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/facebook.service.ts
index e8688534..b9e1b789 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/facebook.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/facebook.service.ts
@@ -15,6 +15,7 @@ import {
FacebookPostAttachmentsResponse,
FacebookPostCommentsRequest,
FacebookPostCommentsResponse,
+ FacebookPostDetail,
FacebookPostDetailResponse,
FacebookPostEdgesRequest,
FacebookPostEdgesResponse,
@@ -276,6 +277,15 @@ export class FacebookService extends MetaBaseService {
}
}
+ async getPostDetail(
+ accountId: string,
+ postId: string,
+ fields: string,
+ ): Promise<FacebookPostDetail> {
+ const credential = await this.authorizePage(accountId)
+ return await this.facebookAPIService.getPagePostDetails(postId, credential.access_token, { fields })
+ }
+
async getPostInsights(
accountId: string,
postId: string,
@@ -291,7 +301,7 @@ export class FacebookService extends MetaBaseService {
const postDetail = await this.facebookAPIService.getPagePostDetails(
objectId,
credential.access_token,
- { field: 'shares' },
+ { fields: 'shares' },
)
const comments = await this.facebookAPIService.getPostComments(
objectId,
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/instagram.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/instagram.service.ts
index cf7c9d09..0cc7e1a3 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/instagram.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/instagram.service.ts
@@ -12,6 +12,7 @@ import {
InstagramObjectInfo,
InstagramUserInfoRequest,
InstagramUserInfoResponse,
+ InstagramUserPost,
InstagramUserPostRequest,
InstagramUserPostResponse,
} from '../../../libs/instagram/instagram.interfaces'
@@ -175,6 +176,15 @@ export class InstagramService extends MetaBaseService {
)
}
+ async getPostDetail(
+ accountId: string,
+ postId: string,
+ query: InstagramUserPostRequest,
+ ): Promise<InstagramUserPost> {
+ const credential = await this.authorize(accountId)
+ return await this.instagramAPIService.getPostDetail(credential.access_token, postId, query)
+ }
+
async fetchPostComments(
accountId: string,
postId: string,
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/threads.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/threads.service.ts
index 18210245..4c4d6a2d 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/meta/threads.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/meta/threads.service.ts
@@ -8,6 +8,7 @@ import {
ThreadsObjectCommentsRequest,
ThreadsObjectCommentsResponse,
ThreadsObjectInfo,
+ ThreadsPostItem,
ThreadsPostResponse,
ThreadsPostsRequest,
ThreadsPostsResponse,
@@ -175,6 +176,23 @@ export class ThreadsService extends MetaBaseService {
)
}
+ async getPostDetail(
+ accountId: string,
+ postId: string,
+ query: ThreadsPostsRequest,
+ ): Promise<ThreadsPostItem | null> {
+ const credential = await this.authorize(accountId)
+ if (!credential) {
+ this.logger.error(`No valid access token found for accountId: ${accountId}`)
+ return null
+ }
+ return await this.threadsAPIService.getPostDetail(
+ credential.access_token,
+ postId,
+ query,
+ )
+ }
+
async fetchObjectComments(
accountId: string,
objectId: string,
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/platforms.module.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/platforms.module.ts
index 4552bd05..0eae5dee 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/platforms.module.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/platforms.module.ts
@@ -1,5 +1,6 @@
import { Module } from '@nestjs/common'
import { BilibiliModule } from './bilibili/bilibili.module'
+import { DumpAvatarConsumer } from './dump-avatar.consumer'
import { KwaiModule } from './kwai/kwai.module'
import { MetaModule } from './meta/meta.module'
import { PinterestModule } from './pinterest/pinterest.module'
@@ -22,7 +23,7 @@ import { YoutubeModule } from './youtube/youtube.module'
YoutubeModule,
],
controllers: [PlatformController],
- providers: [PlatformService],
+ providers: [PlatformService, DumpAvatarConsumer],
exports: [],
})
export class PlatModule {}
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/consumers/finalize-publish.consumer.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/consumers/finalize-publish.consumer.ts
index 32ce1e4a..44c78e64 100644
--- a/project/backend/apps/aitoearn-channel/src/core/publishing/consumers/finalize-publish.consumer.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/consumers/finalize-publish.consumer.ts
@@ -2,11 +2,10 @@ import { OnWorkerEvent, Processor, WorkerHost } from '@nestjs/bullmq'
import { Inject, Logger, OnModuleDestroy } from '@nestjs/common'
import { QueueName } from '@yikart/aitoearn-queue'
import { AccountType } from '@yikart/common'
-import { Job, UnrecoverableError } from 'bullmq'
+import { Job } from 'bullmq'
import { PublishStatus } from '../../../libs/database/schema/publishTask.schema'
-import { SocialMediaError } from '../../../libs/exception'
+import { PublishingErrorHandler } from '../error-handler.service'
import { PublishService } from '../providers/base.service'
-import { PublishingException } from '../publishing.exception'
import { PublishingService } from '../publishing.service'
@Processor(QueueName.PostMediaTask, {
@@ -22,6 +21,7 @@ export class FinalizePublishPostConsumer extends WorkerHost implements OnModuleD
constructor(
readonly publishingService: PublishingService,
+ private readonly publishingErrorHandler: PublishingErrorHandler,
) {
super()
}
@@ -52,31 +52,7 @@ export class FinalizePublishPostConsumer extends WorkerHost implements OnModuleD
}
}
catch (error) {
- if (error instanceof PublishingException) {
- if (error.retryable) {
- throw error
- }
- await this.publishingService.updatePublishTaskStatus(job.data.taskId, {
- status: PublishStatus.FAILED,
- errorMsg: error.message,
- })
- throw new UnrecoverableError(error.message)
- }
- if (error instanceof SocialMediaError) {
- if (error.isNetworkError) {
- throw error
- }
- await this.publishingService.updatePublishTaskStatus(job.data.taskId, {
- status: PublishStatus.FAILED,
- errorMsg: error.message,
- })
- throw new UnrecoverableError(error.message)
- }
- await this.publishingService.updatePublishTaskStatus(job.data.taskId, {
- status: PublishStatus.FAILED,
- errorMsg: error.toString(),
- })
- throw new UnrecoverableError(error.toString())
+ await this.publishingErrorHandler.handle(job.data.taskId, error, job)
}
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/consumers/immediate-publish.consumer.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/consumers/immediate-publish.consumer.ts
index 7ef0dcc9..2a47e2f9 100644
--- a/project/backend/apps/aitoearn-channel/src/core/publishing/consumers/immediate-publish.consumer.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/consumers/immediate-publish.consumer.ts
@@ -2,11 +2,10 @@ import { OnWorkerEvent, Processor, WorkerHost } from '@nestjs/bullmq'
import { Inject, Logger, OnModuleDestroy } from '@nestjs/common'
import { QueueName } from '@yikart/aitoearn-queue'
import { AccountType } from '@yikart/common'
-import { Job, UnrecoverableError } from 'bullmq'
+import { Job } from 'bullmq'
import { PublishStatus } from '../../../libs/database/schema/publishTask.schema'
-import { SocialMediaError } from '../../../libs/exception'
+import { PublishingErrorHandler } from '../error-handler.service'
import { PublishService } from '../providers/base.service'
-import { PublishingException } from '../publishing.exception'
import { PublishingTaskResult } from '../publishing.interface'
import { PublishingService } from '../publishing.service'
@@ -20,7 +19,10 @@ export class ImmediatePublishPostConsumer extends WorkerHost implements OnModule
@Inject('PUBLISHING_PROVIDERS')
private readonly publishingProviders: Record<AccountType, PublishService>
- constructor(readonly publishingService: PublishingService) {
+ constructor(
+ readonly publishingService: PublishingService,
+ private readonly publishingErrorHandler: PublishingErrorHandler,
+ ) {
super()
}
@@ -81,39 +83,7 @@ export class ImmediatePublishPostConsumer extends WorkerHost implements OnModule
}
}
catch (error: unknown) {
- if (error instanceof PublishingException) {
- if (error.retryable) {
- throw error
- }
- await this.publishingService.updatePublishTaskStatus(taskId, {
- status: PublishStatus.FAILED,
- errorMsg: error.message,
- inQueue: false,
- queued: false,
- })
- throw new UnrecoverableError(error.message)
- }
- if (error instanceof SocialMediaError) {
- if (error.isNetworkError) {
- if (job.opts.backoff) {
- throw error
- }
- }
- await this.publishingService.updatePublishTaskStatus(taskId, {
- status: PublishStatus.FAILED,
- errorMsg: error.message,
- inQueue: false,
- queued: false,
- })
- throw new UnrecoverableError(error.message)
- }
- await this.publishingService.updatePublishTaskStatus(taskId, {
- status: PublishStatus.FAILED,
- errorMsg: error.toString(),
- inQueue: false,
- queued: false,
- })
- throw new UnrecoverableError(error.toString())
+ await this.publishingErrorHandler.handle(taskId, error, job)
}
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/credential-invalidation.service.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/credential-invalidation.service.ts
new file mode 100644
index 00000000..8c8f4f5f
--- /dev/null
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/credential-invalidation.service.ts
@@ -0,0 +1,79 @@
+import { Inject, Injectable, Logger } from '@nestjs/common'
+import { InjectModel } from '@nestjs/mongoose'
+import { AccountType } from '@yikart/aitoearn-server-client'
+import { RedisService } from '@yikart/redis'
+import { Model } from 'mongoose'
+import { OAuth2Credential } from '../../libs/database/schema/oauth2Credential.schema'
+import { AccountService } from '../account/account.service'
+import { MetaRedisKeys } from '../platforms/meta/constants'
+import { TiktokRedisKeys } from '../platforms/tiktok/constants'
+import { TwitterRedisKeys } from '../platforms/twitter/constants'
+
+@Injectable()
+export class CredentialInvalidationService {
+ private readonly logger = new Logger(CredentialInvalidationService.name)
+
+ @Inject(RedisService)
+ private readonly redisService: RedisService
+
+ @InjectModel(OAuth2Credential.name)
+ private readonly oAuth2CredentialModel: Model<OAuth2Credential>
+
+ constructor(
+ private readonly accountService: AccountService,
+ ) {}
+
+ private getRedisKeysToDelete(accountId: string, accountType: AccountType): string {
+ switch (accountType) {
+ case AccountType.FACEBOOK:
+ return MetaRedisKeys.getUserPageAccessTokenKey('facebook', accountId)
+ case AccountType.INSTAGRAM:
+ case AccountType.THREADS:
+ case AccountType.LINKEDIN:
+ return MetaRedisKeys.getAccessTokenKey(accountType, accountId)
+ case AccountType.TWITTER:
+ return TwitterRedisKeys.getAccessTokenKey(accountId)
+ case AccountType.TIKTOK:
+ return TiktokRedisKeys.getAccessTokenKey(accountId)
+ case AccountType.YOUTUBE:
+ case AccountType.PINTEREST:
+ case AccountType.BILIBILI:
+ case AccountType.KWAI:
+ case AccountType.WxGzh:
+ return `${accountType.toLowerCase()}:accessToken:${accountId}`
+ default:
+ return ''
+ }
+ }
+
+ private async deleteRedisCredential(accountId: string, accountType: AccountType) {
+ try {
+ const key = this.getRedisKeysToDelete(accountId, accountType)
+ if (!key) {
+ return
+ }
+ await this.redisService.del(key)
+ }
+ catch (err) {
+ this.logger.warn(`Failed to delete Redis credential for accountId=${accountId}, type=${accountType}: ${err?.message}`)
+ }
+ }
+
+ private async deleteDbCredential(accountId: string, accountType: AccountType) {
+ try {
+ await this.oAuth2CredentialModel.deleteOne({
+ accountId,
+ platform: accountType,
+ }).exec()
+ }
+ catch (err) {
+ this.logger.warn(`Failed to delete DB credential for accountId=${accountId}, type=${accountType}: ${err?.message}`)
+ }
+ }
+
+ async invalidate(accountId: string, accountType: AccountType): Promise<void> {
+ this.logger.log(`Invalidating credentials for accountId=${accountId}, type=${accountType}`)
+ await this.deleteRedisCredential(accountId, accountType)
+ await this.deleteDbCredential(accountId, accountType)
+ }
+}
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/dto/meta.container.dto.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/dto/media-container.dto.ts
similarity index 85%
rename from project/backend/apps/aitoearn-channel/src/core/publishing/dto/meta.container.dto.ts
rename to project/backend/apps/aitoearn-channel/src/core/publishing/dto/media-container.dto.ts
index c32fcab5..e15b9ad7 100644
--- a/project/backend/apps/aitoearn-channel/src/core/publishing/dto/meta.container.dto.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/dto/media-container.dto.ts
@@ -2,7 +2,7 @@ import { createZodDto } from '@yikart/common'
import { z } from 'zod'
import { PostCategory, PostMediaStatus, PostSubCategory } from '../../../libs/database/schema/postMediaContainer.schema'
-export const PostMediaContainer = z.object({
+export const MediaContainer = z.object({
publishId: z.string().describe('发布任务ID'),
userId: z.string().describe('用户ID'),
jobId: z.string().describe('任务ID'),
@@ -15,4 +15,4 @@ export const PostMediaContainer = z.object({
option: z.any().optional(),
})
-export class CreatePostMediaContainerDto extends createZodDto(PostMediaContainer) {}
+export class CreateMediaContainerDto extends createZodDto(MediaContainer) {}
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/dto/tiktok.webhook.dto.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/dto/tiktok-webhook.dto.ts
similarity index 100%
rename from project/backend/apps/aitoearn-channel/src/core/publishing/dto/tiktok.webhook.dto.ts
rename to project/backend/apps/aitoearn-channel/src/core/publishing/dto/tiktok-webhook.dto.ts
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/error-handler.service.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/error-handler.service.ts
new file mode 100644
index 00000000..b0a02d48
--- /dev/null
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/error-handler.service.ts
@@ -0,0 +1,75 @@
+import { Injectable, Logger } from '@nestjs/common'
+import { AccountType } from '@yikart/common'
+import { Job, UnrecoverableError } from 'bullmq'
+import { PublishStatus } from '../../libs/database/schema/publishTask.schema'
+import { SocialMediaError } from '../../libs/exception'
+import { CredentialInvalidationService } from './credential-invalidation.service'
+import { PublishingException } from './publishing.exception'
+import { PublishingService } from './publishing.service'
+
+@Injectable()
+export class PublishingErrorHandler {
+ private readonly logger = new Logger(PublishingErrorHandler.name)
+
+ constructor(
+ private readonly publishingService: PublishingService,
+ private readonly credentialInvalidationService: CredentialInvalidationService,
+ ) {}
+
+ private async failTask(taskId: string, message: string) {
+ await this.publishingService.updatePublishTaskStatus(taskId, {
+ status: PublishStatus.FAILED,
+ errorMsg: message,
+ inQueue: false,
+ queued: false,
+ })
+ }
+
+ private async handleAuthFailure(taskId: string) {
+ try {
+ const task = await this.publishingService.getPublishTaskInfo(taskId)
+ if (!task) {
+ this.logger.warn(`Unable to load task/account info for auth failure, taskId=${taskId}`)
+ return
+ }
+ await this.credentialInvalidationService.invalidate(
+ task.accountId,
+ task.accountType as AccountType,
+ )
+ }
+ catch (err) {
+ this.logger.warn(`Auth failure cleanup encountered an error for taskId=${taskId}: ${err?.message}`)
+ }
+ }
+
+ async handle(
+ taskId: string,
+ error: unknown,
+ _job?: Job,
+ ): Promise<never> {
+ if (error instanceof PublishingException) {
+ if (error.retryable) {
+ throw error
+ }
+ await this.failTask(taskId, error.message)
+ throw new UnrecoverableError(error.message)
+ }
+
+ if (error instanceof SocialMediaError) {
+ if (error.isNetworkError) {
+ throw error
+ }
+ if (error.status === 401) {
+ await this.handleAuthFailure(taskId)
+ await this.failTask(taskId, error.message)
+ throw new UnrecoverableError(error.message)
+ }
+ const message = error.message || 'Client error'
+ await this.failTask(taskId, message)
+ throw new UnrecoverableError(message)
+ }
+ const message = error.toString() || 'Unknown error'
+ await this.failTask(taskId, message)
+ throw new UnrecoverableError(message)
+ }
+}
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/media-staging.service.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/media-staging.service.ts
new file mode 100644
index 00000000..9c1eae43
--- /dev/null
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/media-staging.service.ts
@@ -0,0 +1,59 @@
+import { Injectable } from '@nestjs/common'
+import { InjectModel } from '@nestjs/mongoose'
+import { Model } from 'mongoose'
+import { PostMediaContainer, PostMediaStatus } from '../../libs/database/schema/postMediaContainer.schema'
+import { CreateMediaContainerDto } from './dto/media-container.dto'
+
+@Injectable()
+export class MediaStagingService {
+ constructor(
+ @InjectModel(PostMediaContainer.name)
+ private readonly mediaContainerModel: Model<PostMediaContainer>,
+ ) {}
+
+ async createMediaContainer(
+ data: CreateMediaContainerDto,
+ ): Promise<PostMediaContainer> {
+ return await this.mediaContainerModel.create(data)
+ }
+
+ async deleteMediaContainer(publishId: string): Promise<void> {
+ await this.mediaContainerModel.deleteMany({ publishId }).exec()
+ }
+
+ async upsertMediaContainer(
+ data: CreateMediaContainerDto,
+ ): Promise<PostMediaContainer> {
+ return await this.mediaContainerModel.findOneAndUpdate(
+ { publishId: data.publishId, platform: data.platform },
+ data,
+ { upsert: true, new: true },
+ ).exec()
+ }
+
+ async getMediaContainers(publishId: string, jobId: string): Promise<PostMediaContainer[]> {
+ return this.mediaContainerModel.find({ publishId, jobId }).sort({ createdAt: 1 })
+ }
+
+ async getUnProcessedMediaContainers(publishId: string): Promise<PostMediaContainer[]> {
+ return this.mediaContainerModel.find({ publishId, $or: [
+ { status: PostMediaStatus.CREATED },
+ { status: PostMediaStatus.IN_PROGRESS },
+ ] }).exec()
+ }
+
+ async getCompletedMediaContainersCount(publishId: string): Promise<number> {
+ return this.mediaContainerModel.countDocuments({ publishId, status: PostMediaStatus.FINISHED })
+ }
+
+ async updateMediaContainer(
+ id: string,
+ data: Partial<CreateMediaContainerDto>,
+ ): Promise<PostMediaContainer | null> {
+ return this.mediaContainerModel.findByIdAndUpdate(
+ id,
+ data,
+ { new: true },
+ ).exec()
+ }
+}
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 20e91ba6..c10abff8 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
@@ -10,9 +10,9 @@ import {
PublishTask,
} from '../../../libs/database/schema/publishTask.schema'
import { PublishRecordService } from '../../account/publish-record.service'
+import { MediaStagingService } from '../media-staging.service'
import { PublishingException } from '../publishing.exception'
import { MediaProcessingStatus, MediaProcessingStatusResult, PublishingTaskResult } from '../publishing.interface'
-import { PostMediaContainerService } from './container.service'
@Injectable()
export abstract class PublishService {
@@ -31,8 +31,8 @@ export abstract class PublishService {
@Inject(PublishRecordService)
protected readonly publishRecordService: PublishRecordService
- @Inject(PostMediaContainerService)
- protected readonly postMediaContainerService: PostMediaContainerService
+ @Inject(MediaStagingService)
+ protected readonly mediaStagingService: MediaStagingService
constructor() {}
@@ -146,7 +146,7 @@ export abstract class PublishService {
taskId: string,
status: PostMediaStatus = PostMediaStatus.CREATED,
): Promise<void> {
- await this.postMediaContainerService.createMetaPostMedia({
+ await this.mediaStagingService.createMediaContainer({
jobId: publishTask.queueId,
accountId: publishTask.accountId,
publishId: publishTask.id,
@@ -183,7 +183,7 @@ export abstract class PublishService {
): Promise<MediaProcessingStatusResult> {
const mediasStatus: MediaProcessingStatus[] = []
let hasFailed = false
- const medias = await this.postMediaContainerService.getContainers(
+ const medias = await this.mediaStagingService.getMediaContainers(
task.id,
task.queueId,
)
@@ -210,7 +210,7 @@ export abstract class PublishService {
status = PostMediaStatus.FINISHED
completedCount++
}
- await this.postMediaContainerService.updateContainer(media.id, {
+ await this.mediaStagingService.updateMediaContainer(media.id, {
status,
})
mediasStatus.push({ id: media.id, status, taskId: media.taskId, category: media.category })
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/providers/container.service.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/providers/container.service.ts
deleted file mode 100644
index 3819c694..00000000
--- a/project/backend/apps/aitoearn-channel/src/core/publishing/providers/container.service.ts
+++ /dev/null
@@ -1,59 +0,0 @@
-import { Injectable } from '@nestjs/common'
-import { InjectModel } from '@nestjs/mongoose'
-import { Model } from 'mongoose'
-import { PostMediaContainer, PostMediaStatus } from '../../../libs/database/schema/postMediaContainer.schema'
-import { CreatePostMediaContainerDto } from '../dto/meta.container.dto'
-
-@Injectable()
-export class PostMediaContainerService {
- constructor(
- @InjectModel(PostMediaContainer.name)
- private readonly metaPostMediaModel: Model<PostMediaContainer>,
- ) {}
-
- async createMetaPostMedia(
- data: CreatePostMediaContainerDto,
- ): Promise<PostMediaContainer> {
- return await this.metaPostMediaModel.insertOne(data)
- }
-
- async deleteMetaPostMedia(publishId: string): Promise<void> {
- await this.metaPostMediaModel.deleteMany({ publishId }).exec()
- }
-
- async upsertMetaPostMedia(
- data: CreatePostMediaContainerDto,
- ): Promise<PostMediaContainer> {
- return await this.metaPostMediaModel.findOneAndUpdate(
- { publishId: data.publishId, platform: data.platform },
- data,
- { upsert: true, new: true },
- ).exec()
- }
-
- async getContainers(publishId: string, jobId: string): Promise<PostMediaContainer[]> {
- return this.metaPostMediaModel.find({ publishId, jobId }).sort({ createdAt: 1 })
- }
-
- async getUnProcessedContainers(publishId: string): Promise<PostMediaContainer[]> {
- return this.metaPostMediaModel.find({ publishId, $or: [
- { status: PostMediaStatus.CREATED },
- { status: PostMediaStatus.IN_PROGRESS },
- ] }).exec()
- }
-
- async getCompletedContainersCount(publishId: string): Promise<number> {
- return this.metaPostMediaModel.countDocuments({ publishId, status: PostMediaStatus.FINISHED })
- }
-
- async updateContainer(
- id: string,
- data: Partial<CreatePostMediaContainerDto>,
- ): Promise<PostMediaContainer | null> {
- return this.metaPostMediaModel.findByIdAndUpdate(
- id,
- data,
- { new: true },
- ).exec()
- }
-}
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/providers/tiktok.service.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/providers/tiktok.service.ts
index a048d4ee..2adccbbd 100644
--- a/project/backend/apps/aitoearn-channel/src/core/publishing/providers/tiktok.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/providers/tiktok.service.ts
@@ -5,7 +5,7 @@ import { PublishStatus, PublishTask } from '../../../libs/database/schema/publis
import { TiktokPrivacyLevel, TiktokSourceType } from '../../../libs/tiktok/tiktok.enum'
import { PostInfoDto, VideoFileUploadSourceDto, VideoPullUrlSourceDto } from '../../platforms/tiktok/dto/tiktok.dto'
import { TiktokService } from '../../platforms/tiktok/tiktok.service'
-import { TiktokWebhookDto } from '../dto/tiktok.webhook.dto'
+import { TiktokWebhookDto } from '../dto/tiktok-webhook.dto'
import { PublishingException } from '../publishing.exception'
import { PublishingTaskResult } from '../publishing.interface'
import { PublishService } from './base.service'
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/publishing.controller.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/publishing.controller.ts
index 0e323d23..247d7cfc 100644
--- a/project/backend/apps/aitoearn-channel/src/core/publishing/publishing.controller.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/publishing.controller.ts
@@ -1,6 +1,5 @@
import { Body, Controller, Logger, Post } from '@nestjs/common'
-import { ApiTags } from '@nestjs/swagger'
-import { ApiDoc, AppException, ResponseCode } from '@yikart/common'
+import { AppException, ResponseCode } from '@yikart/common'
import { PublishStatus } from '../../libs/database/schema/publishTask.schema'
import {
CreatePublishDto,
@@ -8,12 +7,10 @@ import {
NowPubTaskDto,
PublishRecordListFilterDto,
UpPublishTaskTimeDto,
- UpPublishTaskTimeSchema,
} from './dto/publish.dto'
-import { TiktokWebhookDto, TiktokWebhookSchema } from './dto/tiktok.webhook.dto'
+import { TiktokWebhookDto, TiktokWebhookSchema } from './dto/tiktok-webhook.dto'
import { PublishingService } from './publishing.service'
-@ApiTags('OpenSource/Core/Publishing/Publishing')
@Controller()
export class PublishingController {
private readonly logger = new Logger(PublishingController.name)
@@ -21,18 +18,11 @@ export class PublishingController {
private readonly publishingService: PublishingService,
) {}
- @ApiDoc({
- summary: 'Create Publishing Task',
- })
@Post('plat/publish/create')
async createPublishingTask(@Body() data: CreatePublishDto) {
return await this.publishingService.createPublishingTask(data)
}
- @ApiDoc({
- summary: 'Update Publishing Time',
- body: UpPublishTaskTimeSchema,
- })
@Post('plat/publish/changeTime')
async changeTaskTime(@Body() data: UpPublishTaskTimeDto) {
data.publishTime = new Date(data.publishTime)
@@ -44,9 +34,6 @@ export class PublishingController {
return res
}
- @ApiDoc({
- summary: 'Delete Publishing Task',
- })
@Post('publish/task/delete')
async deletePublishTask(@Body() data: DeletePublishTaskDto) {
return await this.publishingService.deletePublishTaskById(
@@ -55,9 +42,6 @@ export class PublishingController {
)
}
- @ApiDoc({
- summary: 'Trigger Immediate Publishing Task',
- })
@Post('publish/task/run')
async nowPubTask(@Body() data: NowPubTaskDto) {
const info = await this.publishingService.getPublishTaskInfo(data.id)
@@ -74,9 +58,6 @@ export class PublishingController {
return PublishStatus.PUBLISHING
}
- @ApiDoc({
- summary: 'Handle TikTok Publishing Webhook',
- })
@Post('publish/tiktok/post/webhook')
async handleTiktokWebhook(@Body() data: any) {
this.logger.log(`Received TikTok webhook: ${JSON.stringify(data)}`)
@@ -91,54 +72,30 @@ export class PublishingController {
return { status: 'success', message: 'Webhook processed' }
}
- @ApiDoc({
- summary: 'List Publishing Tasks',
- body: PublishRecordListFilterDto.schema,
- })
@Post('channel/publishTask/list')
async getPublishTaskList(@Body() data: PublishRecordListFilterDto) {
const res = await this.publishingService.getPublishTasks(data)
return res
}
- @ApiDoc({
- summary: 'List Queued Publishing Tasks',
- body: PublishRecordListFilterDto.schema,
- })
@Post('channel/status/queued/tasks')
async getQueuedPublishTaskList(@Body() data: PublishRecordListFilterDto) {
- const res = await this.publishingService.getQueuedPublishTasks({
- ...data,
- time: data.time as [Date, Date] | undefined,
- })
+ const res = await this.publishingService.getQueuedPublishTasks(data)
return res
}
- @ApiDoc({
- summary: 'List Published Tasks',
- body: PublishRecordListFilterDto.schema,
- })
@Post('channel/status/published/tasks')
async getPublishedPublishTaskList(@Body() data: PublishRecordListFilterDto) {
- const res = await this.publishingService.getPublishedPublishTasks({
- ...data,
- time: data.time as [Date, Date] | undefined,
- })
+ const res = await this.publishingService.getPublishedPublishTasks(data)
return res
}
- @ApiDoc({
- summary: 'Get Publishing Task Detail',
- })
@Post('channel/publishTask/detail')
async getPublishingTaskDetail(@Body() data: { flowId: string, userId: string }) {
const res = await this.publishingService.getPublishTaskInfoWithFlowId(data.flowId, data.userId)
return res
}
- @ApiDoc({
- summary: 'Get Publishing Task by User',
- })
@Post('channel/publishing/task/detail')
async getPublishTaskInfoWithUserId(@Body() data: { taskId: string, userId: string }) {
const res = await this.publishingService.getPublishTaskInfoWithUserId(data.taskId, data.userId)
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/publishing.module.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/publishing.module.ts
index 30b59056..38a41c5e 100644
--- a/project/backend/apps/aitoearn-channel/src/core/publishing/publishing.module.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/publishing.module.ts
@@ -2,6 +2,7 @@ import { forwardRef, Module } from '@nestjs/common'
import { MongooseModule } from '@nestjs/mongoose'
import { AccountType } from '@yikart/common'
import { Account, AccountSchema } from '../../libs/database/schema/account.schema'
+import { OAuth2Credential, OAuth2CredentialSchema } from '../../libs/database/schema/oauth2Credential.schema'
import {
PostMediaContainer,
PostMediaContainerSchema,
@@ -20,8 +21,10 @@ import { WxPlatModule } from '../platforms/wx-plat/wx-plat.module'
import { YoutubeModule } from '../platforms/youtube/youtube.module'
import { FinalizePublishPostConsumer } from './consumers/finalize-publish.consumer'
import { ImmediatePublishPostConsumer } from './consumers/immediate-publish.consumer'
+import { CredentialInvalidationService } from './credential-invalidation.service'
+import { PublishingErrorHandler } from './error-handler.service'
+import { MediaStagingService } from './media-staging.service'
import { BilibiliPubService } from './providers/bilibili.service'
-import { PostMediaContainerService } from './providers/container.service'
import { FacebookPublishService } from './providers/facebook.service'
import { InstagramPublishService } from './providers/instgram.service'
import { kwaiPubService } from './providers/kwai.service'
@@ -42,6 +45,7 @@ import { EnqueuePublishingTaskScheduler } from './scheduler/enqueue-publishing-t
{ name: Account.name, schema: AccountSchema },
{ name: PublishTask.name, schema: PublishTaskSchema },
{ name: PostMediaContainer.name, schema: PostMediaContainerSchema },
+ { name: OAuth2Credential.name, schema: OAuth2CredentialSchema },
]),
BilibiliModule,
PinterestModule,
@@ -54,7 +58,9 @@ import { EnqueuePublishingTaskScheduler } from './scheduler/enqueue-publishing-t
PinterestModule,
],
providers: [
- PostMediaContainerService,
+ CredentialInvalidationService,
+ PublishingErrorHandler,
+ MediaStagingService,
PublishingService,
ImmediatePublishPostConsumer,
FinalizePublishPostConsumer,
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 e35c99a3..31a5e57d 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
@@ -10,7 +10,7 @@ import { AccountService } from '../account/account.service'
import { PublishRecordService } from '../account/publish-record.service'
import { IMMEDIATE_PUBLISH_TOLERANCE_SECONDS } from './constant'
import { CreatePublishDto, PublishRecordListFilterDto } from './dto/publish.dto'
-import { TiktokWebhookDto } from './dto/tiktok.webhook.dto'
+import { TiktokWebhookDto } from './dto/tiktok-webhook.dto'
import { TiktokPubService } from './providers/tiktok.service'
@Injectable()
@@ -170,7 +170,7 @@ export class PublishingService implements OnModuleDestroy {
userId: string
accountId?: string
accountType?: AccountType
- time?: [Date, Date]
+ time?: [Date?, Date?, ...unknown[]]
}): Promise<PublishTask[]> {
const filters: RootFilterQuery<PublishTask> = {
status: PublishStatus.WaitingForPublish,
@@ -194,7 +194,7 @@ export class PublishingService implements OnModuleDestroy {
userId: string
accountId?: string
accountType?: AccountType
- time?: [Date, Date]
+ time?: [Date?, Date?, ...unknown[]]
}): Promise<PublishTask[]> {
const filters: RootFilterQuery<PublishTask> = {
status: PublishStatus.PUBLISHING,
diff --git a/project/backend/apps/aitoearn-channel/src/libs/aws-s3/common.ts b/project/backend/apps/aitoearn-channel/src/libs/aws-s3/common.ts
deleted file mode 100644
index 95e8a0bb..00000000
--- a/project/backend/apps/aitoearn-channel/src/libs/aws-s3/common.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-export interface S3ModuleOptions {
- region: string
- accessKeyId: string
- secretAccessKey: string
- bucketName: string
-}
-
-export interface S3ModuleAsyncOptions {
- useFactory: (...args: any[]) => Promise<S3ModuleOptions> | S3ModuleOptions
- inject?: any[]
- imports?: any[]
-}
diff --git a/project/backend/apps/aitoearn-channel/src/libs/aws-s3/s3.config.ts b/project/backend/apps/aitoearn-channel/src/libs/aws-s3/s3.config.ts
deleted file mode 100644
index 880e7f57..00000000
--- a/project/backend/apps/aitoearn-channel/src/libs/aws-s3/s3.config.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import { createZodDto } from '@yikart/common'
-import { z } from 'zod'
-
-export const s3ConfigSchema = z.object({
- region: z.string().default(''),
- accessKeyId: z.string().default(''),
- secretAccessKey: z.string().default(''),
- bucketName: z.string().default(''),
- hostUrl: z.string().default(''),
-})
-
-export class S3Config extends createZodDto(s3ConfigSchema) {}
diff --git a/project/backend/apps/aitoearn-channel/src/libs/aws-s3/s3.factory.ts b/project/backend/apps/aitoearn-channel/src/libs/aws-s3/s3.factory.ts
deleted file mode 100644
index 3e6d0c45..00000000
--- a/project/backend/apps/aitoearn-channel/src/libs/aws-s3/s3.factory.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import { S3Client } from '@aws-sdk/client-s3'
-import { S3ModuleOptions } from './common'
-
-export class S3Factory {
- static createS3Client(options: S3ModuleOptions): S3Client {
- return new S3Client({
- region: options.region,
- credentials: {
- accessKeyId: options.accessKeyId,
- secretAccessKey: options.secretAccessKey,
- },
- })
- }
-}
diff --git a/project/backend/apps/aitoearn-channel/src/libs/aws-s3/s3.module.ts b/project/backend/apps/aitoearn-channel/src/libs/aws-s3/s3.module.ts
deleted file mode 100644
index 175cdf51..00000000
--- a/project/backend/apps/aitoearn-channel/src/libs/aws-s3/s3.module.ts
+++ /dev/null
@@ -1,63 +0,0 @@
-import { DynamicModule, Module, Provider } from '@nestjs/common'
-import { S3ModuleAsyncOptions, S3ModuleOptions } from './common'
-import { S3Config } from './s3.config'
-import { S3Factory } from './s3.factory'
-import { S3Service } from './s3.service'
-
-@Module({})
-export class S3Module {
- // 同步配置
- static forRoot(config: S3Config): DynamicModule {
- const providers: Provider[] = [
- { provide: S3Config, useValue: config },
- {
- provide: 'S3_CONFIG',
- useValue: config,
- },
- {
- provide: S3Service,
- useFactory: (s3Config: S3Config) => {
- const service = new S3Service()
- const client = S3Factory.createS3Client(s3Config)
- service.registerClient(s3Config.bucketName, client, true)
- return service
- },
- inject: [S3Config],
- },
- ]
-
- return {
- module: S3Module,
- providers,
- exports: [S3Service],
- }
- }
-
- // 异步配置(如从 ConfigService 读取)
- static forRootAsync(options: S3ModuleAsyncOptions): DynamicModule {
- const providers: Provider[] = [
- {
- provide: 'S3_CONFIG',
- useFactory: options.useFactory,
- inject: options.inject || [],
- },
- {
- provide: S3Service,
- useFactory: (config: S3ModuleOptions) => {
- const service = new S3Service()
- const client = S3Factory.createS3Client(config)
- service.registerClient(config.bucketName, client, true)
- return service
- },
- inject: ['S3_CONFIG'],
- },
- ]
-
- return {
- module: S3Module,
- imports: options.imports || [],
- providers,
- exports: [S3Service],
- }
- }
-}
diff --git a/project/backend/apps/aitoearn-channel/src/libs/aws-s3/s3.service.ts b/project/backend/apps/aitoearn-channel/src/libs/aws-s3/s3.service.ts
deleted file mode 100644
index 7679dadc..00000000
--- a/project/backend/apps/aitoearn-channel/src/libs/aws-s3/s3.service.ts
+++ /dev/null
@@ -1,147 +0,0 @@
-import {
- CompleteMultipartUploadCommand,
- CreateMultipartUploadCommand,
- GetObjectCommand,
- PutObjectCommand,
- S3Client,
- UploadPartCommand,
- UploadPartCommandOutput,
-} from '@aws-sdk/client-s3'
-import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
-import { Injectable, Logger } from '@nestjs/common'
-import { AppException, ResponseCode } from '@yikart/common'
-
-@Injectable()
-export class S3Service {
- private readonly clients: Map<string, S3Client> = new Map()
- private defaultBucket: string
- private readonly logger = new Logger(S3Service.name)
- // 注册 S3 客户端实例
- registerClient(name: string, client: S3Client, isDefault = false) {
- this.clients.set(name, client)
- if (isDefault)
- this.defaultBucket = name
- }
-
- // 获取客户端实例
- private getClient(bucketName?: string): S3Client {
- const targetBucket = bucketName || this.defaultBucket
- const client = this.clients.get(targetBucket)
- if (!client)
- throw new Error(`S3 client for bucket ${targetBucket} not found`)
- return client
- }
-
- // 上传文件(可指定 Bucket)
- async uploadFile(
- key: string,
- file: Buffer,
- contentType = 'application/octet-stream',
- bucketName?: string,
- ) {
- const client = this.getClient(bucketName)
-
- const command = new PutObjectCommand({
- Bucket: bucketName || this.defaultBucket,
- Key: key,
- Body: file,
- ContentType: contentType,
- })
- try {
- await client.send(command)
- return { key }
- }
- catch (error) {
- this.logger.debug(error)
- throw new AppException(ResponseCode.S3UploadFailed)
- }
- }
-
- // 生成预签名 URL(可指定 Bucket)
- getFileUrl(key: string, bucketName?: string, expiresIn = 3600) {
- const client = this.getClient(bucketName)
- const command = new GetObjectCommand({
- Bucket: bucketName || this.defaultBucket,
- Key: key,
- })
- return getSignedUrl(client, command, { expiresIn })
- }
-
- /**
- * 开始分片上传
- * @param {string} bucketName - 存储桶名称
- * @param {string} key - 文件键
- * @returns {Promise<string>} - 返回上传ID
- */
- async initiateMultipartUpload(
- key: string,
- bucketName?: string,
- ): Promise<string> {
- bucketName = bucketName || this.defaultBucket
- const client = this.getClient(bucketName)
-
- const command = new CreateMultipartUploadCommand({
- Bucket: bucketName,
- Key: key,
- })
-
- const response = await client.send(command)
- return response.UploadId || ''
- }
-
- /**
- * 上传单个分片
- * @param {string} key - 文件键
- * @param {string} uploadId - 上传ID
- * @param {number} partNumber - 分片编号
- * @param {Buffer} partData - 分片数据
- * @param {string} bucketName - 存储桶名称
- * @returns {Promise<string>} - 返回ETag
- */
- async uploadPart(
- key: string,
- uploadId: string,
- partNumber: number,
- partData: Buffer,
- bucketName?: string,
- ): Promise<UploadPartCommandOutput> {
- bucketName = bucketName || this.defaultBucket
- const client = this.getClient(bucketName)
- const command = new UploadPartCommand({
- Bucket: bucketName,
- Key: key,
- UploadId: uploadId,
- PartNumber: partNumber,
- Body: partData,
- })
-
- const response = await client.send(command)
- return response
- }
-
- /**
- * 完成分片上传
- * @param {string} bucketName - 存储桶名称
- * @param {string} key - 文件键
- * @param {string} uploadId - 上传ID
- * @param {Array<{ PartNumber: number; ETag: string }>} parts - 分片列表
- */
- async completeMultipartUpload(
- key: string,
- uploadId: string,
- parts: { PartNumber: number, ETag: string }[],
- bucketName?: string,
- ): Promise<void> {
- bucketName = bucketName || this.defaultBucket
- const client = this.getClient(bucketName)
-
- const command = new CompleteMultipartUploadCommand({
- Bucket: bucketName,
- Key: key,
- UploadId: uploadId,
- MultipartUpload: { Parts: parts },
- })
-
- await client.send(command)
- }
-}
diff --git a/project/backend/apps/aitoearn-channel/src/libs/facebook/facebook.interfaces.ts b/project/backend/apps/aitoearn-channel/src/libs/facebook/facebook.interfaces.ts
index 0abf3e89..7e8b2201 100644
--- a/project/backend/apps/aitoearn-channel/src/libs/facebook/facebook.interfaces.ts
+++ b/project/backend/apps/aitoearn-channel/src/libs/facebook/facebook.interfaces.ts
@@ -216,7 +216,7 @@ export interface FacebookPublishedPostResponse {
}
export interface FacebookPostDetailRequest {
- field: string
+ fields: string
summary?: boolean
}
diff --git a/project/backend/apps/aitoearn-channel/src/libs/instagram/instagram.operations.ts b/project/backend/apps/aitoearn-channel/src/libs/instagram/instagram.operations.ts
index 5d3798bc..0049ea72 100644
--- a/project/backend/apps/aitoearn-channel/src/libs/instagram/instagram.operations.ts
+++ b/project/backend/apps/aitoearn-channel/src/libs/instagram/instagram.operations.ts
@@ -10,6 +10,7 @@ export const InstagramOperation = {
GET_ACCOUNT_INFO: 'get_account_info',
GET_USER_PROFILE: 'get_user_profile',
GET_USER_POSTS: 'get_user_posts',
+ GET_POST_DETAIL: 'get_post_detail',
FETCH_POST_COMMENTS: 'fetch_post_comments',
FETCH_COMMENT_REPLIES: 'fetch_comment_replies',
PUBLISH_COMMENT: 'publish_comment',
diff --git a/project/backend/apps/aitoearn-channel/src/libs/instagram/instagram.service.ts b/project/backend/apps/aitoearn-channel/src/libs/instagram/instagram.service.ts
index f5dede0f..5428a2e0 100644
--- a/project/backend/apps/aitoearn-channel/src/libs/instagram/instagram.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/libs/instagram/instagram.service.ts
@@ -17,6 +17,7 @@ import {
InstagramObjectInfo,
InstagramUserInfoRequest,
InstagramUserInfoResponse,
+ InstagramUserPost,
InstagramUserPostRequest,
InstagramUserPostResponse,
} from './instagram.interfaces'
@@ -289,6 +290,26 @@ export class InstagramService {
)
}
+ async getPostDetail(
+ accessToken: string,
+ postId: string,
+ query: InstagramUserPostRequest,
+ ): Promise<InstagramUserPost> {
+ const url = `${this.apiBaseUrl}/${postId}`
+ const config: AxiosRequestConfig = {
+ method: 'GET',
+ headers: {
+ Authorization: `Bearer ${accessToken}`,
+ },
+ params: query,
+ }
+ return await this.request<InstagramUserPost>(
+ url,
+ config,
+ { operation: InstagramOperation.GET_POST_DETAIL },
+ )
+ }
+
async fetchPostComments(
accessToken: string,
postId: string,
diff --git a/project/backend/apps/aitoearn-channel/src/libs/threads/threads.service.ts b/project/backend/apps/aitoearn-channel/src/libs/threads/threads.service.ts
index d1791311..d94571cd 100644
--- a/project/backend/apps/aitoearn-channel/src/libs/threads/threads.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/libs/threads/threads.service.ts
@@ -11,6 +11,7 @@ import {
ThreadsObjectCommentsRequest,
ThreadsObjectCommentsResponse,
ThreadsObjectInfo,
+ ThreadsPostItem,
ThreadsPostResponse,
ThreadsPostsRequest,
ThreadsPostsResponse,
@@ -190,6 +191,22 @@ export class ThreadsService {
return await this.request<ThreadsPostsResponse>(url, config, { operation: 'getAccountAllPosts' })
}
+ async getPostDetail(
+ accessToken: string,
+ postId: string,
+ query: ThreadsPostsRequest,
+ ): Promise<ThreadsPostItem> {
+ const url = `${this.apiBaseUrl}/${postId}`
+ const config: AxiosRequestConfig = {
+ method: 'GET',
+ headers: {
+ Authorization: `Bearer ${accessToken}`,
+ },
+ params: query,
+ }
+ return await this.request<ThreadsPostItem>(url, config, { operation: 'getPostDetail' })
+ }
+
async fetchObjectComments(
objectId: string,
accessToken: string,
diff --git a/project/backend/libs/aitoearn-queue/src/enums/queue-name.enum.ts b/project/backend/libs/aitoearn-queue/src/enums/queue-name.enum.ts
index c8cc9ecd..373d84be 100644
--- a/project/backend/libs/aitoearn-queue/src/enums/queue-name.enum.ts
+++ b/project/backend/libs/aitoearn-queue/src/enums/queue-name.enum.ts
@@ -20,4 +20,7 @@ export enum QueueName {
/** 评论回复任务队列 */
EngagementReplyToComment = 'engagement_reply_to_comment_task',
+
+ /** dump social media avatar queue */
+ DumpSocialMediaAvatar = 'dump_social_media_avatar',
}
diff --git a/project/backend/libs/aitoearn-queue/src/queue.service.ts b/project/backend/libs/aitoearn-queue/src/queue.service.ts
index afa91906..89dd25ec 100644
--- a/project/backend/libs/aitoearn-queue/src/queue.service.ts
+++ b/project/backend/libs/aitoearn-queue/src/queue.service.ts
@@ -34,6 +34,8 @@ export class QueueService {
private engagementTaskDistributionQueue: Queue,
@InjectQueue(QueueName.EngagementReplyToComment)
private engagementReplyToCommentQueue: Queue,
+ @InjectQueue(QueueName.DumpSocialMediaAvatar)
+ private dumpSocialMediaAvatarQueue: Queue,
) {
// 从配置中读取默认的 job options
this.defaultOptions = config.jobOptions || {
@@ -179,4 +181,11 @@ export class QueueService {
): Promise<Job<EngagementReplyToCommentData> | undefined> {
return await this.engagementReplyToCommentQueue.getJob(jobId)
}
+
+ async addDumpSocialMediaAvatarJob(data: { accountId: string }, options?: JobsOptions) {
+ return await this.dumpSocialMediaAvatarQueue.add('dump-social-avatar', data, {
+ ...this.defaultOptions,
+ ...options,
+ })
+ }
}
diff --git a/project/backend/pnpm-lock.yaml b/project/backend/pnpm-lock.yaml
index 1abb6625..48fd5f25 100644
--- a/project/backend/pnpm-lock.yaml
+++ b/project/backend/pnpm-lock.yaml
@@ -95,7 +95,7 @@ importers:
version: 5.9.3
webpack-cli:
specifier: ^5.1.4
- version: 5.1.4([email protected])
+ version: 5.1.4([email protected])
zx:
specifier: ^8.8.1
version: 8.8.5
@@ -165,6 +165,9 @@ importers:
'@yikart/aitoearn-server-client':
specifier: workspace:*
version: link:../../libs/aitoearn-server-client
+ '@yikart/aws-s3':
+ specifier: workspace:*
+ version: link:../../libs/aws-s3
'@yikart/common':
specifier: workspace:*
version: link:../../libs/common
@@ -234,6 +237,9 @@ importers:
rxjs:
specifier: ^7.8.1
version: 7.8.2
+ tslib:
+ specifier: ^2.3.0
+ version: 2.8.1
uuid:
specifier: ^11.1.0
version: 11.1.0
@@ -249,7 +255,7 @@ importers:
'@nestjs/cli':
specifier: ^11.0.0
'@nestjs/schematics':
specifier: ^11.0.0
@@ -306,7 +312,7 @@ importers:
ts-loader:
specifier: ^9.5.2
ts-node:
specifier: ^10.9.2
@@ -12547,7 +12553,7 @@ snapshots:
bullmq: 5.61.2
tslib: 2.8.1
dependencies:
'@angular-devkit/core': 19.2.15([email protected])
'@angular-devkit/schematics': 19.2.15([email protected])
@@ -12558,7 +12564,7 @@ snapshots:
chokidar: 4.0.3
cli-table3: 0.6.5
commander: 4.1.1
glob: 11.0.3
node-emoji: 1.11.0
ora: 5.4.1
@@ -12566,7 +12572,7 @@ snapshots:
tsconfig-paths: 4.2.0
tsconfig-paths-webpack-plugin: 4.2.0
typescript: 5.8.3
webpack-node-externals: 3.0.0
optionalDependencies:
'@swc/core': 1.13.21(@swc/[email protected])
@@ -12983,36 +12989,36 @@ snapshots:
'@phenomnomnominal/tsquery': 5.0.1([email protected])
ajv: 8.17.1
autoprefixer: 10.4.21([email protected])
+ babel-loader: 9.2.1(@babel/[email protected])([email protected])
browserslist: 4.27.0
- css-minimizer-webpack-plugin: 5.0.1([email protected](@swc/[email protected](@swc/[email protected]))([email protected]))
- fork-ts-checker-webpack-plugin: 7.2.13([email protected])([email protected](@swc/[email protected](@swc/[email protected]))([email protected]))
+ copy-webpack-plugin: 10.2.4([email protected])
+ css-loader: 6.11.0([email protected])
+ css-minimizer-webpack-plugin: 5.0.1([email protected])
+ fork-ts-checker-webpack-plugin: 7.2.13([email protected])([email protected])
less: 4.4.2
- license-webpack-plugin: 4.0.2([email protected](@swc/[email protected](@swc/[email protected]))([email protected]))
+ less-loader: 11.1.4([email protected])([email protected])
+ license-webpack-plugin: 4.0.2([email protected])
loader-utils: 2.0.4
- mini-css-extract-plugin: 2.4.7([email protected](@swc/[email protected](@swc/[email protected]))([email protected]))
+ mini-css-extract-plugin: 2.4.7([email protected])
parse5: 4.0.0
picocolors: 1.1.1
postcss: 8.5.6
postcss-import: 14.1.0([email protected])
+ postcss-loader: 6.2.1([email protected])([email protected])
rxjs: 7.8.2
sass: 1.93.2
sass-embedded: 1.93.2
+ source-map-loader: 5.0.0([email protected])
+ style-loader: 3.3.4([email protected])
+ terser-webpack-plugin: 5.3.14(@swc/[email protected](@swc/[email protected]))([email protected])
tsconfig-paths-webpack-plugin: 4.0.0
tslib: 2.8.1
+ webpack-dev-server: 5.2.2([email protected])([email protected])
webpack-node-externals: 3.0.0
- webpack-subresource-integrity: 5.1.0([email protected](@swc/[email protected](@swc/[email protected]))([email protected]))
+ webpack-subresource-integrity: 5.1.0([email protected])
transitivePeerDependencies:
- '@babel/traverse'
- '@parcel/css'
@@ -14253,20 +14259,20 @@ snapshots:
'@webassemblyjs/ast': 1.14.1
'@xtuc/long': 4.2.2
dependencies:
- webpack-cli: 5.1.4([email protected])
+ webpack-cli: 5.1.4([email protected])
dependencies:
- webpack-cli: 5.1.4([email protected])
+ webpack-cli: 5.1.4([email protected])
dependencies:
- webpack-cli: 5.1.4([email protected])
+ webpack-cli: 5.1.4([email protected])
'@xtuc/[email protected]': {}
@@ -14582,7 +14588,7 @@ snapshots:
transitivePeerDependencies:
- supports-color
dependencies:
'@babel/core': 7.28.5
find-cache-dir: 4.0.0
@@ -15167,7 +15173,7 @@ snapshots:
dependencies:
fast-glob: 3.3.3
glob-parent: 6.0.2
@@ -15262,7 +15268,7 @@ snapshots:
dependencies:
postcss: 8.5.6
dependencies:
icss-utils: 5.1.0([email protected])
postcss: 8.5.6
@@ -15275,7 +15281,7 @@ snapshots:
optionalDependencies:
dependencies:
'@jridgewell/trace-mapping': 0.3.31
cssnano: 6.1.2([email protected])
@@ -16493,7 +16499,7 @@ snapshots:
cross-spawn: 7.0.6
signal-exit: 4.1.0
dependencies:
'@babel/code-frame': 7.27.1
chalk: 4.1.2
@@ -16510,7 +16516,7 @@ snapshots:
typescript: 5.9.3
dependencies:
'@babel/code-frame': 7.27.1
chalk: 4.1.2
@@ -16525,7 +16531,7 @@ snapshots:
semver: 7.7.3
tapable: 2.3.0
typescript: 5.8.3
dependencies:
@@ -18114,7 +18120,7 @@ snapshots:
optional: true
dependencies:
less: 4.4.2
@@ -18163,7 +18169,7 @@ snapshots:
optional: true
dependencies:
webpack-sources: 3.3.3
optionalDependencies:
@@ -18716,7 +18722,7 @@ snapshots:
dependencies:
schema-utils: 4.3.3
@@ -19900,7 +19906,7 @@ snapshots:
read-cache: 1.0.0
resolve: 1.22.11
dependencies:
cosmiconfig: 7.1.0
klona: 2.0.6
@@ -20545,7 +20551,7 @@ snapshots:
sass-embedded-win32-arm64: 1.93.2
sass-embedded-win32-x64: 1.93.2
dependencies:
neo-async: 2.6.2
optionalDependencies:
@@ -20795,7 +20801,7 @@ snapshots:
dependencies:
iconv-lite: 0.6.3
source-map-js: 1.2.1
@@ -20989,7 +20995,7 @@ snapshots:
dependencies:
'@tokenizer/token': 0.3.0
dependencies:
@@ -21082,29 +21088,18 @@ snapshots:
transitivePeerDependencies:
- supports-color
dependencies:
'@jridgewell/trace-mapping': 0.3.31
jest-worker: 27.5.1
schema-utils: 4.3.3
serialize-javascript: 6.0.2
terser: 5.44.0
optionalDependencies:
'@swc/core': 1.13.21(@swc/[email protected])
- dependencies:
- '@jridgewell/trace-mapping': 0.3.31
- jest-worker: 27.5.1
- schema-utils: 4.3.3
- serialize-javascript: 6.0.2
- terser: 5.44.0
- optionalDependencies:
- '@swc/core': 1.13.21(@swc/[email protected])
-
dependencies:
'@jridgewell/trace-mapping': 0.3.31
jest-worker: 27.5.1
@@ -21230,7 +21225,7 @@ snapshots:
babel-jest: 30.2.0(@babel/[email protected])
jest-util: 30.2.0
dependencies:
chalk: 4.1.2
enhanced-resolve: 5.18.3
@@ -21238,9 +21233,9 @@ snapshots:
semver: 7.7.3
source-map: 0.7.6
typescript: 5.9.3
dependencies:
chalk: 4.1.2
enhanced-resolve: 5.18.3
@@ -21588,12 +21583,12 @@ snapshots:
dependencies:
'@discoveryjs/json-ext': 0.5.7
- '@webpack-cli/configtest': 2.1.1([email protected])([email protected])
- '@webpack-cli/info': 2.0.2([email protected])([email protected])
- '@webpack-cli/serve': 2.0.5([email protected])([email protected])
+ '@webpack-cli/configtest': 2.1.1([email protected])([email protected])
+ '@webpack-cli/info': 2.0.2([email protected])([email protected])
+ '@webpack-cli/serve': 2.0.5([email protected])([email protected])
colorette: 2.0.20
commander: 10.0.1
cross-spawn: 7.0.6
@@ -21602,10 +21597,10 @@ snapshots:
import-local: 3.2.0
interpret: 3.1.1
rechoir: 0.8.0
webpack-merge: 5.10.0
dependencies:
colorette: 2.0.20
memfs: 4.49.0
@@ -21616,7 +21611,7 @@ snapshots:
optionalDependencies:
dependencies:
'@types/bonjour': 3.5.13
'@types/connect-history-api-fallback': 1.5.4
@@ -21644,11 +21639,11 @@ snapshots:
serve-index: 1.9.1
sockjs: 0.3.24
spdy: 4.0.2
- webpack-dev-middleware: 7.4.5([email protected](@swc/[email protected](@swc/[email protected]))([email protected]))
+ webpack-dev-middleware: 7.4.5([email protected])
ws: 8.18.3
optionalDependencies:
- webpack-cli: 5.1.4([email protected])
+ webpack-cli: 5.1.4([email protected])
transitivePeerDependencies:
- bufferutil
- debug
@@ -21665,46 +21660,12 @@ snapshots:
dependencies:
typed-assert: 1.0.9
- dependencies:
- '@types/eslint-scope': 3.7.7
- '@types/estree': 1.0.8
- '@types/json-schema': 7.0.15
- '@webassemblyjs/ast': 1.14.1
- '@webassemblyjs/wasm-edit': 1.14.1
- '@webassemblyjs/wasm-parser': 1.14.1
- acorn: 8.15.0
- acorn-import-phases: 1.0.4([email protected])
- browserslist: 4.27.0
- chrome-trace-event: 1.0.4
- enhanced-resolve: 5.18.3
- es-module-lexer: 1.7.0
- eslint-scope: 5.1.1
- events: 3.3.0
- glob-to-regexp: 0.4.1
- graceful-fs: 4.2.11
- json-parse-even-better-errors: 2.3.1
- loader-runner: 4.3.1
- mime-types: 2.1.35
- neo-async: 2.6.2
- schema-utils: 4.3.3
- tapable: 2.3.0
- watchpack: 2.4.4
- webpack-sources: 3.3.3
- optionalDependencies:
- webpack-cli: 5.1.4([email protected])
- transitivePeerDependencies:
- - '@swc/core'
- - esbuild
- - uglify-js
-
dependencies:
'@types/eslint-scope': 3.7.7
'@types/estree': 1.0.8
@@ -21728,11 +21689,11 @@ snapshots:
neo-async: 2.6.2
schema-utils: 4.3.3
tapable: 2.3.0
- terser-webpack-plugin: 5.3.14(@swc/[email protected](@swc/[email protected]))([email protected])
watchpack: 2.4.4
webpack-sources: 3.3.3
optionalDependencies:
- webpack-cli: 5.1.4([email protected])
+ webpack-cli: 5.1.4([email protected])
transitivePeerDependencies:
- '@swc/core'
- esbuild
@@ -21761,11 +21722,11 @@ snapshots:
neo-async: 2.6.2
schema-utils: 4.3.3
tapable: 2.3.0
+ terser-webpack-plugin: 5.3.14(@swc/[email protected](@swc/[email protected]))([email protected])
watchpack: 2.4.4
webpack-sources: 3.3.3
optionalDependencies:
- webpack-cli: 5.1.4([email protected])
+ webpack-cli: 5.1.4([email protected])
transitivePeerDependencies:
- '@swc/core'
- esbuild