2021-01-15 13:06:17 -08:00
import {
IExecuteFunctions ,
} from 'n8n-core' ;
import {
IDataObject ,
INodeExecutionData ,
INodeType ,
INodeTypeDescription ,
} from 'n8n-workflow' ;
import {
redditApiRequest ,
redditApiRequestAllItems ,
} from './GenericFunctions' ;
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 ,
} ,
] ,
properties : [
// ----------------------------------
// Resources
// ----------------------------------
{
displayName : 'Resource' ,
name : 'resource' ,
type : 'options' ,
options : [
{
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-15 16:20:37 -08:00
default : 'myAccount' ,
2021-01-15 13:06:17 -08:00
description : 'Resource to consume' ,
} ,
// ----------------------------------
2021-01-15 16:20:37 -08:00
// My Account operations
2021-01-15 13:06:17 -08:00
// ----------------------------------
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
displayOptions : {
show : {
resource : [
2021-01-15 16:20:37 -08:00
'myAccount' ,
2021-01-15 13:06:17 -08:00
] ,
} ,
} ,
options : [
{
2021-01-15 16:20:37 -08:00
name : 'Get identity' ,
value : 'getIdentity' ,
2021-01-15 15:15:32 -08:00
description : 'Return the identity of the logged-in user' ,
} ,
{
name : 'Get blocked users' ,
2021-01-15 16:20:37 -08:00
value : 'getBlockedUsers' ,
2021-01-15 15:15:32 -08:00
description : 'Return the identity of the logged-in user' ,
} ,
{
2021-01-15 16:20:37 -08:00
name : 'Get friends' ,
value : 'getFriends' ,
2021-01-15 15:15:32 -08:00
description : 'Return a list of friends for the logged-in user' ,
} ,
{
2021-01-15 16:20:37 -08:00
name : 'Get karma' ,
value : 'getKarma' ,
2021-01-15 15:15:32 -08:00
description : 'Return a breakdown of subreddit karma' ,
} ,
{
2021-01-15 16:20:37 -08:00
name : 'Get preferences' ,
value : 'getPrefs' ,
2021-01-15 15:15:32 -08:00
description : 'Return the preference settings of the logged-in user' ,
} ,
{
2021-01-15 16:20:37 -08:00
name : 'Get trophies' ,
value : 'getTrophies' ,
2021-01-15 15:15:32 -08:00
description : 'Return a list of trophies for the logged-in user' ,
2021-01-15 13:06:17 -08:00
} ,
] ,
2021-01-15 16:20:37 -08:00
default : 'getIdentity' ,
description : 'Operation to perform' ,
} ,
// ----------------------------------
// Submission operations
// ----------------------------------
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
displayOptions : {
show : {
resource : [
'submission' ,
] ,
} ,
} ,
options : [
{
name : 'Post' ,
value : 'post' ,
description : 'Post a submission to a subreddit' ,
} ,
] ,
default : 'post' ,
2021-01-15 13:06:17 -08:00
description : 'Operation to perform' ,
} ,
2021-01-15 16:20:37 -08:00
{
displayName : 'Title' ,
name : 'title' ,
type : 'string' ,
required : true ,
default : '' ,
description : 'Title of the submission, up to 300 characters long' ,
displayOptions : {
show : {
resource : [
'submission' ,
] ,
operation : [
'post' ,
] ,
} ,
} ,
} ,
{
displayName : 'Subreddit' ,
name : 'subreddit' ,
type : 'string' ,
required : true ,
default : '' ,
description : 'Subreddit to post the submission to' ,
displayOptions : {
show : {
resource : [
'submission' ,
] ,
operation : [
'post' ,
] ,
} ,
} ,
} ,
{
displayName : 'Kind' ,
name : 'kind' ,
type : 'options' ,
options : [
{
name : 'Text Post' ,
value : 'self' ,
} ,
{
name : 'Link Post' ,
value : 'link' ,
} ,
{
name : 'Image Post' ,
value : 'image' ,
} ,
{
name : 'Video Post' ,
value : 'video' ,
} ,
{
name : 'Video GIF Post' ,
value : 'videogif' ,
} ,
] ,
default : 'text' ,
description : 'The kind of the submission to be posted' ,
displayOptions : {
show : {
resource : [
'submission' ,
] ,
operation : [
'post' ,
] ,
} ,
} ,
} ,
{
displayName : 'URL' ,
name : 'url' ,
type : 'string' ,
required : true ,
default : '' ,
description : 'URL of the content of the submission' ,
displayOptions : {
show : {
resource : [
'submission' ,
] ,
operation : [
'post' ,
] ,
kind : [
'link' ,
'image' ,
'video' ,
'videogif' ,
] ,
} ,
} ,
} ,
{
displayName : 'Text' ,
name : 'text' ,
type : 'string' ,
required : true ,
default : '' ,
description : 'Text content of the submission (Markdown supported)' ,
displayOptions : {
show : {
resource : [
'submission' ,
] ,
operation : [
'post' ,
] ,
kind : [
'self' ,
] ,
} ,
} ,
} ,
{
displayName : 'Resubmit' ,
name : 'resubmit' ,
type : 'boolean' ,
default : false ,
description : 'If toggled on, the URL will be submitted even if<br>it was already submitted to the subreddit before.<br>Otherwise, a resubmission will trigger an error.' ,
displayOptions : {
show : {
resource : [
'submission' ,
] ,
operation : [
'post' ,
] ,
kind : [
'link' ,
'image' ,
'video' ,
'videogif' ,
] ,
} ,
} ,
} ,
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 ;
2021-01-15 15:15:32 -08:00
let responseData ;
const returnData : IDataObject [ ] = [ ] ;
2021-01-15 13:06:17 -08:00
for ( let i = 0 ; i < items . length ; i ++ ) {
2021-01-15 16:20:37 -08:00
if ( resource === 'myAccount' ) {
2021-01-15 13:06:17 -08:00
2021-01-15 16:20:37 -08:00
if ( operation === 'getIdentity' ) {
2021-01-15 15:15:32 -08:00
responseData = await redditApiRequest . call ( this , 'GET' , 'me' ) ;
responseData = responseData . features ;
2021-01-15 16:20:37 -08:00
} else if ( operation === 'getBlockedUsers' ) {
2021-01-15 15:15:32 -08:00
responseData = await redditApiRequest . call ( this , 'GET' , 'me/blocked' ) ;
2021-01-15 16:20:37 -08:00
} else if ( operation === 'getFriends' ) {
2021-01-15 15:15:32 -08:00
responseData = await redditApiRequest . call ( this , 'GET' , 'me/friends' ) ;
2021-01-15 16:20:37 -08:00
} else if ( operation === 'getKarma' ) {
2021-01-15 15:15:32 -08:00
responseData = await redditApiRequest . call ( this , 'GET' , 'me/karma' ) ;
2021-01-15 16:20:37 -08:00
} else if ( operation === 'getPrefs' ) {
2021-01-15 15:15:32 -08:00
responseData = await redditApiRequest . call ( this , 'GET' , 'me/prefs' ) ;
2021-01-15 16:20:37 -08:00
} else if ( operation === 'getTrophies' ) {
2021-01-15 15:15:32 -08:00
responseData = await redditApiRequest . call ( this , 'GET' , 'me/trophies' ) ;
2021-01-15 13:06:17 -08:00
}
2021-01-15 15:15:32 -08:00
2021-01-15 16:20:37 -08:00
} else if ( resource === 'post' ) {
if ( operation === 'submit' ) {
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 ) ;
}
2021-01-15 13:06:17 -08:00
}
2021-01-15 15:15:32 -08:00
Array . isArray ( responseData )
? returnData . push ( . . . responseData )
: returnData . push ( responseData ) ;
2021-01-15 13:06:17 -08:00
}
return [ this . helpers . returnJsonArray ( returnData ) ] ;
}
}