Skip to content

Let's use AI to Earn!

fix(publishing): fix delete/update facebook post issue (#249)

wheat97efdfe945af
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/bilibili/bilibili.controller.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/bilibili/bilibili.controller.ts
index cae6bd78..175eacaa 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/bilibili/bilibili.controller.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/bilibili/bilibili.controller.ts
@@ -185,6 +185,6 @@ export class BilibiliController {
})
@Delete(':accountId/archives/:archiveId')
async deleteArchive(@Param() accountId: string, @Param() archiveId: string) {
- return await this.bilibiliService.deleteArchive(accountId, archiveId)
+ return await this.bilibiliService.deletePost(accountId, archiveId)
}
}
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 e7a35113..8dd08b37 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
@@ -21,13 +21,14 @@ import {
VideoUTypes,
} from '../../../libs/bilibili/common'
import { OAuth2Credential } from '../../../libs/database/schema/oauth2Credential.schema'
+import { PlatformBaseService } from '../base.service'
import { AuthTaskInfo } from '../common'
import { BilibiliAuthInfo } from './common'
@Injectable()
-export class BilibiliService {
- private readonly platform = AccountType.BILIBILI
- private readonly logger = new Logger(BilibiliService.name)
+export class BilibiliService extends PlatformBaseService {
+ protected override readonly platform: string = AccountType.BILIBILI
+ protected override readonly logger = new Logger(BilibiliService.name)
constructor(
private readonly redisService: RedisService,
private readonly bilibiliApiService: BilibiliApiService,
@@ -35,7 +36,9 @@ export class BilibiliService {
private readonly fileService: FileService,
@InjectModel(OAuth2Credential.name)
private OAuth2CredentialModel: Model<OAuth2Credential>,
- ) {}
+ ) {
+ super()
+ }
async getBilibiliConfig() {
return config.bilibili
@@ -574,11 +577,12 @@ export class BilibiliService {
/**
* 删除稿件
* @param accountId
- * @param resourceId
+ * @param postId
* @returns
*/
- async deleteArchive(accountId: string, resourceId: string) {
+ override async deletePost(accountId: string, postId: string): Promise<boolean> {
const accessToken = await this.getAccountAccessToken(accountId)
- return await this.bilibiliApiService.deleteArchive(accessToken, resourceId)
+ const res = await this.bilibiliApiService.deleteArchive(accessToken, postId)
+ return res.code === 0
}
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/kwai/kwai.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/kwai/kwai.service.ts
index 1649ab7b..cc19c1fe 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/kwai/kwai.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/kwai/kwai.service.ts
@@ -12,19 +12,22 @@ import { AuthTaskInfo } from '../../../core/platforms/common'
import { OAuth2Credential } from '../../../libs/database/schema/oauth2Credential.schema'
import { KwaiOAuthCredentialsResponse } from '../../../libs/kwai/kwai.interfaces'
import { KwaiApiService } from '../../../libs/kwai/kwai.service'
+import { PlatformBaseService } from '../base.service'
import { KWAI_TIME_CONSTANTS } from './constants'
@Injectable()
-export class KwaiService {
- private readonly platform = AccountType.KWAI
- private readonly logger = new Logger(KwaiService.name)
+export class KwaiService extends PlatformBaseService {
+ protected override readonly platform: string = AccountType.KWAI
+ protected override readonly logger = new Logger(KwaiService.name)
constructor(
private readonly kwaiApiService: KwaiApiService,
private readonly redisService: RedisService,
private readonly accountService: AccountService,
@InjectModel(OAuth2Credential.name)
private OAuth2CredentialModel: Model<OAuth2Credential>,
- ) { }
+ ) {
+ super()
+ }
private async getOAuth2Credential(accountId: string): Promise<KwaiOAuthCredentialsResponse | null> {
let credential = await this.redisService.getJson<KwaiOAuthCredentialsResponse>(
@@ -334,12 +337,13 @@ export class KwaiService {
return tokenInfo.refresh_token_expires_in > getCurrentTimestamp() ? 1 : 0
}
- async deleteVideo(accountId: string, videoId: string) {
+ override async deletePost(accountId: string, postId: string): Promise<boolean> {
const accountToken = await this.getAccessTokenAndRefresh(accountId)
if (accountToken === null) {
this.logger.warn(`Kwai account ${accountId} access_token is expired or invalid`)
throw new Error('kwai account access_token is expired or invalid')
}
- return await this.kwaiApiService.deleteVideo(accountToken, videoId)
+ const res = await this.kwaiApiService.deleteVideo(accountToken, postId)
+ return res === 1
}
}
diff --git a/project/backend/apps/aitoearn-channel/src/core/platforms/platforms.service.ts b/project/backend/apps/aitoearn-channel/src/core/platforms/platforms.service.ts
index 940e1c38..8e8f3037 100644
--- a/project/backend/apps/aitoearn-channel/src/core/platforms/platforms.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/platforms/platforms.service.ts
@@ -1,5 +1,5 @@
import { Inject, Injectable, Logger } from '@nestjs/common'
-import { AccountType } from '@yikart/aitoearn-server-client'
+import { AccountType } from '@yikart/common'
import { RedisService } from '@yikart/redis'
import { AccountService } from '../account/account.service'
import { PlatformBaseService } from './base.service'
diff --git a/project/backend/apps/aitoearn-channel/src/core/publishing/providers/facebook.service.ts b/project/backend/apps/aitoearn-channel/src/core/publishing/providers/facebook.service.ts
index ecd2e4da..8cf9f1fd 100644
--- a/project/backend/apps/aitoearn-channel/src/core/publishing/providers/facebook.service.ts
+++ b/project/backend/apps/aitoearn-channel/src/core/publishing/providers/facebook.service.ts
@@ -414,9 +414,13 @@ export class FacebookPublishService
throw PublishingException.nonRetryable('Invalid publish task: no postId')
}
const videoId = await this.uploadVideo(publishTask.accountId, videoUrl)
+ const message = this.generatePostMessage(publishTask)
await this.facebookService.updatePost(publishTask.accountId, publishTask.dataId, {
+ is_published: true,
+ message,
attachments: [{
media_fbid: videoId,
+ message,
}],
})
return {
@@ -440,11 +444,17 @@ export class FacebookPublishService
if (medias.length === 0) {
throw PublishingException.nonRetryable('Image upload failed')
}
- await this.facebookService.updatePost(publishTask.accountId, publishTask.dataId, {
+ const message = this.generatePostMessage(publishTask)
+ const data = {
+ message,
+ is_published: true,
attachments: medias.map(mediaId => ({
media_fbid: mediaId,
+ message,
})),
- })
+ }
+ this.logger.log(`Updating photos post: ${publishTask.dataId}, message: ${message}, data: ${JSON.stringify(data)}`)
+ await this.facebookService.updatePost(publishTask.accountId, publishTask.dataId, data)
return {
status: PublishStatus.PUBLISHED,
}
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 38a41c5e..dc7c7e6d 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
@@ -21,6 +21,7 @@ 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 { UpdatePublishedPostConsumer } from './consumers/update-published-post.consumer'
import { CredentialInvalidationService } from './credential-invalidation.service'
import { PublishingErrorHandler } from './error-handler.service'
import { MediaStagingService } from './media-staging.service'
@@ -64,6 +65,7 @@ import { EnqueuePublishingTaskScheduler } from './scheduler/enqueue-publishing-t
PublishingService,
ImmediatePublishPostConsumer,
FinalizePublishPostConsumer,
+ UpdatePublishedPostConsumer,
BilibiliPubService,
kwaiPubService,
PinterestPubService,
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 283e8ba4..ac506f1a 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
@@ -411,6 +411,7 @@ export interface MetaPostAttachment {
}
export interface UpdatePostRequest {
+ is_published?: boolean
attachments?: MetaPostAttachment[]
message?: string
}