Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

fix(youtube): keep the video id and URL on a post that fails only at the thumbnail step

Gilad Resisi23beddcd097e
diff --git a/apps/orchestrator/src/activities/post.activity.ts b/apps/orchestrator/src/activities/post.activity.ts
index 185020cd8..c94a03990 100644
--- a/apps/orchestrator/src/activities/post.activity.ts
+++ b/apps/orchestrator/src/activities/post.activity.ts
@@ -31,6 +31,7 @@ import {
import {
BadBody,
Disconnect,
+ PublishedWithError,
} from '@gitroom/nestjs-libraries/integrations/social.abstract';
// Drops fields the workflow and downstream activities never read β€” biggest wins are `error` (grows per retry) and `childrenPost` (Prisma side-loads it on every recursive row).
@@ -301,6 +302,28 @@ export class PostActivity {
);
}
+ // The post is live but a follow-up mutation failed: keep its platform
+ // id / URL on the post, then fail it as BadBody like any other error.
+ if (err instanceof PublishedWithError) {
+ try {
+ await this._postService.updateResolvedRelease(
+ err.postDbId,
+ integration.organizationId,
+ err.releaseId,
+ err.releaseURL
+ );
+ } catch (e) {
+ /**empty**/
+ }
+
+ throw new BadBody(
+ integration.providerIdentifier,
+ JSON.stringify({}),
+ Buffer.from('{}'),
+ err.message
+ );
+ }
+
throw err;
}
}
diff --git a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts
index 7a5504435..b4b883c66 100644
--- a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts
@@ -85,6 +85,20 @@ export class PostsService {
return this._postRepository.updatePost(id, postId, releaseURL);
}
+ updateResolvedRelease(
+ id: string,
+ orgId: string,
+ releaseId: string,
+ releaseURL: string
+ ) {
+ return this._postRepository.updateResolvedRelease(
+ id,
+ orgId,
+ releaseId,
+ releaseURL
+ );
+ }
+
async getMissingContent(
orgId: string,
postId: string,
diff --git a/libraries/nestjs-libraries/src/integrations/social.abstract.ts b/libraries/nestjs-libraries/src/integrations/social.abstract.ts
index d131f6007..eedc283f0 100644
--- a/libraries/nestjs-libraries/src/integrations/social.abstract.ts
+++ b/libraries/nestjs-libraries/src/integrations/social.abstract.ts
@@ -98,6 +98,28 @@ export class Disconnect extends ApplicationFailure {
}
}
+// The platform already accepted the post (it is live) but a follow-up mutation
+// on it failed (e.g. YouTube rejected the thumbnail after the upload): the post
+// still ends in ERROR, yet its platform id / URL must survive so the user can
+// reach the live post instead of re-publishing it. The post activity records
+// them and rethrows as BadBody, like Disconnect.
+export class PublishedWithError extends ApplicationFailure {
+ constructor(
+ identifier: string,
+ public readonly postDbId: string,
+ public readonly releaseId: string,
+ public readonly releaseURL: string,
+ message = ''
+ ) {
+ super(
+ truncateForTemporal(message, MAX_FAILURE_MESSAGE),
+ 'published_with_error',
+ true,
+ [{ identifier, postDbId, releaseId, releaseURL }]
+ );
+ }
+}
+
export class BadBody extends ApplicationFailure {
constructor(identifier: string, json: string, body: BodyInit, message = '') {
super(truncateForTemporal(message, MAX_FAILURE_MESSAGE), 'bad_body', true, [
diff --git a/libraries/nestjs-libraries/src/integrations/social/youtube.provider.ts b/libraries/nestjs-libraries/src/integrations/social/youtube.provider.ts
index 1b135048f..4eb7364c2 100644
--- a/libraries/nestjs-libraries/src/integrations/social/youtube.provider.ts
+++ b/libraries/nestjs-libraries/src/integrations/social/youtube.provider.ts
@@ -13,6 +13,7 @@ import { OAuth2Client } from 'google-auth-library/build/src/auth/oauth2client';
import { YoutubeSettingsDto } from '@gitroom/nestjs-libraries/dtos/posts/providers-settings/youtube.settings.dto';
import {
BadBody,
+ PublishedWithError,
RefreshToken,
SocialAbstract,
ValidityMedia,
@@ -633,6 +634,7 @@ export class YoutubeProvider extends SocialAbstract implements SocialProvider {
path,
uploadedBytes: 0,
thumbnail: settings?.thumbnail?.path || '',
+ postDbId: firstPost.id,
},
},
];
@@ -647,6 +649,7 @@ export class YoutubeProvider extends SocialAbstract implements SocialProvider {
uploadedBytes: number;
thumbnail: string;
videoId?: string;
+ postDbId?: string;
},
integration: Integration
): Promise<PendingCheckResponse> {
@@ -698,6 +701,7 @@ export class YoutubeProvider extends SocialAbstract implements SocialProvider {
uploadedBytes: number;
thumbnail: string;
videoId?: string;
+ postDbId?: string;
},
integration: Integration
): Promise<PendingCheckResponse> {
@@ -810,31 +814,49 @@ export class YoutubeProvider extends SocialAbstract implements SocialProvider {
}
}
+ const releaseURL = `https://www.youtube.com/watch?v=${videoId}`;
+
if (pendingData.thumbnail) {
const { client, youtube } = clientAndYoutube();
client.setCredentials({ access_token: accessToken });
const youtubeClient = youtube(client);
- await this.runInConcurrent(async () =>
- youtubeClient.thumbnails.set({
- videoId,
- media: {
- body: (
- await this.getSsrfSafeAxios()({
- url: pendingData.thumbnail,
- method: 'GET',
- responseType: 'stream',
- })
- ).data,
- },
- })
- );
+ try {
+ await this.runInConcurrent(async () =>
+ youtubeClient.thumbnails.set({
+ videoId,
+ media: {
+ body: (
+ await this.getSsrfSafeAxios()({
+ url: pendingData.thumbnail,
+ method: 'GET',
+ responseType: 'stream',
+ })
+ ).data,
+ },
+ })
+ );
+ } catch (err) {
+ // the video is already live: a rejected thumbnail must not lose its id
+ // and URL (a token problem is left to the refresh + retry path, the
+ // thumbnail call is idempotent)
+ if (err instanceof BadBody && pendingData.postDbId) {
+ throw new PublishedWithError(
+ this.identifier,
+ pendingData.postDbId,
+ videoId,
+ releaseURL,
+ err.message
+ );
+ }
+ throw err;
+ }
}
return {
status: 'completed',
postId: videoId,
- releaseURL: `https://www.youtube.com/watch?v=${videoId}`,
+ releaseURL,
};
}