mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-31 15:37:26 -08:00
224 lines
4.9 KiB
TypeScript
224 lines
4.9 KiB
TypeScript
import {
|
|
IExecuteFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
INodeExecutionData,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
handleListing,
|
|
redditApiRequest,
|
|
redditApiRequestAllItems,
|
|
} from './GenericFunctions';
|
|
|
|
import {
|
|
allRedditFields,
|
|
allRedditOperations,
|
|
} from './AllRedditDescription';
|
|
|
|
import {
|
|
myAccountFields,
|
|
myAccountOperations,
|
|
} from './MyAccountDescription';
|
|
|
|
import {
|
|
submissionFields,
|
|
submissionOperations,
|
|
} from './SubmissionDescription';
|
|
|
|
import {
|
|
subredditFields,
|
|
subredditOperations,
|
|
} from './SubredditDescription';
|
|
|
|
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,
|
|
displayOptions: {
|
|
show: {
|
|
resource: [
|
|
'myAccount',
|
|
'submission',
|
|
],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
properties: [
|
|
{
|
|
displayName: 'Resource',
|
|
name: 'resource',
|
|
type: 'options',
|
|
options: [
|
|
{
|
|
name: 'All Reddit',
|
|
value: 'allReddit',
|
|
},
|
|
{
|
|
name: 'My Account',
|
|
value: 'myAccount',
|
|
},
|
|
{
|
|
name: 'Submission',
|
|
value: 'submission',
|
|
},
|
|
{
|
|
name: 'Subreddit',
|
|
value: 'subreddit',
|
|
},
|
|
],
|
|
default: 'myAccount',
|
|
description: 'Resource to consume',
|
|
},
|
|
|
|
// allReddit
|
|
...allRedditOperations,
|
|
...allRedditFields,
|
|
|
|
// myAccount
|
|
...myAccountOperations,
|
|
...myAccountFields,
|
|
|
|
// submission
|
|
...submissionOperations,
|
|
...submissionFields,
|
|
|
|
// subreddit
|
|
...subredditOperations,
|
|
...subredditFields,
|
|
],
|
|
};
|
|
|
|
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[] = [];
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
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') {
|
|
|
|
const endpoints: {[key: string]: string} = {
|
|
identity: 'me',
|
|
blockedUsers: 'me/blocked',
|
|
friends: 'me/friends',
|
|
karma: 'me/karma',
|
|
prefs: 'me/prefs',
|
|
trophies: 'me/trophies',
|
|
};
|
|
|
|
const details = this.getNodeParameter('details', i) as string;
|
|
responseData = await redditApiRequest.call(this, 'GET', endpoints[details], {}, {}, false);
|
|
|
|
if (details === 'identity') {
|
|
responseData = responseData.features;
|
|
}
|
|
|
|
}
|
|
|
|
} else if (resource === 'submission') {
|
|
|
|
if (operation === 'post') {
|
|
|
|
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;
|
|
}
|
|
|
|
responseData = await redditApiRequest.call(this, 'POST', 'submit', {}, body, false);
|
|
|
|
}
|
|
|
|
} else if (resource === 'subreddit') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
const content = this.getNodeParameter('content', i) as string;
|
|
const subreddit = this.getNodeParameter('subreddit', i);
|
|
|
|
if (['about', 'rules', 'sidebar', 'sticky'].includes(content)) {
|
|
|
|
const endpoint = `r/${subreddit}/about/${content}.json`;
|
|
responseData = await redditApiRequest.call(this, 'GET', endpoint, qs, {}, true);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
Array.isArray(responseData)
|
|
? returnData.push(...responseData)
|
|
: returnData.push(responseData);
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
}
|
|
}
|