diff --git a/packages/nodes-base/nodes/Reddit/PostDescription.ts b/packages/nodes-base/nodes/Reddit/PostDescription.ts index cce6a0f582..a5c9606874 100644 --- a/packages/nodes-base/nodes/Reddit/PostDescription.ts +++ b/packages/nodes-base/nodes/Reddit/PostDescription.ts @@ -30,6 +30,11 @@ export const postOperations = [ value: 'getAll', description: 'Get all posts from a subreddit', }, + { + name: 'Search', + value: 'search', + description: 'Search posts in a subreddit or in all of Reddit.', + }, ], displayOptions: { show: { @@ -348,4 +353,166 @@ export const postFields = [ }, ], }, + + // ---------------------------------- + // post: search + // ---------------------------------- + { + displayName: 'Location', + name: 'location', + type: 'options', + default: 'subreddit', + description: 'Location where to search for posts.', + options: [ + { + name: 'All Reddit', + value: 'allReddit', + description: 'Search for posts in all of Reddit.', + }, + { + name: 'Subreddit', + value: 'subreddit', + description: 'Search for posts in a specific subreddit.', + }, + ], + displayOptions: { + show: { + resource: [ + 'post', + ], + operation: [ + 'search', + ], + }, + }, + }, + { + displayName: 'Subreddit', + name: 'subreddit', + type: 'string', + required: true, + default: '', + description: 'The name of subreddit to search in.', + displayOptions: { + show: { + resource: [ + 'post', + ], + operation: [ + 'search', + ], + location: [ + 'subreddit', + ], + }, + }, + }, + { + displayName: 'Keyword', + name: 'keyword', + type: 'string', + default: '', + required: true, + description: 'The keyword for the search.', + displayOptions: { + show: { + resource: [ + 'post', + ], + operation: [ + 'search', + ], + }, + }, + }, + { + displayName: 'Return All', + name: 'returnAll', + type: 'boolean', + default: false, + description: 'Return all results.', + displayOptions: { + show: { + resource: [ + 'post', + ], + operation: [ + 'search', + ], + }, + }, + }, + { + displayName: 'Limit', + name: 'limit', + type: 'number', + default: 100, + description: 'The number of results to return.', + typeOptions: { + minValue: 1, + maxValue: 100, + }, + displayOptions: { + show: { + resource: [ + 'post', + ], + operation: [ + 'search', + ], + returnAll: [ + false, + ], + }, + }, + }, + { + displayName: 'Additional Fields', + name: 'additionalFields', + type: 'collection', + placeholder: 'Add Field', + default: {}, + displayOptions: { + show: { + resource: [ + 'post', + ], + operation: [ + 'search', + ], + }, + }, + options: [ + { + displayName: 'Sort', + name: 'sort', + placeholder: '', + type: 'options', + default: 'relevance', + description: 'The category to sort results by.', + options: [ + { + name: 'Comments', + value: 'comments', + }, + { + name: 'Hot', + value: 'hot', + }, + { + name: 'New', + value: 'new', + }, + { + name: 'Top', + value: 'top', + }, + { + name: 'Relevance', + value: 'relevance', + }, + ], + }, + ], + }, ] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/Reddit/Reddit.node.ts b/packages/nodes-base/nodes/Reddit/Reddit.node.ts index 29d41a39d3..093414e021 100644 --- a/packages/nodes-base/nodes/Reddit/Reddit.node.ts +++ b/packages/nodes-base/nodes/Reddit/Reddit.node.ts @@ -130,6 +130,7 @@ export class Reddit implements INodeType { if (resource === 'post') { if (operation === 'create') { + // ---------------------------------- // post: create // ---------------------------------- @@ -155,6 +156,7 @@ export class Reddit implements INodeType { responseData = responseData.json.data; } else if (operation === 'delete') { + // ---------------------------------- // post: delete // ---------------------------------- @@ -172,6 +174,7 @@ export class Reddit implements INodeType { responseData = { success: true }; } else if (operation === 'get') { + // ---------------------------------- // post: get // ---------------------------------- @@ -184,6 +187,7 @@ export class Reddit implements INodeType { responseData = responseData[0].data.children[0].data; } else if (operation === 'getAll') { + // ---------------------------------- // post: getAll // ---------------------------------- @@ -203,6 +207,45 @@ export class Reddit implements INodeType { responseData = await handleListing.call(this, i, endpoint); + } else if (operation === 'search') { + + // ---------------------------------- + // post: search + // ---------------------------------- + + // https://www.reddit.com/dev/api/#GET_search + + const location = this.getNodeParameter('location', i); + + const qs = { + q: this.getNodeParameter('keyword', i), + restrict_sr: location === 'subreddit', + } as IDataObject; + + const { sort } = this.getNodeParameter('additionalFields', i) as IDataObject; + + if (sort) { + qs.sort = sort; + } + + let endpoint = ''; + + if (location === 'allReddit') { + endpoint = 'search.json'; + } else { + const subreddit = this.getNodeParameter('subreddit', i); + endpoint = `r/${subreddit}/search.json`; + } + + responseData = await handleListing.call(this, i, endpoint, qs); + + const returnAll = this.getNodeParameter('returnAll', 0) as boolean; + + if (!returnAll) { + const limit = this.getNodeParameter('limit', 0) as number; + responseData = responseData.splice(0, limit); + } + } } else if (resource === 'postComment') { @@ -212,6 +255,7 @@ export class Reddit implements INodeType { // ********************************************************************* if (operation === 'create') { + // ---------------------------------- // postComment: create // ---------------------------------- @@ -229,6 +273,7 @@ export class Reddit implements INodeType { responseData = responseData.json.data.things[0].data; } else if (operation === 'getAll') { + // ---------------------------------- // postComment: getAll // ---------------------------------- @@ -242,6 +287,7 @@ export class Reddit implements INodeType { responseData = await handleListing.call(this, i, endpoint); } else if (operation === 'delete') { + // ---------------------------------- // postComment: delete // ---------------------------------- @@ -259,6 +305,7 @@ export class Reddit implements INodeType { responseData = { success: true }; } else if (operation === 'reply') { + // ---------------------------------- // postComment: reply // ---------------------------------- @@ -277,11 +324,13 @@ export class Reddit implements INodeType { } } else if (resource === 'profile') { + // ********************************************************************* - // pprofile + // profile // ********************************************************************* if (operation === 'get') { + // ---------------------------------- // profile: get // ---------------------------------- @@ -329,6 +378,7 @@ export class Reddit implements INodeType { // ********************************************************************* if (operation === 'get') { + // ---------------------------------- // subreddit: get // ---------------------------------- @@ -349,6 +399,7 @@ export class Reddit implements INodeType { } } else if (operation === 'getAll') { + // ---------------------------------- // subreddit: getAll // ---------------------------------- @@ -389,6 +440,7 @@ export class Reddit implements INodeType { } } else if (resource === 'user') { + // ********************************************************************* // user // *********************************************************************