π¨ The ultimate agentic social media scheduling tool π€
TypeScript nextjsopen-sourceopen-source-social-media-scheduling-toolosspostiz-cloudpostiz-hostedredisscheduling-toolsocial-media-scheduling-tooltypescript
feat(tiktok): connect UPLOAD drafts to the published post from the open-post icon
Gilad Resisi6eed47e0af66
diff --git a/libraries/nestjs-libraries/src/database/prisma/posts/posts.repository.ts b/libraries/nestjs-libraries/src/database/prisma/posts/posts.repository.tsindex 70eddc502..eedb18687 100644--- a/libraries/nestjs-libraries/src/database/prisma/posts/posts.repository.ts+++ b/libraries/nestjs-libraries/src/database/prisma/posts/posts.repository.ts@@ -451,7 +451,12 @@ export class PostsRepository {});}- updateReleaseId(id: string, orgId: string, releaseId: string) {+ updateReleaseId(+ id: string,+ orgId: string,+ releaseId: string,+ releaseURL?: string+ ) {return this._post.model.post.update({where: {id,@@ -460,6 +465,7 @@ export class PostsRepository {},data: {releaseId: String(releaseId),+ ...(releaseURL ? { releaseURL } : {}),},});}diff --git a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.tsindex 319afb105..5dc2fe559 100644--- a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts+++ b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts@@ -153,7 +153,30 @@ export class PostsService {}async updateReleaseId(orgId: string, postId: string, releaseId: string) {- return this._postRepository.updateReleaseId(postId, orgId, releaseId);+ const post = await this._postRepository.getPostById(postId, orgId);++ // the URL is a bonus: connecting must not fail when it cannot be fetched+ let releaseURL: string | undefined;+ if (post?.releaseId === 'missing') {+ try {+ releaseURL = await this._integrationManager+ .getSocialIntegration(post.integration.providerIdentifier)+ .releaseUrl?.(+ post.integration.token,+ String(releaseId),+ post.integration+ );+ } catch (e) {+ console.log(e);+ }+ }++ return this._postRepository.updateReleaseId(+ postId,+ orgId,+ releaseId,+ releaseURL+ );}async resolveRelease(@@ -174,7 +197,8 @@ export class PostsService {post.integration.token,post.releaseId,post.integration,- JSON.parse(post.settings || '{}')+ JSON.parse(post.settings || '{}'),+ post.releaseURL);if (!resolved ||diff --git a/libraries/nestjs-libraries/src/integrations/social/social.integrations.interface.ts b/libraries/nestjs-libraries/src/integrations/social/social.integrations.interface.tsindex 333011016..298d8d5e2 100644--- a/libraries/nestjs-libraries/src/integrations/social/social.integrations.interface.ts+++ b/libraries/nestjs-libraries/src/integrations/social/social.integrations.interface.ts@@ -38,13 +38,19 @@ export interface IAuthenticator {accessToken: string,releaseId: string,integration: Integration,- settings: any+ settings: any,+ releaseURL: string): 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 resolve+ releaseUrl?(+ accessToken: string,+ releaseId: string,+ integration: Integration+ ): Promise<string | undefined>; // URL of a post the user connected by handchangeNickname?(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 a57b11dc6..31c672fbe 100644--- a/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/tiktok.business.provider.ts@@ -474,9 +474,11 @@ export class TiktokBusinessProviderconst { status, reason } = post?.data || {};if (status === 'SEND_TO_USER_INBOX') {+ // the id stays 'missing' until the user publishes the draft - the+ // share id rides in the URL fragment so resolveReleaseId can ask laterreturn {status: 'completed',- releaseURL: 'https://www.tiktok.com/messages?lang=en',+ releaseURL: `https://www.tiktok.com/messages?lang=en#${pendingData.publishId}`,postId: 'missing',};}@@ -931,10 +933,11 @@ export class TiktokBusinessProviderreturn videoListData?.data?.videos;}- // photo posts (p_pub_...) live under /photo/, TikTok rejects /video/ for them+ // photo posts (p_pub_... / p_inbox_...) live under /photo/, TikTok rejects+ // /video/ for themprivate postUrl(integration: Integration, publishId: string, postId: string) {return `https://www.tiktok.com/@${integration.profile}/${- publishId.indexOf('p_pub_') === 0 ? 'photo' : 'video'+ publishId.indexOf('p_') === 0 ? 'photo' : 'video'}/${postId}`;}@@ -1120,16 +1123,26 @@ export class TiktokBusinessProvideraccessToken: string,releaseId: string,integration: Integration,- settings: any+ settings: any,+ releaseURL: string) {- if (releaseId.indexOf('_pub_') === -1) {+ // a draft sent to the inbox (UPLOAD) is stored as 'missing' with its+ // share id in the URL fragment+ const draft = releaseId === 'missing';+ const publishId = draft ? releaseURL?.split('#')[1] || '' : releaseId;+ if (+ publishId.indexOf('_pub_') === -1 &&+ publishId.indexOf('_inbox_') === -1+ ) {return undefined;}- // privacy_level only applies to photo posts here, and TikTok only gives a- // post id to posts published for public viewership+ // privacy_level only applies to photo posts here (and not to a draft, the+ // user picks it in the app), and TikTok only gives a post id to posts+ // published for public viewershipif (- releaseId.indexOf('p_pub_') === 0 &&+ !draft &&+ publishId.indexOf('p_pub_') === 0 &&['SELF_ONLY', 'MUTUAL_FOLLOW_FRIENDS'].includes(settings?.privacy_level)) {return { unavailable: true as const };@@ -1139,7 +1152,7 @@ export class TiktokBusinessProviderawait this.fetch(`${this.baseUrl}/business/publish/status/?business_id=${encodeURIComponent(integration.internalId- )}&publish_id=${encodeURIComponent(releaseId)}`,+ )}&publish_id=${encodeURIComponent(publishId)}`,{method: 'GET',headers: {@@ -1153,12 +1166,14 @@ export class TiktokBusinessProviderconst publicPostId = this.publicPostId(body);if (!publicPostId) {- return { pending: true as const };+ // a draft with no public post (not published yet, or published as+ // non-public) keeps opening the inbox instead of asking to retry+ return draft ? undefined : { pending: true as const };}return {postId: publicPostId,- releaseURL: this.postUrl(integration, releaseId, publicPostId),+ releaseURL: this.postUrl(integration, publishId, publicPostId),};}diff --git a/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts b/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.tsindex 759ed88e3..bae187233 100644--- a/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts@@ -483,9 +483,11 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {const { status } = post?.data || {};if (status === 'SEND_TO_USER_INBOX') {+ // the id stays 'missing' until the user publishes the draft - the+ // publish id rides in the URL fragment so resolveReleaseId can ask laterreturn {status: 'completed',- releaseURL: 'https://www.tiktok.com/messages?lang=en',+ releaseURL: `https://www.tiktok.com/messages?lang=en#${pendingData.publishId}`,postId: 'missing',};}@@ -515,10 +517,11 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {return { status: 'pending', pendingData };}- // photo posts (p_pub_...) live under /photo/, TikTok rejects /video/ for them+ // photo posts (p_pub_... / p_inbox_...) live under /photo/, TikTok rejects+ // /video/ for themprivate postUrl(integration: Integration, publishId: string, postId: string) {return `https://www.tiktok.com/@${integration.profile}/${- publishId.indexOf('p_pub_') === 0 ? 'photo' : 'video'+ publishId.indexOf('p_') === 0 ? 'photo' : 'video'}/${postId}`;}@@ -1155,14 +1158,24 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {accessToken: string,releaseId: string,integration: Integration,- settings: any+ settings: any,+ releaseURL: string) {- if (releaseId.indexOf('_pub_') === -1) {+ // a draft sent to the inbox (UPLOAD) is stored as 'missing' with its+ // publish id in the URL fragment+ const draft = releaseId === 'missing';+ const publishId = draft ? releaseURL?.split('#')[1] || '' : releaseId;+ if (+ publishId.indexOf('_pub_') === -1 &&+ publishId.indexOf('_inbox_') === -1+ ) {return undefined;}// TikTok only gives a post id to posts published for public viewership+ // (a draft ignores the privacy setting, the user picks it in the app)if (+ !draft &&['SELF_ONLY', 'MUTUAL_FOLLOW_FRIENDS'].includes(settings?.privacy_level)) {return { unavailable: true as const };@@ -1179,7 +1192,7 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {Authorization: `Bearer ${accessToken}`,},body: JSON.stringify({- publish_id: releaseId,+ publish_id: publishId,}),})@@ -1187,15 +1200,42 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {);if (!publicPostId) {- return { pending: true as const };+ // a draft with no public post (not published yet, or published as+ // non-public) keeps opening the inbox instead of asking to retry+ return draft ? undefined : { pending: true as const };}return {postId: publicPostId,- releaseURL: this.postUrl(integration, releaseId, publicPostId),+ releaseURL: this.postUrl(integration, publishId, publicPostId),};}+ async releaseUrl(accessToken: string, releaseId: string) {+ const data = await (+ await this.fetch(+ 'https://open.tiktokapis.com/v2/video/query/?fields=id,share_url',+ {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ Authorization: `Bearer ${accessToken}`,+ },+ body: JSON.stringify({+ filters: {+ video_ids: [releaseId],+ },+ }),+ }+ )+ ).json();++ // share_url carries tracking params (the app's client key among them)+ return data?.data?.videos?.[0]?.share_url?.split('?')[0] as+ | string+ | undefined;+ }+async postAnalytics(integrationId: string,accessToken: string,