n8n/packages/nodes-base/nodes/Reddit/Reddit.node.ts

224 lines
4.9 KiB
TypeScript
Raw Normal View History

2021-01-15 13:06:17 -08:00
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
2021-01-17 14:36:30 -08:00
handleListing,
2021-01-15 13:06:17 -08:00
redditApiRequest,
redditApiRequestAllItems,
} from './GenericFunctions';
2021-01-17 12:18:17 -08:00
import {
2021-01-17 14:36:30 -08:00
allRedditFields,
allRedditOperations,
} from './AllRedditDescription';
2021-01-17 12:18:17 -08:00
import {
myAccountFields,
myAccountOperations,
} from './MyAccountDescription';
import {
submissionFields,
submissionOperations,
} from './SubmissionDescription';
2021-01-17 14:36:30 -08:00
import {
subredditFields,
subredditOperations,
} from './SubredditDescription';
2021-01-15 13:06:17 -08:00
export class Reddit implements INodeType {
description: INodeTypeDescription = {
displayName: 'Reddit',
name: 'reddit',
icon: 'file:reddit.svg',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume the Reddit API',
defaults: {
name: 'Reddit',
color: '#ff5700',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'redditOAuth2Api',
required: true,
2021-01-17 14:52:50 -08:00
displayOptions: {
show: {
resource: [
'myAccount',
'submission',
],
},
},
2021-01-15 13:06:17 -08:00
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
2021-01-17 14:36:30 -08:00
{
name: 'All Reddit',
value: 'allReddit',
},
2021-01-15 13:06:17 -08:00
{
2021-01-15 16:20:37 -08:00
name: 'My Account',
value: 'myAccount',
},
{
name: 'Submission',
value: 'submission',
2021-01-15 13:06:17 -08:00
},
2021-01-17 10:01:18 -08:00
{
2021-01-17 14:36:30 -08:00
name: 'Subreddit',
value: 'subreddit',
2021-01-17 10:01:18 -08:00
},
2021-01-15 13:06:17 -08:00
],
2021-01-15 16:20:37 -08:00
default: 'myAccount',
2021-01-15 13:06:17 -08:00
description: 'Resource to consume',
},
2021-01-17 14:36:30 -08:00
// allReddit
...allRedditOperations,
...allRedditFields,
// myAccount
...myAccountOperations,
...myAccountFields,
2021-01-15 16:20:37 -08:00
// submission
...submissionOperations,
2021-01-17 10:01:18 -08:00
...submissionFields,
2021-01-17 14:36:30 -08:00
// subreddit
...subredditOperations,
...subredditFields,
2021-01-15 13:06:17 -08:00
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
let responseData;
const returnData: IDataObject[] = [];
2021-01-15 13:06:17 -08:00
for (let i = 0; i < items.length; i++) {
2021-01-17 14:36:30 -08:00
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') {
2021-01-15 13:06:17 -08:00
if (operation === 'get') {
const endpoints: {[key: string]: string} = {
identity: 'me',
blockedUsers: 'me/blocked',
friends: 'me/friends',
karma: 'me/karma',
prefs: 'me/prefs',
trophies: 'me/trophies',
};
2021-01-17 12:18:17 -08:00
const details = this.getNodeParameter('details', i) as string;
2021-01-17 11:17:17 -08:00
responseData = await redditApiRequest.call(this, 'GET', endpoints[details], {}, {}, false);
if (details === 'identity') {
responseData = responseData.features;
}
2021-01-15 13:06:17 -08:00
}
2021-01-17 10:01:18 -08:00
} else if (resource === 'submission') {
2021-01-15 16:20:37 -08:00
2021-01-17 10:01:18 -08:00
if (operation === 'post') {
2021-01-15 16:20:37 -08:00
const body: 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);
const resubmit = this.getNodeParameter('resubmit', i);
if (resubmit) {
body.resubmit = true;
}
2021-01-17 11:17:17 -08:00
responseData = await redditApiRequest.call(this, 'POST', 'submit', {}, body, false);
2021-01-15 16:20:37 -08:00
}
2021-01-17 14:36:30 -08:00
} else if (resource === 'subreddit') {
2021-01-17 10:01:18 -08:00
if (operation === 'get') {
2021-01-17 14:36:30 -08:00
const qs: IDataObject = {};
2021-01-17 10:01:18 -08:00
2021-01-17 14:36:30 -08:00
const content = this.getNodeParameter('content', i) as string;
const subreddit = this.getNodeParameter('subreddit', i);
2021-01-17 10:01:18 -08:00
2021-01-17 14:36:30 -08:00
if (['about', 'rules', 'sidebar', 'sticky'].includes(content)) {
2021-01-17 10:01:18 -08:00
2021-01-17 14:36:30 -08:00
const endpoint = `r/${subreddit}/about/${content}.json`;
responseData = await redditApiRequest.call(this, 'GET', endpoint, qs, {}, true);
2021-01-17 11:48:50 -08:00
2021-01-17 14:36:30 -08:00
if (content === 'rules') {
responseData = responseData.rules;
}
2021-01-17 10:01:18 -08:00
2021-01-17 14:36:30 -08:00
} else if (['top', 'hot', 'new', 'rising'].includes(content)) {
2021-01-17 11:17:17 -08:00
2021-01-17 14:36:30 -08:00
const endpoint = `r/${subreddit}/${content}.json`;
responseData = await handleListing.call(this, i, endpoint);
2021-01-17 10:01:18 -08:00
2021-01-17 11:48:50 -08:00
}
2021-01-17 14:36:30 -08:00
2021-01-17 10:01:18 -08:00
}
2021-01-15 13:06:17 -08:00
}
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(responseData);
2021-01-15 13:06:17 -08:00
}
return [this.helpers.returnJsonArray(returnData)];
}
}