Add submission search and commenting functionality

This commit is contained in:
Iván Ovejero 2021-01-18 16:58:35 -03:00
parent a04e797df4
commit e34e4f181f
3 changed files with 128 additions and 13 deletions

View file

@ -24,14 +24,17 @@ export async function redditApiRequest(
): Promise<any> { // tslint:disable-line:no-any
const resource = this.getNodeParameter('resource', 0) as string;
const requiresAuth = ['myAccount', 'submission'].includes(resource);
const operation = this.getNodeParameter('operation', 0) as string;
// const requiresAuth = ['myAccount', 'submission'].includes(resource);
const requiresAuth = resource === 'myAccount' ||
(resource === 'submission' && ['post', 'comment'].includes(operation));
const options: OptionsWithUri = {
headers: {
'user-agent': 'n8n',
},
method,
uri: requiresAuth ? `https://oauth.reddit.com/api/v1/${endpoint}` : `https://www.reddit.com/${endpoint}`,
uri: requiresAuth ? `https://oauth.reddit.com/${endpoint}` : `https://www.reddit.com/${endpoint}`,
qs,
body,
json: true,
@ -46,6 +49,7 @@ export async function redditApiRequest(
}
try {
console.log(options);
return requiresAuth
? await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options)
: await this.helpers.request.call(this, options);

View file

@ -165,7 +165,7 @@ export class Reddit implements INodeType {
};
const details = this.getNodeParameter('details', i) as string;
responseData = await redditApiRequest.call(this, 'GET', endpoints[details], {}, {});
responseData = await redditApiRequest.call(this, 'GET', `api/v1/${endpoints[details]}`, {}, {});
if (details === 'identity') {
responseData = responseData.features;
@ -177,23 +177,43 @@ export class Reddit implements INodeType {
if (operation === 'post') {
const body: IDataObject = {
const qs: IDataObject = {
title: this.getNodeParameter('title', i),
sr: this.getNodeParameter('subreddit', i),
kind: this.getNodeParameter('kind', i),
};
body.kind === 'self'
? body.text = this.getNodeParameter('text', i)
: body.url = this.getNodeParameter('url', i);
qs.kind === 'self'
? qs.text = this.getNodeParameter('text', i)
: qs.url = this.getNodeParameter('url', i);
const resubmit = this.getNodeParameter('resubmit', i);
if (resubmit) {
body.resubmit = true;
if (qs.url) {
qs.resubmit = this.getNodeParameter('resubmit', i);
}
responseData = await redditApiRequest.call(this, 'POST', 'submit', {}, body);
responseData = await redditApiRequest.call(this, 'POST', 'api/submit', qs, {});
} else if (operation === 'comment') {
const qs: IDataObject = {
thing_id: this.getNodeParameter('target', i),
text: this.getNodeParameter('text', i),
};
responseData = await redditApiRequest.call(this, 'POST', 'api/comment', qs, {});
} else if (operation === 'search') {
const subreddit = this.getNodeParameter('subreddit', i);
const qs: IDataObject = {
q: this.getNodeParameter('keyword', i),
restrict_sr: 'on',
};
const endpoint = `r/${subreddit}/search.json`;
responseData = await redditApiRequest.call(this, 'GET', endpoint, qs, {});
}

View file

@ -15,6 +15,16 @@ export const submissionOperations = [
value: 'post',
description: 'Post a submission to a subreddit',
},
{
name: 'Comment',
value: 'comment',
description: 'Comment on a submission in a subreddit',
},
{
name: 'Search',
value: 'search',
description: 'Search for a submission in a subreddit',
},
],
displayOptions: {
show: {
@ -27,6 +37,9 @@ export const submissionOperations = [
] as INodeProperties[];
export const submissionFields = [
// ----------------------------------
// post submission
// ----------------------------------
{
displayName: 'Title',
name: 'title',
@ -89,7 +102,7 @@ export const submissionFields = [
value: 'videogif',
},
],
default: 'text',
default: 'self',
description: 'The kind of the submission to be posted',
displayOptions: {
show: {
@ -170,4 +183,82 @@ export const submissionFields = [
},
},
},
// ----------------------------------
// comment on submission
// ----------------------------------
{
displayName: 'Target',
name: 'target',
type: 'string',
default: '',
description: 'ID of the target of the comment. The target can be either<br>the top-level submission or a reply in that submission.',
placeholder: 't3_15bfi0',
displayOptions: {
show: {
resource: [
'submission',
],
operation: [
'comment',
],
},
},
},
{
displayName: 'Text',
name: 'text',
type: 'string',
required: true,
default: '',
description: 'Text content of the comment (Markdown supported)',
displayOptions: {
show: {
resource: [
'submission',
],
operation: [
'comment',
],
},
},
},
// ----------------------------------
// search for submission
// ----------------------------------
{
displayName: 'Subreddit',
name: 'subreddit',
type: 'string',
required: true,
default: '',
description: 'Subreddit to search for posts',
displayOptions: {
show: {
resource: [
'submission',
],
operation: [
'search',
],
},
},
},
{
displayName: 'Keyword',
name: 'keyword',
type: 'string',
required: true,
default: '',
description: 'The keyword for the subreddit post search',
displayOptions: {
show: {
resource: [
'submission',
],
operation: [
'search',
],
},
},
},
] as INodeProperties[];