@@ -10,6 +10,8 @@ import {
1010 SocialAbstract ,
1111} from '@gitroom/nestjs-libraries/integrations/social.abstract' ;
1212import { TikTokDto } from '@gitroom/nestjs-libraries/dtos/posts/providers-settings/tiktok.dto' ;
13+ import { timer } from '@gitroom/helpers/utils/timer' ;
14+ import { Integration } from '@prisma/client' ;
1315
1416export class TiktokProvider extends SocialAbstract implements SocialProvider {
1517 identifier = 'tiktok' ;
@@ -166,13 +168,62 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {
166168 } ;
167169 }
168170
171+ private async uploadedVideoSuccess (
172+ id : string ,
173+ publishId : string ,
174+ accessToken : string
175+ ) : Promise < { url : string ; id : number } > {
176+ // eslint-disable-next-line no-constant-condition
177+ while ( true ) {
178+ const post = await (
179+ await this . fetch (
180+ 'https://open.tiktokapis.com/v2/post/publish/status/fetch/' ,
181+ {
182+ method : 'POST' ,
183+ headers : {
184+ 'Content-Type' : 'application/json; charset=UTF-8' ,
185+ Authorization : `Bearer ${ accessToken } ` ,
186+ } ,
187+ body : JSON . stringify ( {
188+ publish_id : publishId ,
189+ } ) ,
190+ }
191+ )
192+ ) . json ( ) ;
193+
194+ const { status, publicaly_available_post_id } = post . data ;
195+
196+ if ( status === 'PUBLISH_COMPLETE' ) {
197+ return {
198+ url : ! publicaly_available_post_id
199+ ? `https://www.tiktok.com/@${ id } `
200+ : `https://www.tiktok.com/@${ id } /video/` +
201+ publicaly_available_post_id ,
202+ id : ! publicaly_available_post_id ? publishId : publicaly_available_post_id ?. [ 0 ] ,
203+ } ;
204+ }
205+
206+ if ( status === 'FAILED' ) {
207+ throw new BadBody ( 'titok-error-upload' , JSON . stringify ( post ) , {
208+ // @ts -ignore
209+ postDetails,
210+ } ) ;
211+ }
212+
213+
214+ await timer ( 3000 ) ;
215+ }
216+ }
217+
169218 async post (
170219 id : string ,
171220 accessToken : string ,
172- postDetails : PostDetails < TikTokDto > [ ]
221+ postDetails : PostDetails < TikTokDto > [ ] ,
222+ integration : Integration
173223 ) : Promise < PostResponse [ ] > {
174224 try {
175225 const [ firstPost , ...comments ] = postDetails ;
226+
176227 const {
177228 data : { publish_id } ,
178229 } = await (
@@ -203,11 +254,17 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {
203254 )
204255 ) . json ( ) ;
205256
257+ const { url, id : videoId } = await this . uploadedVideoSuccess (
258+ integration . profile ! ,
259+ publish_id ,
260+ accessToken
261+ ) ;
262+
206263 return [
207264 {
208265 id : firstPost . id ,
209- releaseURL : `https://www.tiktok.com` ,
210- postId : publish_id ,
266+ releaseURL : url ,
267+ postId : String ( videoId ) ,
211268 status : 'success' ,
212269 } ,
213270 ] ;
0 commit comments