UNPKG

@memberjunction/actions-bizapps-social

Version:

Social Media Actions for MemberJunction - Twitter, LinkedIn, Facebook, Instagram, TikTok, YouTube, HootSuite, Buffer

138 lines 6.48 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import { RegisterClass } from '@memberjunction/global'; import { BufferBaseAction } from '../buffer-base.action.js'; import { BaseAction } from '@memberjunction/actions'; function resolveShareMode(postNow, addToTop, scheduledTime) { if (postNow) return 'shareNow'; if (addToTop) return 'shareNext'; if (scheduledTime) return 'customScheduled'; return 'addToQueue'; } /** * Builds the assets input from image/video URLs and link params. */ function buildAssetsInput(imageUrls, videoUrls, mediaLink, mediaDescription) { const assets = {}; let hasAssets = false; if (imageUrls?.length) { assets.images = imageUrls.map((url) => ({ url })); hasAssets = true; } if (videoUrls?.length) { assets.videos = videoUrls.map((url) => ({ url })); hasAssets = true; } if (mediaLink) { assets.link = { url: mediaLink, description: mediaDescription || undefined }; hasAssets = true; } return hasAssets ? assets : undefined; } function summarizePost(post) { return { id: post.id, channelId: post.channelId, channelService: post.channelService, status: post.status, scheduledAt: post.dueAt, text: post.text, }; } /** * Creates a new post in Buffer via the GraphQL createPost mutation. * * The new API accepts one channelId per mutation call. To post to multiple * channels, pass an array of ChannelIDs and a separate createPost call is * made for each. */ let BufferCreatePostAction = class BufferCreatePostAction extends BufferBaseAction { async InternalRunAction(params) { const { Params } = params; try { const channelIds = this.getParamValue(Params, 'ChannelIDs'); const content = this.getParamValue(Params, 'Content'); const imageUrls = this.getParamValue(Params, 'ImageURLs'); const videoUrls = this.getParamValue(Params, 'VideoURLs'); const mediaLink = this.getParamValue(Params, 'MediaLink'); const mediaDescription = this.getParamValue(Params, 'MediaDescription'); const scheduledTime = this.getParamValue(Params, 'ScheduledTime'); const postNow = this.getParamValue(Params, 'PostNow') === true; const addToTop = this.getParamValue(Params, 'AddToTop') === true; if (!channelIds?.length) throw new Error('ChannelIDs array is required with at least one channel'); if (!content && !imageUrls?.length && !videoUrls?.length && !mediaLink) { throw new Error('Content, ImageURLs, VideoURLs, or MediaLink is required'); } const authError = await this.ensureAuthenticated(params); if (authError) return authError; const mode = resolveShareMode(postNow, addToTop, scheduledTime); const assets = buildAssetsInput(imageUrls, videoUrls, mediaLink, mediaDescription); const dueAt = scheduledTime ? new Date(scheduledTime).toISOString() : undefined; const results = await Promise.allSettled(channelIds.map((channelId) => this.createBufferPost({ channelId, text: content || '', mode, dueAt, assets, }))); const succeeded = results .filter((r) => r.status === 'fulfilled') .map((r) => summarizePost(r.value)); const failed = results .filter((r) => r.status === 'rejected') .map((r) => (r.reason instanceof Error ? r.reason.message : String(r.reason))); const summary = { totalCreated: succeeded.length, totalFailed: failed.length, channels: channelIds, scheduled: mode !== 'shareNow', scheduledTime: scheduledTime || null, hasMedia: !!assets, errors: failed.length > 0 ? failed : undefined, }; this.setOutputParam(Params, 'CreatedPosts', succeeded); this.setOutputParam(Params, 'Summary', summary); if (succeeded.length === 0) { return { Success: false, ResultCode: 'CREATE_FAILED', Message: `Failed to create posts: ${failed.join('; ')}`, Params }; } const msg = failed.length > 0 ? `Created ${succeeded.length} post(s), ${failed.length} failed` : `Successfully created ${succeeded.length} Buffer post(s)`; return { Success: true, ResultCode: 'SUCCESS', Message: msg, Params }; } catch (error) { return this.buildErrorResult(error, 'create Buffer post', Params); } } get Params() { return [ ...this.bufferCommonParams, { Name: 'ChannelIDs', Type: 'Input', Value: null }, { Name: 'Content', Type: 'Input', Value: null }, { Name: 'ImageURLs', Type: 'Input', Value: null }, { Name: 'VideoURLs', Type: 'Input', Value: null }, { Name: 'MediaLink', Type: 'Input', Value: null }, { Name: 'MediaDescription', Type: 'Input', Value: null }, { Name: 'ScheduledTime', Type: 'Input', Value: null }, { Name: 'PostNow', Type: 'Input', Value: false }, { Name: 'AddToTop', Type: 'Input', Value: false }, { Name: 'CreatedPosts', Type: 'Output', Value: null }, { Name: 'Summary', Type: 'Output', Value: null }, ]; } get Description() { return 'Creates a new post in Buffer that can be scheduled or posted immediately to one or more channels'; } }; BufferCreatePostAction = __decorate([ RegisterClass(BaseAction, 'BufferCreatePostAction') ], BufferCreatePostAction); export { BufferCreatePostAction }; //# sourceMappingURL=create-post.action.js.map