π¨ The ultimate agentic social media scheduling tool π€
TypeScript nextjsopen-sourceopen-source-social-media-scheduling-toolosspostiz-cloudpostiz-hostedredisscheduling-toolsocial-media-scheduling-tooltypescript
fix(calendar): open-post icon no longer lands on a wrong or missing TikTok link
Gilad Resisi6f79545d4a26
diff --git a/apps/frontend/src/components/launches/calendar.tsx b/apps/frontend/src/components/launches/calendar.tsxindex 40a489d10..8d18d57bb 100644--- a/apps/frontend/src/components/launches/calendar.tsx+++ b/apps/frontend/src/components/launches/calendar.tsx@@ -38,6 +38,7 @@ import { useDrag, useDrop } from 'react-dnd';import { Integration, Post, State, Tags } from '@prisma/client';import { useAddProvider } from '@gitroom/frontend/components/launches/add.provider.component';import { useToaster } from '@gitroom/react/toaster/toaster';+import { timer } from '@gitroom/helpers/utils/timer';import { useUser } from '@gitroom/frontend/components/layout/user.context';import isSameOrAfter from 'dayjs/plugin/isSameOrAfter';import isSameOrBefore from 'dayjs/plugin/isSameOrBefore';@@ -269,17 +270,55 @@ const usePostActions = (onMutate?: () => void) => {return;}tab.opener = null;- let releaseURL = post.releaseURL;+ let release = {+ releaseURL: '',+ pending: true,+ unavailable: false,+ reconnect: false,+ };try {- releaseURL =- (await (await fetch(`/posts/${post.id}/release-url`)).json())- .releaseURL || releaseURL;+ release = await Promise.race([+ fetch(`/posts/${post.id}/release-url`).then((r) => r.json()),+ timer(5000).then(() => release),+ ]);} catch (e) {}+ if (release.unavailable) {+ tab.close();+ toaster.show(+ t('post_has_no_public_link', 'This post has no public link'),+ 'warning'+ );+ return;+ }+ if (release.reconnect) {+ tab.close();+ toaster.show(+ t(+ 'post_link_reconnect_channel',+ 'Reconnect this channel to open the post link'+ ),+ 'warning'+ );+ return;+ }+ // the platform has not released the post link yet, or the request+ // failed or took too long+ if (release.pending) {+ tab.close();+ toaster.show(+ t(+ 'post_link_not_ready',+ 'The post link is not available yet, please try again in a minute'+ ),+ 'warning'+ );+ return;+ }// multi-target posts (several subreddits / communities / channels)// join their URLs with commas: open the first one- tab.location.href = releaseURL.split(',')[0];+ tab.location.href = (release.releaseURL || post.releaseURL).split(',')[0];},- [fetch]+ [fetch, toaster, t]);return { editPost, deletePost, copyDebugJson, openStatistics, openMissingRelease, openPost };diff --git a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.tsindex 58c639ae1..319afb105 100644--- a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts+++ b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts@@ -162,6 +162,7 @@ export class PostsService {id: string;releaseId: string;releaseURL: string;+ settings: string;integration: Integration;}) {@@ -172,10 +173,20 @@ export class PostsService {const resolved = await integrationProvider.resolveReleaseId?.(post.integration.token,post.releaseId,- post.integration+ post.integration,+ JSON.parse(post.settings || '{}'));- if (!resolved || resolved.postId === post.releaseId) {- return { releaseId: post.releaseId, releaseURL: post.releaseURL };+ if (+ !resolved ||+ !('postId' in resolved) ||+ resolved.postId === post.releaseId+ ) {+ return {+ releaseId: post.releaseId,+ releaseURL: post.releaseURL,+ pending: !!resolved && 'pending' in resolved,+ unavailable: !!resolved && 'unavailable' in resolved,+ };}await this._postRepository.updateResolvedRelease(@@ -184,14 +195,24 @@ export class PostsService {resolved.postId,resolved.releaseURL);- return { releaseId: resolved.postId, releaseURL: resolved.releaseURL };+ return {+ releaseId: resolved.postId,+ releaseURL: resolved.releaseURL,+ pending: false,+ unavailable: false,+ };}async getReleaseURL(orgId: string,postId: string,forceRefresh = false- ): Promise<{ releaseURL: string }> {+ ): Promise<{+ releaseURL: string;+ pending?: boolean;+ unavailable?: boolean;+ reconnect?: boolean;+ }> {const post = await this._postRepository.getPostById(postId, orgId);if (!post || !post.releaseURL) {return { releaseURL: '' };@@ -207,15 +228,14 @@ export class PostsService {const getIntegration = post.integration!;- if (- dayjs(getIntegration?.tokenExpiration).isBefore(dayjs()) ||- forceRefresh- ) {+ // only refreshed once the platform rejects the token, so a post that is+ // already resolved never reaches the platform+ if (forceRefresh) {const data = await this._refreshIntegrationService.refresh(getIntegration);if (!data) {- return { releaseURL: post.releaseURL };+ return { releaseURL: post.releaseURL, reconnect: true };}const { accessToken } = data;@@ -228,21 +248,24 @@ export class PostsService {}} else {await this._integrationService.disconnectChannel(orgId, getIntegration);- return { releaseURL: post.releaseURL };+ return { releaseURL: post.releaseURL, reconnect: true };}}try {- const { releaseURL } = await this.resolveRelease(orgId, post);- return { releaseURL };+ const { releaseURL, pending, unavailable } = await this.resolveRelease(+ orgId,+ post+ );+ return { releaseURL, pending, unavailable };} catch (e) {console.log(e);- if (e instanceof RefreshToken) {+ if (e instanceof RefreshToken && !forceRefresh) {return this.getReleaseURL(orgId, postId, true);}}- return { releaseURL: post.releaseURL };+ return { releaseURL: post.releaseURL, pending: true };}async checkPostAnalytics(diff --git a/libraries/nestjs-libraries/src/integrations/social/social.integrations.interface.ts b/libraries/nestjs-libraries/src/integrations/social/social.integrations.interface.tsindex 52bf2fc43..333011016 100644--- a/libraries/nestjs-libraries/src/integrations/social/social.integrations.interface.ts+++ b/libraries/nestjs-libraries/src/integrations/social/social.integrations.interface.ts@@ -37,8 +37,14 @@ export interface IAuthenticator {resolveReleaseId?(accessToken: string,releaseId: string,- integration: Integration- ): Promise<{ postId: string; releaseURL: string } | undefined>; // Final id + URL to persist when the stored releaseId is still a publish id, undefined when nothing to resolve (yet)+ integration: Integration,+ settings: any+ ): Promise<+ | { postId: string; releaseURL: string }+ | { pending: true }+ | { unavailable: true }+ | undefined+ >; // Final id + URL to persist when the stored releaseId is still a publish id, pending when the platform has no final id yet, unavailable when the post will never get one, undefined when nothing to resolvechangeNickname?(id: string,accessToken: string,diff --git a/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.ts b/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.tsindex 875ac6a73..a57b11dc6 100644--- a/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.ts@@ -491,7 +491,7 @@ export class TiktokBusinessProviderstatus: 'completed',releaseURL: !publicPostId? `https://www.tiktok.com/@${integration.profile}`- : `https://www.tiktok.com/@${integration.profile}/video/${publicPostId}`,+ : this.postUrl(integration, pendingData.publishId, publicPostId),postId: !publicPostId ? pendingData.publishId : publicPostId,};}@@ -931,6 +931,13 @@ export class TiktokBusinessProviderreturn videoListData?.data?.videos;}+ // photo posts (p_pub_...) live under /photo/, TikTok rejects /video/ for them+ private postUrl(integration: Integration, publishId: string, postId: string) {+ return `https://www.tiktok.com/@${integration.profile}/${+ publishId.indexOf('p_pub_') === 0 ? 'photo' : 'video'+ }/${postId}`;+ }+// post_ids holds int64 ids that exceed Number.MAX_SAFE_INTEGER, so the id// must be read from the raw body instead of the parsed JSONprivate publicPostId(body: string) {@@ -1112,12 +1119,22 @@ export class TiktokBusinessProviderasync resolveReleaseId(accessToken: string,releaseId: string,- integration: Integration+ integration: Integration,+ settings: any) {if (releaseId.indexOf('_pub_') === -1) {return undefined;}+ // privacy_level only applies to photo posts here, and TikTok only gives a+ // post id to posts published for public viewership+ if (+ releaseId.indexOf('p_pub_') === 0 &&+ ['SELF_ONLY', 'MUTUAL_FOLLOW_FRIENDS'].includes(settings?.privacy_level)+ ) {+ return { unavailable: true as const };+ }+const body = await (await this.fetch(`${this.baseUrl}/business/publish/status/?business_id=${encodeURIComponent(@@ -1136,12 +1153,12 @@ export class TiktokBusinessProviderconst publicPostId = this.publicPostId(body);if (!publicPostId) {- return undefined;+ return { pending: true as const };}return {postId: publicPostId,- releaseURL: `https://www.tiktok.com/@${integration.profile}/video/${publicPostId}`,+ releaseURL: this.postUrl(integration, releaseId, publicPostId),};}diff --git a/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts b/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.tsindex 7f2e453c5..759ed88e3 100644--- a/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts@@ -497,7 +497,7 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {status: 'completed',releaseURL: !publicPostId? `https://www.tiktok.com/@${integration.profile}`- : `https://www.tiktok.com/@${integration.profile}/video/${publicPostId}`,+ : this.postUrl(integration, pendingData.publishId, publicPostId),postId: !publicPostId ? pendingData.publishId : publicPostId,};}@@ -515,6 +515,13 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {return { status: 'pending', pendingData };}+ // photo posts (p_pub_...) live under /photo/, TikTok rejects /video/ for them+ private postUrl(integration: Integration, publishId: string, postId: string) {+ return `https://www.tiktok.com/@${integration.profile}/${+ publishId.indexOf('p_pub_') === 0 ? 'photo' : 'video'+ }/${postId}`;+ }+// TikTok returns publicaly_available_post_id as an int64, which JSON.parse// rounds past 2^53 - pull the digits out of the raw body before parsing.private parsePublishStatus(raw: string) {@@ -1147,12 +1154,20 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {async resolveReleaseId(accessToken: string,releaseId: string,- integration: Integration+ integration: Integration,+ settings: any) {if (releaseId.indexOf('_pub_') === -1) {return undefined;}+ // TikTok only gives a post id to posts published for public viewership+ if (+ ['SELF_ONLY', 'MUTUAL_FOLLOW_FRIENDS'].includes(settings?.privacy_level)+ ) {+ return { unavailable: true as const };+ }+const { publicPostId } = this.parsePublishStatus(await (await this.fetch(@@ -1172,12 +1187,12 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {);if (!publicPostId) {- return undefined;+ return { pending: true as const };}return {postId: publicPostId,- releaseURL: `https://www.tiktok.com/@${integration.profile}/video/${publicPostId}`,+ releaseURL: this.postUrl(integration, releaseId, publicPostId),};}