@memberjunction/actions-bizapps-social
Version:
Social Media Actions for MemberJunction - Twitter, LinkedIn, Facebook, Instagram, TikTok, YouTube, HootSuite, Buffer
189 lines • 7.77 kB
JavaScript
;
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LinkedInCreatePostAction = void 0;
const global_1 = require("@memberjunction/global");
const linkedin_base_action_1 = require("../linkedin-base.action");
const core_1 = require("@memberjunction/core");
const actions_1 = require("@memberjunction/actions");
/**
* Action to create a post on LinkedIn
*/
let LinkedInCreatePostAction = class LinkedInCreatePostAction extends linkedin_base_action_1.LinkedInBaseAction {
/**
* Create a post on LinkedIn (personal or organization)
*/
async InternalRunAction(params) {
const { Params, ContextUser } = params;
try {
// Initialize OAuth
const companyIntegrationId = this.getParamValue(Params, 'CompanyIntegrationID');
if (!await this.initializeOAuth(companyIntegrationId)) {
throw new Error('Failed to initialize OAuth connection');
}
// Extract parameters
const content = this.getParamValue(Params, 'Content');
const authorType = this.getParamValue(Params, 'AuthorType') || 'personal'; // 'personal' or 'organization'
const organizationId = this.getParamValue(Params, 'OrganizationID');
const mediaFiles = this.getParamValue(Params, 'MediaFiles');
const visibility = this.getParamValue(Params, 'Visibility') || 'PUBLIC';
const visibleToGuest = this.getParamValue(Params, 'VisibleToGuest') !== false; // Default true
// Validate required parameters
if (!content) {
throw new Error('Content is required');
}
// Determine author URN
let authorUrn;
if (authorType === 'organization') {
if (!organizationId) {
// Get first admin organization if not specified
const orgs = await this.getAdminOrganizations();
if (orgs.length === 0) {
throw new Error('No organizations found. Please specify OrganizationID.');
}
authorUrn = orgs[0].urn;
(0, core_1.LogStatus)(`Using organization: ${orgs[0].name}`);
}
else {
authorUrn = `urn:li:organization:${organizationId}`;
}
}
else {
// Personal post
authorUrn = await this.getCurrentUserUrn();
}
// Upload media if provided
let mediaUrns = [];
if (mediaFiles && Array.isArray(mediaFiles)) {
(0, core_1.LogStatus)(`Uploading ${mediaFiles.length} media files...`);
mediaUrns = await this.uploadMedia(mediaFiles);
}
// Build share data
const shareData = {
author: authorUrn,
lifecycleState: 'PUBLISHED',
specificContent: {
'com.linkedin.ugc.ShareContent': {
shareCommentary: {
text: content
},
shareMediaCategory: mediaUrns.length > 0 ? 'IMAGE' : 'NONE',
media: mediaUrns.length > 0 ? mediaUrns.map(urn => ({
status: 'READY',
media: urn
})) : undefined
}
},
visibility: {
'com.linkedin.ugc.MemberNetworkVisibility': visibility
}
};
// Add distribution settings if public
if (visibility === 'PUBLIC') {
shareData.distribution = {
linkedInDistributionTarget: {
visibleToGuest: visibleToGuest
}
};
}
// Create the post
(0, core_1.LogStatus)('Creating LinkedIn post...');
const postId = await this.createShare(shareData);
// Get the created post details
const shares = await this.getShares(authorUrn, 1);
const createdPost = shares.find(s => s.id === postId);
if (!createdPost) {
throw new Error('Failed to retrieve created post');
}
// Normalize the created post
const normalizedPost = this.normalizePost(createdPost);
// Update output parameters
const outputParams = [...Params];
const postParam = outputParams.find(p => p.Name === 'CreatedPost');
if (postParam)
postParam.Value = normalizedPost;
const postIdParam = outputParams.find(p => p.Name === 'PostID');
if (postIdParam)
postIdParam.Value = postId;
return {
Success: true,
ResultCode: 'SUCCESS',
Message: `Successfully created LinkedIn post (ID: ${postId})`,
Params: outputParams
};
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
return {
Success: false,
ResultCode: 'ERROR',
Message: `Failed to create LinkedIn post: ${errorMessage}`,
Params
};
}
}
/**
* Define the parameters this action expects
*/
get Params() {
return [
...this.commonSocialParams,
{
Name: 'Content',
Type: 'Input',
Value: null
},
{
Name: 'AuthorType',
Type: 'Input',
Value: 'personal' // 'personal' or 'organization'
},
{
Name: 'OrganizationID',
Type: 'Input',
Value: null
},
{
Name: 'MediaFiles',
Type: 'Input',
Value: null
},
{
Name: 'Visibility',
Type: 'Input',
Value: 'PUBLIC' // 'PUBLIC', 'CONNECTIONS', 'LOGGED_IN', 'CONTAINER'
},
{
Name: 'VisibleToGuest',
Type: 'Input',
Value: true
},
{
Name: 'CreatedPost',
Type: 'Output',
Value: null
},
{
Name: 'PostID',
Type: 'Output',
Value: null
}
];
}
/**
* Get action description
*/
get Description() {
return 'Creates a post on LinkedIn for personal profiles or organization pages with optional media attachments';
}
};
exports.LinkedInCreatePostAction = LinkedInCreatePostAction;
exports.LinkedInCreatePostAction = LinkedInCreatePostAction = __decorate([
(0, global_1.RegisterClass)(actions_1.BaseAction, 'LinkedInCreatePostAction')
], LinkedInCreatePostAction);
//# sourceMappingURL=create-post.action.js.map