Add all operations for post resource

This commit is contained in:
Iván Ovejero 2021-01-29 18:01:17 -03:00
parent dc38273fa7
commit 2918e01c18
3 changed files with 205 additions and 89 deletions

View file

@ -13,10 +13,22 @@ export const postOperations = [
{
name: 'Create',
value: 'create',
description: 'Submit a post to a subreddit.',
},
{
name: 'Delete',
value: 'delete',
description: 'Withdraw a post from a subreddit.',
},
{
name: 'Get',
value: 'get',
description: 'Get a post from a subreddit.',
},
{
name: 'Get All',
value: 'getAll',
description: 'Get all posts from a subreddit.',
},
],
displayOptions: {
@ -177,6 +189,68 @@ export const postFields = [
},
},
// ----------------------------------
// post: delete
// ----------------------------------
{
displayName: 'Post ID',
name: 'postId',
type: 'string',
required: true,
default: '',
description: 'ID of the post to delete. Found in the post URL:<br><code>/r/[subreddit_name]/comments/[post_id]/[post_title]</code>',
placeholder: 'gla7fmt',
displayOptions: {
show: {
resource: [
'post',
],
operation: [
'delete',
],
},
},
},
// ----------------------------------
// post: get
// ----------------------------------
{
displayName: 'Subreddit',
name: 'subreddit',
type: 'string',
required: true,
default: '',
description: 'The name of subreddit to retrieve the post from.',
displayOptions: {
show: {
resource: [
'post',
],
operation: [
'get',
],
},
},
},
{
displayName: 'Post ID',
name: 'postId',
type: 'string',
required: true,
default: '',
description: 'ID of the post to retrieve. Found in the post URL:<br><code>/r/[subreddit_name]/comments/[post_id]/[post_title]</code>',
placeholder: 'l0me7x',
displayOptions: {
show: {
resource: [
'post',
],
operation: [
'get',
],
},
},
},
// ----------------------------------
// post: getAll
// ----------------------------------
{
@ -197,42 +271,6 @@ export const postFields = [
},
},
},
{
displayName: 'Content',
name: 'content',
type: 'options',
required: true,
default: 'top',
description: 'Content of the posts to retrieve.',
options: [
{
name: 'Top Posts',
value: 'top',
},
{
name: 'Hot Posts',
value: 'hot',
},
{
name: 'New Posts',
value: 'new',
},
{
name: 'Rising Posts',
value: 'rising',
},
],
displayOptions: {
show: {
resource: [
'post',
],
operation: [
'getAll',
],
},
},
},
{
displayName: 'Return All',
name: 'returnAll',
@ -274,4 +312,49 @@ export const postFields = [
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
displayOptions: {
show: {
resource: [
'post',
],
operation: [
'getAll',
],
},
},
default: {},
placeholder: 'Add Field',
options: [
{
displayName: 'Category',
name: 'category',
type: 'options',
required: true,
default: 'top',
description: 'Category of the posts to retrieve.',
options: [
{
name: 'Top Posts',
value: 'top',
},
{
name: 'Hot Posts',
value: 'hot',
},
{
name: 'New Posts',
value: 'new',
},
{
name: 'Rising Posts',
value: 'rising',
},
],
},
],
},
] as INodeProperties[];

View file

@ -127,11 +127,95 @@ export class Reddit implements INodeType {
for (let i = 0; i < items.length; i++) {
// *********************************************************************
// post
// *********************************************************************
if (resource === 'post') {
// ----------------------------------
// post: create
// ----------------------------------
if (operation === 'create') {
// https://www.reddit.com/dev/api/#POST_api_submit
const qs: IDataObject = {
title: this.getNodeParameter('title', i),
sr: this.getNodeParameter('subreddit', i),
kind: this.getNodeParameter('kind', i),
};
qs.kind === 'self'
? qs.text = this.getNodeParameter('text', i)
: qs.url = this.getNodeParameter('url', i);
if (qs.url) {
qs.resubmit = this.getNodeParameter('resubmit', i);
}
responseData = await redditApiRequest.call(this, 'POST', 'api/submit', qs);
// ----------------------------------
// post: delete
// ----------------------------------
} else if (operation === 'delete') {
// https://www.reddit.com/dev/api/#POST_api_del
const postTypePrefix = 't3_';
const qs: IDataObject = {
id: postTypePrefix + this.getNodeParameter('postId', i),
};
await redditApiRequest.call(this, 'POST', 'api/del', qs);
responseData = { success: true };
// ----------------------------------
// post: get
// ----------------------------------
} else if (operation === 'get') {
const subreddit = this.getNodeParameter('subreddit', i);
const postId = this.getNodeParameter('postId', i) as string;
const endpoint = `r/${subreddit}/comments/${postId}.json`;
responseData = await redditApiRequest.call(this, 'GET', endpoint, {});
responseData = responseData[0].data.children[0].data;
// ----------------------------------
// post: getAll
// ----------------------------------
} else if (operation === 'getAll') {
// https://www.reddit.com/dev/api/#GET_hot
// https://www.reddit.com/dev/api/#GET_new
// https://www.reddit.com/dev/api/#GET_rising
// https://www.reddit.com/dev/api/#GET_{sort}
const subreddit = this.getNodeParameter('subreddit', i);
let endpoint = `r/${subreddit}.json`;
const { category } = this.getNodeParameter('additionalFields', i) as { category: string };
if (category) {
endpoint = `r/${subreddit}/${category}.json`;
}
responseData = await handleListing.call(this, i, endpoint);
}
// *********************************************************************
// postComment
// *********************************************************************
if (resource === 'postComment') {
} else if (resource === 'postComment') {
// ----------------------------------
// postComment: add
@ -278,57 +362,6 @@ export class Reddit implements INodeType {
}
}
// *********************************************************************
// post
// *********************************************************************
} else if (resource === 'post') {
// ----------------------------------
// post: create
// ----------------------------------
if (operation === 'create') {
// https://www.reddit.com/dev/api/#POST_api_submit
const qs: IDataObject = {
title: this.getNodeParameter('title', i),
sr: this.getNodeParameter('subreddit', i),
kind: this.getNodeParameter('kind', i),
};
qs.kind === 'self'
? qs.text = this.getNodeParameter('text', i)
: qs.url = this.getNodeParameter('url', i);
if (qs.url) {
qs.resubmit = this.getNodeParameter('resubmit', i);
}
responseData = await redditApiRequest.call(this, 'POST', 'api/submit', qs);
}
// ----------------------------------
// post: getAll
// ----------------------------------
else if (operation === 'getAll') {
// https://www.reddit.com/dev/api/#GET_hot
// https://www.reddit.com/dev/api/#GET_new
// https://www.reddit.com/dev/api/#GET_rising
// https://www.reddit.com/dev/api/#GET_{sort}
const subreddit = this.getNodeParameter('subreddit', i);
const content = this.getNodeParameter('content', i);
const endpoint = `r/${subreddit}/${content}.json`;
responseData = await handleListing.call(this, i, endpoint);
}
// *********************************************************************
// user
// *********************************************************************