|
| 1 | +import type { IExecuteFunctions, INodeProperties } from 'n8n-workflow'; |
| 2 | +import { |
| 3 | + createParameterWithDisplayOptions, |
| 4 | + postTextParameter, |
| 5 | + postCompanyUrlParameter, |
| 6 | +} from '../../shared/SharedParameters'; |
| 7 | +import { StandardLinkedApiOperation } from '../../shared/LinkedApiOperation'; |
| 8 | +import { AVAILABLE_ACTION } from '../../shared/AvailableActions'; |
| 9 | + |
| 10 | +export class CreatePost extends StandardLinkedApiOperation { |
| 11 | + operationName = AVAILABLE_ACTION.createPost; |
| 12 | + |
| 13 | + fields: INodeProperties[] = [ |
| 14 | + createParameterWithDisplayOptions(postTextParameter, this.show), |
| 15 | + createParameterWithDisplayOptions(postCompanyUrlParameter, this.show), |
| 16 | + { |
| 17 | + displayName: 'Attachments', |
| 18 | + name: 'attachments', |
| 19 | + type: 'fixedCollection', |
| 20 | + typeOptions: { |
| 21 | + multipleValues: true, |
| 22 | + maxValue: 9, |
| 23 | + }, |
| 24 | + default: {}, |
| 25 | + displayOptions: { show: this.show }, |
| 26 | + description: 'Media attachments for the post (max 9 images, or 1 video, or 1 document)', |
| 27 | + placeholder: 'Add Attachment', |
| 28 | + options: [ |
| 29 | + { |
| 30 | + name: 'attachment', |
| 31 | + displayName: 'Attachment', |
| 32 | + values: [ |
| 33 | + { |
| 34 | + displayName: 'Type', |
| 35 | + name: 'type', |
| 36 | + type: 'options', |
| 37 | + default: 'image', |
| 38 | + description: 'Type of media attachment', |
| 39 | + // eslint-disable-next-line n8n-nodes-base/node-param-options-type-unsorted-items |
| 40 | + options: [ |
| 41 | + { |
| 42 | + name: 'Image', |
| 43 | + value: 'image', |
| 44 | + description: 'Image file (JPEG, PNG, GIF, WebP; max 8MB)', |
| 45 | + }, |
| 46 | + { |
| 47 | + name: 'Video', |
| 48 | + value: 'video', |
| 49 | + description: 'Video file (MP4, MOV, WebM; max 200MB)', |
| 50 | + }, |
| 51 | + { |
| 52 | + name: 'Document', |
| 53 | + value: 'document', |
| 54 | + description: 'PDF document (max 100MB)', |
| 55 | + }, |
| 56 | + ], |
| 57 | + }, |
| 58 | + { |
| 59 | + displayName: 'URL', |
| 60 | + name: 'url', |
| 61 | + type: 'string', |
| 62 | + default: '', |
| 63 | + placeholder: 'https://example.com/file.jpg', |
| 64 | + description: 'Publicly accessible URL of the media file', |
| 65 | + }, |
| 66 | + { |
| 67 | + displayName: 'Document Name', |
| 68 | + name: 'name', |
| 69 | + type: 'string', |
| 70 | + default: '', |
| 71 | + placeholder: 'My Document', |
| 72 | + description: 'Display name for the document (required for documents)', |
| 73 | + displayOptions: { |
| 74 | + show: { |
| 75 | + type: ['document'], |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + ], |
| 80 | + }, |
| 81 | + ], |
| 82 | + }, |
| 83 | + ]; |
| 84 | + |
| 85 | + public body(context: IExecuteFunctions): Record<string, any> { |
| 86 | + const text = this.stringParameter(context, 'postText'); |
| 87 | + const companyUrl = this.stringParameter(context, 'postCompanyUrl'); |
| 88 | + const attachmentsData = context.getNodeParameter('attachments', 0, {}) as { |
| 89 | + attachment?: Array<{ type: string; url: string; name?: string }>; |
| 90 | + }; |
| 91 | + |
| 92 | + const body: Record<string, any> = { text }; |
| 93 | + |
| 94 | + if (companyUrl) { |
| 95 | + body.companyUrl = companyUrl; |
| 96 | + } |
| 97 | + |
| 98 | + if (attachmentsData.attachment && attachmentsData.attachment.length > 0) { |
| 99 | + body.attachments = attachmentsData.attachment.map((item) => { |
| 100 | + const attachment: Record<string, string> = { |
| 101 | + url: item.url, |
| 102 | + type: item.type, |
| 103 | + }; |
| 104 | + |
| 105 | + if (item.type === 'document' && item.name) { |
| 106 | + attachment.name = item.name; |
| 107 | + } |
| 108 | + |
| 109 | + return attachment; |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + return body; |
| 114 | + } |
| 115 | +} |
0 commit comments