From 2918e01c1859563c81e02e3e91b7af1211f7fb6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Fri, 29 Jan 2021 18:01:17 -0300 Subject: [PATCH] Add all operations for post resource --- .../nodes/Reddit/PostCommentDescription.ts | 2 +- .../nodes/Reddit/PostDescription.ts | 155 ++++++++++++++---- .../nodes-base/nodes/Reddit/Reddit.node.ts | 137 ++++++++++------ 3 files changed, 205 insertions(+), 89 deletions(-) diff --git a/packages/nodes-base/nodes/Reddit/PostCommentDescription.ts b/packages/nodes-base/nodes/Reddit/PostCommentDescription.ts index 81d9ebeed2..00fa59fadb 100644 --- a/packages/nodes-base/nodes/Reddit/PostCommentDescription.ts +++ b/packages/nodes-base/nodes/Reddit/PostCommentDescription.ts @@ -144,7 +144,7 @@ export const postCommentFields = [ }, }, }, - // ---------------------------------- + // ---------------------------------- // postComment: reply // ---------------------------------- { diff --git a/packages/nodes-base/nodes/Reddit/PostDescription.ts b/packages/nodes-base/nodes/Reddit/PostDescription.ts index 2b117ec33e..fdd35e6a13 100644 --- a/packages/nodes-base/nodes/Reddit/PostDescription.ts +++ b/packages/nodes-base/nodes/Reddit/PostDescription.ts @@ -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:
/r/[subreddit_name]/comments/[post_id]/[post_title]', + 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:
/r/[subreddit_name]/comments/[post_id]/[post_title]', + 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[]; diff --git a/packages/nodes-base/nodes/Reddit/Reddit.node.ts b/packages/nodes-base/nodes/Reddit/Reddit.node.ts index 9a82a95601..769d593853 100644 --- a/packages/nodes-base/nodes/Reddit/Reddit.node.ts +++ b/packages/nodes-base/nodes/Reddit/Reddit.node.ts @@ -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 // *********************************************************************