Skip to content

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

fix: address PR review - classify oauth/analytics errors, preview playback, copy

Nevo David306b6e947f42
diff --git a/apps/frontend/src/components/new-launch/providers/tiktok/tiktok.music.tsx b/apps/frontend/src/components/new-launch/providers/tiktok/tiktok.music.tsx
index 3c395fb31..9caa44e99 100644
--- a/apps/frontend/src/components/new-launch/providers/tiktok/tiktok.music.tsx
+++ b/apps/frontend/src/components/new-launch/providers/tiktok/tiktok.music.tsx
@@ -137,7 +137,14 @@ export const TikTokMusicSelector: FC<{
}
const newPlayer = new Audio(track.previewUrl);
newPlayer.onended = () => setPlayingId('');
- newPlayer.play();
+ // A failing preview URL rejects the play() promise - reset the playing
+ // state so the row doesn't show "Stop" while nothing is playing.
+ newPlayer.play().catch(() => {
+ if (player.current === newPlayer) {
+ player.current = undefined;
+ setPlayingId('');
+ }
+ });
player.current = newPlayer;
setPlayingId(track.id);
},
diff --git a/apps/frontend/src/components/new-launch/providers/tiktok/tiktok.provider.tsx b/apps/frontend/src/components/new-launch/providers/tiktok/tiktok.provider.tsx
index 68813df24..99bb9afb8 100644
--- a/apps/frontend/src/components/new-launch/providers/tiktok/tiktok.provider.tsx
+++ b/apps/frontend/src/components/new-launch/providers/tiktok/tiktok.provider.tsx
@@ -193,7 +193,7 @@ const TikTokSettings: FC<{
{isBusiness
? t(
'tiktok_random_music_only_for_photos',
- 'This feature available only for photos, it will add a random trending track from TikTok\'s commercial music library.'
+ 'This feature is available only for photos, it adds a random trending track from TikTok\'s commercial music library.'
)
: t(
'this_feature_available_only_for_photos',
diff --git a/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.ts b/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.ts
index cd9f8de37..42bcea508 100644
--- a/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.ts
+++ b/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.ts
@@ -295,9 +295,7 @@ export class TiktokBusinessProvider
}
async refreshToken(refreshToken: string): Promise<AuthTokenDetails> {
- const {
- data: { access_token, refresh_token, open_id },
- } = await (
+ const response = await (
await fetch(`${this.baseUrl}/tt_user/oauth2/refresh_token/`, {
method: 'POST',
headers: {
@@ -312,6 +310,23 @@ export class TiktokBusinessProvider
})
).json();
+ // OAuth failures also arrive as HTTP 200 with a non-zero code and no data -
+ // without this check the destructure below throws an unclassified TypeError.
+ if (response?.code !== 0 || !response?.data?.access_token) {
+ const asString = JSON.stringify(response);
+ const handleError = this.handleErrors(asString);
+ throw new BadBody(
+ 'tiktok-business',
+ asString,
+ Buffer.from('{}'),
+ handleError?.value ||
+ response?.message ||
+ 'Could not refresh the TikTok access token'
+ );
+ }
+
+ const { access_token, refresh_token, open_id } = response.data;
+
const { display_name, profile_image, username } =
await this.fetchUserInformation(access_token, open_id);
@@ -350,9 +365,7 @@ export class TiktokBusinessProvider
codeVerifier: string;
refresh?: string;
}) {
- const {
- data: { access_token, refresh_token, scope, open_id },
- } = await (
+ const response = await (
await fetch(`${this.baseUrl}/tt_user/oauth2/token/`, {
method: 'POST',
headers: {
@@ -368,6 +381,24 @@ export class TiktokBusinessProvider
})
).json();
+ // Same HTTP 200 + non-zero code envelope as the refresh call: an expired
+ // auth_code or mismatched redirect_uri must surface TikTok's reason instead
+ // of a TypeError on the destructure.
+ if (response?.code !== 0 || !response?.data?.access_token) {
+ const asString = JSON.stringify(response);
+ const handleError = this.handleErrors(asString);
+ throw new BadBody(
+ 'tiktok-business',
+ asString,
+ Buffer.from('{}'),
+ handleError?.value ||
+ response?.message ||
+ 'Could not authenticate the TikTok business account'
+ );
+ }
+
+ const { access_token, refresh_token, scope, open_id } = response.data;
+
this.checkScopes(this.scopes, scope);
const { display_name, profile_image, username } =
@@ -884,9 +915,28 @@ export class TiktokBusinessProvider
)
).json();
+ // Same HTTP 200 + non-zero code envelope: a token error must surface as
+ // RefreshToken so the analytics/missing callers refresh and retry instead
+ // of silently showing empty data until the user reconnects.
+ this.throwIfTokenError(videoListData);
+
return videoListData?.data?.videos;
}
+ private throwIfTokenError(body: { code?: number }) {
+ if (body?.code === 0) {
+ return;
+ }
+ const asString = JSON.stringify(body);
+ const handleError = this.handleErrors(asString);
+ if (handleError?.type === 'refresh-token') {
+ throw new RefreshToken('tiktok-business', asString, '{}', handleError.value);
+ }
+ if (handleError?.type === 'disconnect') {
+ throw new Disconnect('tiktok-business', asString, '{}', handleError.value);
+ }
+ }
+
async analytics(
id: string,
accessToken: string,
@@ -897,7 +947,7 @@ export class TiktokBusinessProvider
try {
// Real-time account stats (followers, following, likes, videos)
const userStatsData = await (
- await fetch(
+ await this.fetch(
`${this.baseUrl}/business/get/?business_id=${encodeURIComponent(
id
)}&fields=${encodeURIComponent(
@@ -917,6 +967,8 @@ export class TiktokBusinessProvider
)
).json();
+ this.throwIfTokenError(userStatsData);
+
const userStats = userStatsData?.data;
const result: AnalyticsData[] = [];
@@ -1004,6 +1056,11 @@ export class TiktokBusinessProvider
return result;
} catch (err) {
+ // The callers catch RefreshToken and retry with a refreshed token -
+ // swallowing it here would leave the panel empty until a manual reconnect.
+ if (err instanceof RefreshToken || err instanceof Disconnect) {
+ throw err;
+ }
console.error('Error fetching TikTok Business analytics:', err);
return [];
}
@@ -1028,6 +1085,9 @@ export class TiktokBusinessProvider
url: v.thumbnail_url,
}));
} catch (err) {
+ if (err instanceof RefreshToken || err instanceof Disconnect) {
+ throw err;
+ }
console.error('Error fetching TikTok Business missing content:', err);
return [];
}
@@ -1046,7 +1106,7 @@ export class TiktokBusinessProvider
// post id first.
if (postId.indexOf('_pub_url') > -1) {
const post = await (
- await fetch(
+ await this.fetch(
`${this.baseUrl}/business/publish/status/?business_id=${encodeURIComponent(
integrationId
)}&publish_id=${encodeURIComponent(postId)}`,
@@ -1059,6 +1119,8 @@ export class TiktokBusinessProvider
)
).json();
+ this.throwIfTokenError(post);
+
if (!post?.data?.post_ids?.[0]) {
return [];
}
@@ -1068,7 +1130,7 @@ export class TiktokBusinessProvider
try {
const videoQueryData = await (
- await fetch(
+ await this.fetch(
`${this.baseUrl}/business/video/list/?business_id=${encodeURIComponent(
integrationId
)}&fields=${encodeURIComponent(
@@ -1085,6 +1147,8 @@ export class TiktokBusinessProvider
)
).json();
+ this.throwIfTokenError(videoQueryData);
+
const video = videoQueryData?.data?.videos?.[0];
if (!video) {
@@ -1127,6 +1191,9 @@ export class TiktokBusinessProvider
return result;
} catch (err) {
+ if (err instanceof RefreshToken || err instanceof Disconnect) {
+ throw err;
+ }
console.error('Error fetching TikTok Business post analytics:', err);
return [];
}