2020-12-07 23:40:29 -08:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2020-12-07 23:40:29 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ghostApiRequest,
|
|
|
|
ghostApiRequestAllItems,
|
|
|
|
validateJSON,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
|
|
|
import {
|
|
|
|
postFields,
|
|
|
|
postOperations,
|
|
|
|
} from './PostDescription';
|
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment-timezone';
|
2020-12-07 23:40:29 -08:00
|
|
|
|
|
|
|
export class Ghost implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Ghost',
|
|
|
|
name: 'ghost',
|
2022-05-14 01:39:28 -07:00
|
|
|
icon: 'file:ghost.svg',
|
2020-12-07 23:40:29 -08:00
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume Ghost API',
|
2020-12-07 23:40:29 -08:00
|
|
|
defaults: {
|
|
|
|
name: 'Ghost',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'ghostAdminApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
source: [
|
|
|
|
'adminApi',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'ghostContentApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
source: [
|
|
|
|
'contentApi',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Source',
|
|
|
|
name: 'source',
|
|
|
|
type: 'options',
|
|
|
|
description: 'Pick where your data comes from, Content or Admin API',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Admin API',
|
|
|
|
value: 'adminApi',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Content API',
|
|
|
|
value: 'contentApi',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'contentApi',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Post',
|
|
|
|
value: 'post',
|
|
|
|
},
|
|
|
|
],
|
2022-05-14 01:39:28 -07:00
|
|
|
noDataExpression: true,
|
2020-12-07 23:40:29 -08:00
|
|
|
default: 'post',
|
2022-05-14 01:39:28 -07:00
|
|
|
description: 'The resource to operate on',
|
2020-12-07 23:40:29 -08:00
|
|
|
},
|
|
|
|
...postOperations,
|
|
|
|
...postFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the authors to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getAuthors(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const users = await ghostApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'users',
|
|
|
|
'GET',
|
|
|
|
`/admin/users`,
|
|
|
|
);
|
|
|
|
for (const user of users) {
|
|
|
|
returnData.push({
|
|
|
|
name: user.name,
|
|
|
|
value: user.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
// Get all the tags to display them to user so that he can
|
|
|
|
// select them easily
|
|
|
|
async getTags(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const tags = await ghostApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'tags',
|
|
|
|
'GET',
|
|
|
|
`/admin/tags`,
|
|
|
|
);
|
|
|
|
for (const tag of tags) {
|
|
|
|
returnData.push({
|
|
|
|
name: tag.name,
|
2022-05-14 01:39:28 -07:00
|
|
|
value: tag.name,
|
2020-12-07 23:40:29 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-12-07 23:40:29 -08:00
|
|
|
const timezone = this.getTimezone();
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
const source = this.getNodeParameter('source', 0) as string;
|
2021-07-19 23:58:54 -07:00
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
try {
|
|
|
|
if (source === 'contentApi') {
|
|
|
|
if (resource === 'post') {
|
|
|
|
if (operation === 'get') {
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const by = this.getNodeParameter('by', i) as string;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const identifier = this.getNodeParameter('identifier', i) as string;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, options);
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
let endpoint;
|
|
|
|
|
|
|
|
if (by === 'slug') {
|
|
|
|
endpoint = `/content/posts/slug/${identifier}`;
|
|
|
|
} else {
|
|
|
|
endpoint = `/content/posts/${identifier}`;
|
|
|
|
}
|
|
|
|
responseData = await ghostApiRequest.call(this, 'GET', endpoint, {}, qs);
|
|
|
|
|
|
|
|
returnData.push.apply(returnData, responseData.posts);
|
2020-12-07 23:40:29 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, options);
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
|
|
|
responseData = await ghostApiRequestAllItems.call(this, 'posts', 'GET', '/content/posts', {} ,qs);
|
|
|
|
} else {
|
|
|
|
qs.limit = this.getNodeParameter('limit', 0);
|
|
|
|
responseData = await ghostApiRequest.call(this, 'GET', '/content/posts', {}, qs);
|
|
|
|
responseData = responseData.posts;
|
|
|
|
}
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push.apply(returnData, responseData);
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-12-07 23:40:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (source === 'adminApi') {
|
|
|
|
if (resource === 'post') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
|
|
|
|
const title = this.getNodeParameter('title', i) as string;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const contentFormat = this.getNodeParameter('contentFormat', i) as string;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const content = this.getNodeParameter('content', i) as string;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const post: IDataObject = {
|
|
|
|
title,
|
|
|
|
};
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (contentFormat === 'html') {
|
|
|
|
post.html = content;
|
|
|
|
qs.source = 'html';
|
|
|
|
} else {
|
|
|
|
const mobileDoc = validateJSON(content);
|
|
|
|
if (mobileDoc === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Content must be a valid JSON');
|
|
|
|
}
|
|
|
|
post.mobiledoc = content;
|
2020-12-07 23:40:29 -08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
delete post.content;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(post, additionalFields);
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (post.published_at) {
|
|
|
|
post.published_at = moment.tz(post.published_at, timezone).utc().format();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (post.status === 'scheduled' && post.published_at === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Published at must be define when status is scheduled');
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await ghostApiRequest.call(this, 'POST', '/admin/posts', { posts: [post] }, qs);
|
|
|
|
|
|
|
|
returnData.push.apply(returnData, responseData.posts);
|
2020-12-07 23:40:29 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'delete') {
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const postId = this.getNodeParameter('postId', i) as string;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await ghostApiRequest.call(this, 'DELETE', `/admin/posts/${postId}`);
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push({ success: true });
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'get') {
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const by = this.getNodeParameter('by', i) as string;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const identifier = this.getNodeParameter('identifier', i) as string;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, options);
|
|
|
|
|
|
|
|
let endpoint;
|
|
|
|
|
|
|
|
if (by === 'slug') {
|
|
|
|
endpoint = `/admin/posts/slug/${identifier}`;
|
|
|
|
} else {
|
|
|
|
endpoint = `/admin/posts/${identifier}`;
|
|
|
|
}
|
|
|
|
responseData = await ghostApiRequest.call(this, 'GET', endpoint, {}, qs);
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push.apply(returnData, responseData.posts);
|
2020-12-07 23:40:29 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
|
|
|
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const options = this.getNodeParameter('options', i) as IDataObject;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, options);
|
|
|
|
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await ghostApiRequestAllItems.call(this, 'posts', 'GET', '/admin/posts', {} ,qs);
|
|
|
|
} else {
|
|
|
|
qs.limit = this.getNodeParameter('limit', 0);
|
|
|
|
responseData = await ghostApiRequest.call(this, 'GET', '/admin/posts', {}, qs);
|
|
|
|
responseData = responseData.posts;
|
|
|
|
}
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push.apply(returnData, responseData);
|
2020-12-07 23:40:29 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'update') {
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const postId = this.getNodeParameter('postId', i) as string;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const contentFormat = this.getNodeParameter('contentFormat', i) as string;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const post: IDataObject = {};
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (contentFormat === 'html') {
|
|
|
|
post.html = updateFields.content || '';
|
|
|
|
qs.source = 'html';
|
|
|
|
delete updateFields.content;
|
|
|
|
} else {
|
|
|
|
const mobileDoc = validateJSON(updateFields.contentJson as string || undefined);
|
|
|
|
if (mobileDoc === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Content must be a valid JSON');
|
|
|
|
}
|
|
|
|
post.mobiledoc = updateFields.contentJson;
|
|
|
|
delete updateFields.contentJson;
|
2020-12-07 23:40:29 -08:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(post, updateFields);
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const { posts } = await ghostApiRequest.call(this, 'GET', `/admin/posts/${postId}`, {}, { fields: 'id, updated_at' });
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (post.published_at) {
|
|
|
|
post.published_at = moment.tz(post.published_at, timezone).utc().format();
|
|
|
|
}
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (post.status === 'scheduled' && post.published_at === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Published at must be define when status is scheduled');
|
|
|
|
}
|
|
|
|
|
|
|
|
post.updated_at = posts[0].updated_at;
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await ghostApiRequest.call(this, 'PUT', `/admin/posts/${postId}`, { posts: [post] }, qs);
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push.apply(returnData, responseData.posts);
|
2020-12-07 23:40:29 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2020-12-07 23:40:29 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-12-07 23:40:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|