From 5556bf14a1068ca5f66c02b055a2ede93155d03a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Sun, 17 Jan 2021 19:36:30 -0300 Subject: [PATCH] Add subreddit resource --- .../nodes/Reddit/GenericFunctions.ts | 22 +++ .../nodes/Reddit/ListingDescription.ts | 158 ------------------ .../nodes-base/nodes/Reddit/Reddit.node.ts | 84 +++++++--- 3 files changed, 79 insertions(+), 185 deletions(-) delete mode 100644 packages/nodes-base/nodes/Reddit/ListingDescription.ts diff --git a/packages/nodes-base/nodes/Reddit/GenericFunctions.ts b/packages/nodes-base/nodes/Reddit/GenericFunctions.ts index b546084677..876b4cb6a8 100644 --- a/packages/nodes-base/nodes/Reddit/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Reddit/GenericFunctions.ts @@ -85,3 +85,25 @@ export async function redditApiRequestAllItems( return returnData; } + +/** + * Handles a large Reddit listing by returning all items or up to a limit. + */ +export async function handleListing( + this: IExecuteFunctions, + i: number, + endpoint: string, +): Promise { // tslint:disable-line:no-any + let responseData; + + const returnAll = this.getNodeParameter('returnAll', i); + if (returnAll) { + return await redditApiRequestAllItems.call(this, 'GET', endpoint, {}, {}, true); + } + + const qs: IDataObject = { limit: this.getNodeParameter('limit', i) }; + responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, qs, {}, true); + responseData = responseData.splice(0, qs.limit); + + return responseData; +} \ No newline at end of file diff --git a/packages/nodes-base/nodes/Reddit/ListingDescription.ts b/packages/nodes-base/nodes/Reddit/ListingDescription.ts deleted file mode 100644 index 26ab1d4834..0000000000 --- a/packages/nodes-base/nodes/Reddit/ListingDescription.ts +++ /dev/null @@ -1,158 +0,0 @@ -import { - INodeProperties, -} from 'n8n-workflow'; - -export const listingOperations = [ - { - displayName: 'Operation', - name: 'operation', - type: 'options', - default: 'get', - description: 'Operation to perform', - options: [ - { - name: 'Get', - value: 'get', - }, - ], - displayOptions: { - show: { - resource: [ - 'listing', - ], - }, - }, - }, -] as INodeProperties[]; - -export const listingFields = [ - { - displayName: 'Type', - name: 'type', - type: 'options', - required: true, - default: 'trending', - description: 'Type of listing to retrieve', - options: [ - { - name: 'Trending', - value: 'trending', - description: 'Currently trending subreddits', - }, - { - name: 'Best', - value: 'best', - description: 'Top posts in all of Reddit', - }, - { - name: 'Top', - value: 'top', - description: 'Top posts in a specifc subreddit', - }, - { - name: 'Hot', - value: 'hot', - description: 'Hot posts in a specifc subreddit', - }, - { - name: 'New', - value: 'new', - description: 'New posts in a specifc subreddit', - }, - { - name: 'Rising', - value: 'rising', - description: 'Rising posts in a specifc subreddit', - }, - ], - displayOptions: { - show: { - resource: [ - 'listing', - ], - operation: [ - 'get', - ], - }, - }, - }, - { - displayName: 'Return All', - name: 'returnAll', - type: 'boolean', - default: false, - description: 'Return all results', - displayOptions: { - show: { - resource: [ - 'listing', - ], - operation: [ - 'get', - ], - type: [ - 'best', - 'top', - 'hot', - 'new', - 'rising', - ], - }, - }, - }, - { - displayName: 'Limit', - name: 'limit', - type: 'number', - default: 5, - description: 'The number of results to return', - typeOptions: { - minValue: 1, - maxValue: 100, - }, - displayOptions: { - show: { - resource: [ - 'listing', - ], - operation: [ - 'get', - ], - type: [ - 'best', - 'top', - 'hot', - 'new', - 'rising', - ], - returnAll: [ - false, - ], - }, - }, - }, - { - displayName: 'Subreddit', - name: 'subreddit', - type: 'string', - required: true, - default: '', - description: 'The subreddit to retrieve the listing from', - displayOptions: { - show: { - resource: [ - 'listing', - ], - operation: [ - 'get', - ], - type: [ - 'top', - 'hot', - 'new', - 'rising', - ], - }, - }, - }, -] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/Reddit/Reddit.node.ts b/packages/nodes-base/nodes/Reddit/Reddit.node.ts index 92b0246af1..4f6a5e1ec8 100644 --- a/packages/nodes-base/nodes/Reddit/Reddit.node.ts +++ b/packages/nodes-base/nodes/Reddit/Reddit.node.ts @@ -10,14 +10,15 @@ import { } from 'n8n-workflow'; import { + handleListing, redditApiRequest, redditApiRequestAllItems, } from './GenericFunctions'; import { - listingFields, - listingOperations, -} from './ListingDescription'; + allRedditFields, + allRedditOperations, +} from './AllRedditDescription'; import { myAccountFields, @@ -29,6 +30,11 @@ import { submissionOperations, } from './SubmissionDescription'; +import { + subredditFields, + subredditOperations, +} from './SubredditDescription'; + export class Reddit implements INodeType { description: INodeTypeDescription = { displayName: 'Reddit', @@ -56,6 +62,10 @@ export class Reddit implements INodeType { name: 'resource', type: 'options', options: [ + { + name: 'All Reddit', + value: 'allReddit', + }, { name: 'My Account', value: 'myAccount', @@ -65,14 +75,18 @@ export class Reddit implements INodeType { value: 'submission', }, { - name: 'Listing', - value: 'listing', + name: 'Subreddit', + value: 'subreddit', }, ], default: 'myAccount', description: 'Resource to consume', }, + // allReddit + ...allRedditOperations, + ...allRedditFields, + // myAccount ...myAccountOperations, ...myAccountFields, @@ -81,9 +95,9 @@ export class Reddit implements INodeType { ...submissionOperations, ...submissionFields, - // listing - ...listingOperations, - ...listingFields, + // subreddit + ...subredditOperations, + ...subredditFields, ], }; @@ -98,7 +112,26 @@ export class Reddit implements INodeType { for (let i = 0; i < items.length; i++) { - if (resource === 'myAccount') { + if (resource === 'allReddit') { + + if (operation === 'get') { + + const information = this.getNodeParameter('information', i) as string; + + if (information === 'trending') { + + const endpoint = 'api/trending_subreddits.json'; + responseData = await redditApiRequest.call(this, 'GET', endpoint, {}, {}, true); + + } else { + + const endpoint = 'best.json'; + responseData = await handleListing.call(this, i, endpoint); + + } + } + + } else if (resource === 'myAccount') { if (operation === 'get') { @@ -144,34 +177,31 @@ export class Reddit implements INodeType { } - } else if (resource === 'listing') { + } else if (resource === 'subreddit') { if (operation === 'get') { - const type = this.getNodeParameter('type', i) as string; + const qs: IDataObject = {}; - if (type === 'trending') { + const content = this.getNodeParameter('content', i) as string; + const subreddit = this.getNodeParameter('subreddit', i); - const endpoint = 'api/trending_subreddits.json'; - responseData = await redditApiRequest.call(this, 'GET', endpoint, {}, {}, true); + if (['about', 'rules', 'sidebar', 'sticky'].includes(content)) { - } else { + const endpoint = `r/${subreddit}/about/${content}.json`; + responseData = await redditApiRequest.call(this, 'GET', endpoint, qs, {}, true); - const endpoint = type === 'best' - ? 'best.json' - : `r/${this.getNodeParameter('subreddit', i)}/${type}.json`; - - const returnAll = this.getNodeParameter('returnAll', i); - - if (returnAll) { - responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, {}, {}, true); - } else { - const qs: IDataObject = { limit: this.getNodeParameter('limit', i) }; - responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, qs, {}, true); - responseData = responseData.splice(0, qs.limit); + if (content === 'rules') { + responseData = responseData.rules; } + } else if (['top', 'hot', 'new', 'rising'].includes(content)) { + + const endpoint = `r/${subreddit}/${content}.json`; + responseData = await handleListing.call(this, i, endpoint); + } + } }