2020-01-21 05:53:54 -08:00
import {
IExecuteFunctions ,
} from 'n8n-core' ;
import {
IDataObject ,
INodeExecutionData ,
INodeType ,
2020-10-01 05:01:39 -07:00
INodeTypeDescription ,
2021-04-16 09:33:36 -07:00
NodeOperationError ,
2020-01-21 05:53:54 -08:00
} from 'n8n-workflow' ;
2020-01-23 07:13:57 -08:00
import { disqusApiRequest , disqusApiRequestAllItems } from './GenericFunctions' ;
2020-01-21 05:53:54 -08:00
export class Disqus implements INodeType {
description : INodeTypeDescription = {
displayName : 'Disqus' ,
name : 'disqus' ,
icon : 'file:disqus.png' ,
group : [ 'input' ] ,
version : 1 ,
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
description : 'Access data on Disqus' ,
defaults : {
name : 'Disqus' ,
color : '#22BB44' ,
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'disqusApi' ,
required : true ,
2020-10-22 06:46:03 -07:00
} ,
2020-01-21 05:53:54 -08:00
] ,
properties : [
{
displayName : 'Resource' ,
name : 'resource' ,
type : 'options' ,
options : [
{
name : 'Forum' ,
value : 'forum' ,
2020-01-23 14:50:22 -08:00
} ,
2020-01-21 05:53:54 -08:00
] ,
default : 'forum' ,
description : 'The resource to operate on.' ,
} ,
// ----------------------------------
// forum
// ----------------------------------
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
displayOptions : {
show : {
resource : [
'forum' ,
] ,
} ,
} ,
options : [
2020-01-21 06:12:28 -08:00
{
2020-01-21 05:53:54 -08:00
name : 'Get' ,
value : 'get' ,
2020-07-24 03:56:41 -07:00
description : 'Return forum details' ,
2020-01-21 06:12:28 -08:00
} ,
{
2020-01-21 05:53:54 -08:00
name : 'Get All Categories' ,
value : 'getCategories' ,
2020-07-24 03:56:41 -07:00
description : 'Return a list of categories within a forum' ,
2020-01-21 06:12:28 -08:00
} ,
2020-01-21 05:53:54 -08:00
{
name : 'Get All Threads' ,
value : 'getThreads' ,
2020-07-24 03:56:41 -07:00
description : 'Return a list of threads within a forum' ,
2020-01-21 05:53:54 -08:00
} ,
{
name : 'Get All Posts' ,
value : 'getPosts' ,
2020-07-24 03:56:41 -07:00
description : 'Return a list of posts within a forum' ,
2020-10-22 06:46:03 -07:00
} ,
2020-01-21 05:53:54 -08:00
] ,
default : 'get' ,
description : 'The operation to perform.' ,
} ,
// ----------------------------------
// forum:get
// ----------------------------------
{
displayName : 'Forum name' ,
name : 'id' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
'get' ,
] ,
resource : [
'forum' ,
] ,
} ,
} ,
description : 'The short name(aka ID) of the forum to get.' ,
} ,
{
displayName : 'Additional Fields' ,
name : 'additionalFields' ,
type : 'collection' ,
placeholder : 'Add Field' ,
displayOptions : {
show : {
operation : [
'get' ,
] ,
resource : [
'forum' ,
] ,
} ,
} ,
default : { } ,
options : [
{
2020-01-23 14:50:22 -08:00
displayName : 'Attach' ,
2020-01-21 05:53:54 -08:00
name : 'attach' ,
2020-01-23 14:50:22 -08:00
type : 'multiOptions' ,
options : [
{
name : 'counters' ,
value : 'counters' ,
} ,
{
name : 'followsForum' ,
value : 'followsForum' ,
} ,
{
name : 'forumCanDisableAds' ,
value : 'forumCanDisableAds' ,
} ,
{
name : 'forumDaysAlive' ,
value : 'forumDaysAlive' ,
} ,
{
name : 'forumFeatures' ,
value : 'forumFeatures' ,
} ,
{
name : 'forumForumCategory' ,
value : 'forumForumCategory' ,
} ,
{
name : 'forumIntegration' ,
value : 'forumIntegration' ,
} ,
{
name : 'forumNewPolicy' ,
value : 'forumNewPolicy' ,
} ,
{
name : 'forumPermissions' ,
value : 'forumPermissions' ,
} ,
] ,
default : [ ] ,
description : 'The resource to operate on.' ,
2020-01-21 05:53:54 -08:00
} ,
{
2020-01-23 14:50:22 -08:00
displayName : 'Related' ,
2020-01-21 05:53:54 -08:00
name : 'related' ,
2020-01-23 14:50:22 -08:00
type : 'multiOptions' ,
options : [
{
name : 'author' ,
value : 'author' ,
} ,
] ,
default : [ ] ,
description : 'You may specify relations to include with your response' ,
2020-01-21 05:53:54 -08:00
} ,
] ,
} ,
// ----------------------------------
// forum:getPosts
// ----------------------------------
{
displayName : 'Forum name' ,
name : 'id' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
'getPosts' ,
] ,
resource : [
'forum' ,
] ,
} ,
} ,
description : 'The short name(aka ID) of the forum to get.' ,
} ,
2020-01-23 07:13:57 -08:00
{
displayName : 'Return All' ,
name : 'returnAll' ,
type : 'boolean' ,
displayOptions : {
show : {
resource : [
'forum' ,
] ,
operation : [
'getPosts' ,
] ,
} ,
} ,
default : false ,
description : 'If all results should be returned or only up to a given limit.' ,
} ,
{
displayName : 'Limit' ,
name : 'limit' ,
type : 'number' ,
displayOptions : {
show : {
resource : [
'forum' ,
] ,
operation : [
'getPosts' ,
] ,
returnAll : [
false ,
] ,
} ,
} ,
typeOptions : {
minValue : 1 ,
maxValue : 100 ,
} ,
default : 100 ,
description : 'How many results to return.' ,
} ,
2020-01-21 05:53:54 -08:00
{
displayName : 'Additional Fields' ,
name : 'additionalFields' ,
type : 'collection' ,
placeholder : 'Add Field' ,
displayOptions : {
show : {
operation : [
'getPosts' ,
] ,
resource : [
'forum' ,
] ,
} ,
} ,
default : { } ,
options : [
{
2020-01-23 14:50:22 -08:00
displayName : 'Filters' ,
name : 'filters' ,
type : 'multiOptions' ,
options : [
{
name : 'Has_Bad_Word' ,
value : 'Has_Bad_Word' ,
} ,
{
name : 'Has_Link' ,
value : 'Has_Link' ,
} ,
{
name : 'Has_Low_Rep_Author' ,
value : 'Has_Low_Rep_Author' ,
} ,
{
name : 'Has_Media' ,
value : 'Has_Media' ,
} ,
{
name : 'Is_Anonymous' ,
value : 'Is_Anonymous' ,
} ,
{
name : 'Is_Flagged' ,
value : 'Is_Flagged' ,
} ,
{
name : 'No_Issue' ,
value : 'No_Issue' ,
} ,
{
name : 'Is_At_Flag_Limit' ,
value : 'Is_At_Flag_Limit' ,
} ,
{
name : 'Is_Toxic' ,
value : 'Is_Toxic' ,
} ,
{
name : 'Modified_By_Rule' ,
value : 'Modified_By_Rule' ,
} ,
{
name : 'Shadow_Banned' ,
value : 'Shadow_Banned' ,
} ,
] ,
default : [ ] ,
2020-10-22 06:46:03 -07:00
description : 'You may specify filters for your response.' ,
2020-01-21 06:12:28 -08:00
} ,
{
2020-01-23 14:50:22 -08:00
displayName : 'Include' ,
name : 'include' ,
type : 'multiOptions' ,
options : [
{
2020-01-23 20:57:59 -08:00
name : 'approved' ,
value : 'approved' ,
2020-01-23 14:50:22 -08:00
} ,
] ,
default : [ ] ,
description : 'You may specify relations to include with your response.' ,
2020-01-21 06:12:28 -08:00
} ,
{
2020-01-23 14:50:22 -08:00
displayName : 'Order' ,
name : 'order' ,
type : 'options' ,
options : [
{
name : 'ASC' ,
value : 'asc' ,
} ,
{
name : 'DESC' ,
value : 'desc' ,
2020-10-22 06:46:03 -07:00
} ,
2020-01-23 14:50:22 -08:00
] ,
default : 'asc' ,
description : 'You may specify order to sort your response.' ,
2020-01-21 06:12:28 -08:00
} ,
{
2020-01-21 05:53:54 -08:00
displayName : 'Query' ,
name : 'query' ,
type : 'string' ,
default : '' ,
2020-01-23 14:50:22 -08:00
description : 'You may specify query forChoices: asc, desc your response.' ,
2020-01-21 06:12:28 -08:00
} ,
{
2020-01-23 14:50:22 -08:00
displayName : 'Related' ,
name : 'related' ,
type : 'multiOptions' ,
options : [
{
name : 'thread' ,
value : 'thread' ,
} ,
] ,
default : [ ] ,
description : 'You may specify relations to include with your response' ,
2020-01-21 06:12:28 -08:00
} ,
{
2020-01-23 14:50:22 -08:00
displayName : 'Since' ,
name : 'since' ,
type : 'dateTime' ,
default : '' ,
description : 'Unix timestamp (or ISO datetime standard)' ,
2020-01-21 05:53:54 -08:00
} ,
] ,
2020-01-21 06:12:28 -08:00
} ,
2020-01-21 05:53:54 -08:00
2020-01-21 06:12:28 -08:00
// ----------------------------------
2020-01-21 05:53:54 -08:00
// forum:getCategories
// ----------------------------------
{
displayName : 'Forum name' ,
name : 'id' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
'getCategories' ,
] ,
resource : [
'forum' ,
] ,
} ,
} ,
description : 'The short name(aka ID) of the forum to get Categories.' ,
} ,
2020-01-23 07:13:57 -08:00
{
displayName : 'Return All' ,
name : 'returnAll' ,
type : 'boolean' ,
displayOptions : {
show : {
resource : [
'forum' ,
] ,
operation : [
'getCategories' ,
] ,
} ,
} ,
default : false ,
description : 'If all results should be returned or only up to a given limit.' ,
} ,
{
displayName : 'Limit' ,
name : 'limit' ,
type : 'number' ,
displayOptions : {
show : {
resource : [
'forum' ,
] ,
operation : [
'getCategories' ,
] ,
returnAll : [
false ,
] ,
} ,
} ,
typeOptions : {
minValue : 1 ,
maxValue : 100 ,
} ,
default : 100 ,
description : 'How many results to return.' ,
} ,
2020-01-21 05:53:54 -08:00
{
displayName : 'Additional Fields' ,
name : 'additionalFields' ,
type : 'collection' ,
placeholder : 'Add Field' ,
displayOptions : {
show : {
operation : [
'getCategories' ,
] ,
resource : [
'forum' ,
] ,
} ,
} ,
default : { } ,
options : [
2020-01-21 06:12:28 -08:00
{
2020-01-21 05:53:54 -08:00
displayName : 'Order' ,
name : 'order' ,
2020-01-23 14:50:22 -08:00
type : 'options' ,
options : [
{
name : 'ASC' ,
value : 'asc' ,
} ,
{
name : 'DESC' ,
value : 'desc' ,
2020-10-22 06:46:03 -07:00
} ,
2020-01-23 14:50:22 -08:00
] ,
2020-01-21 05:53:54 -08:00
default : 'asc' ,
2020-01-23 14:50:22 -08:00
description : 'You may specify order to sort your response.' ,
2020-01-21 05:53:54 -08:00
} ,
] ,
2020-01-21 06:12:28 -08:00
} ,
2020-01-21 05:53:54 -08:00
2020-01-21 06:12:28 -08:00
// ----------------------------------
2020-01-21 05:53:54 -08:00
// forum:getThreads
// ----------------------------------
{
displayName : 'Forum name' ,
name : 'id' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
'getThreads' ,
] ,
resource : [
'forum' ,
] ,
} ,
} ,
description : 'The short name(aka ID) of the forum to get Threads.' ,
} ,
2020-01-23 07:13:57 -08:00
{
displayName : 'Return All' ,
name : 'returnAll' ,
type : 'boolean' ,
displayOptions : {
show : {
resource : [
'forum' ,
] ,
operation : [
'getThreads' ,
] ,
} ,
} ,
default : false ,
description : 'If all results should be returned or only up to a given limit.' ,
} ,
{
displayName : 'Limit' ,
name : 'limit' ,
type : 'number' ,
displayOptions : {
show : {
resource : [
'forum' ,
] ,
operation : [
'getThreads' ,
] ,
returnAll : [
false ,
] ,
} ,
} ,
typeOptions : {
minValue : 1 ,
maxValue : 100 ,
} ,
default : 100 ,
description : 'How many results to return.' ,
} ,
2020-01-21 05:53:54 -08:00
{
displayName : 'Additional Fields' ,
name : 'additionalFields' ,
type : 'collection' ,
placeholder : 'Add Field' ,
displayOptions : {
show : {
operation : [
'getThreads' ,
] ,
resource : [
'forum' ,
] ,
} ,
} ,
default : { } ,
2020-01-21 06:12:28 -08:00
options : [
2020-01-21 05:53:54 -08:00
{
displayName : 'Related' ,
name : 'related' ,
2020-01-23 14:50:22 -08:00
type : 'multiOptions' ,
options : [
{
name : 'author' ,
value : 'author' ,
} ,
{
name : 'forum' ,
value : 'forum' ,
} ,
] ,
default : [ ] ,
description : 'You may specify relations to include with your response' ,
2020-01-21 06:12:28 -08:00
} ,
{
2020-01-23 14:50:22 -08:00
displayName : 'Include' ,
name : 'include' ,
type : 'multiOptions' ,
options : [
{
name : 'closed' ,
value : 'closed' ,
} ,
{
name : 'open' ,
value : 'open' ,
} ,
{
name : 'killed' ,
value : 'killed' ,
} ,
] ,
default : [ ] ,
description : 'You may specify relations to include with your response.' ,
2020-01-21 06:12:28 -08:00
} ,
{
2020-01-23 14:50:22 -08:00
displayName : 'Order' ,
name : 'order' ,
type : 'options' ,
options : [
{
name : 'ASC' ,
value : 'asc' ,
} ,
{
name : 'DESC' ,
value : 'desc' ,
2020-10-22 06:46:03 -07:00
} ,
2020-01-23 14:50:22 -08:00
] ,
default : 'asc' ,
description : 'You may specify order to sort your response.' ,
2020-01-21 06:12:28 -08:00
} ,
{
2020-01-23 14:50:22 -08:00
displayName : 'Since' ,
name : 'since' ,
type : 'dateTime' ,
2020-01-21 05:53:54 -08:00
default : '' ,
2020-01-23 14:50:22 -08:00
description : 'Unix timestamp (or ISO datetime standard)' ,
2020-01-21 06:12:28 -08:00
} ,
{
2020-01-23 14:50:22 -08:00
displayName : 'Thread' ,
name : 'threadId' ,
2020-01-21 05:53:54 -08:00
type : 'string' ,
2020-01-23 14:50:22 -08:00
default : '' ,
2021-10-27 13:00:13 -07:00
description : 'Looks up a thread by ID. You may pass us the "ident" query type instead of an ID by including "forum". You may pass us the "link" query type to filter by URL. You must pass the "forum" if you do not have the Pro API Access addon.' ,
2020-01-21 05:53:54 -08:00
} ,
] ,
2020-10-22 06:46:03 -07:00
} ,
2020-01-21 05:53:54 -08:00
] ,
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
const items = this . getInputData ( ) ;
const returnData : IDataObject [ ] = [ ] ;
const resource = this . getNodeParameter ( 'resource' , 0 ) as string ;
const operation = this . getNodeParameter ( 'operation' , 0 ) as string ;
let endpoint = '' ;
let requestMethod = '' ;
let body : IDataObject | Buffer ;
let qs : IDataObject ;
for ( let i = 0 ; i < items . length ; i ++ ) {
2021-07-19 23:58:54 -07:00
try {
body = { } ;
qs = { } ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
if ( resource === 'forum' ) {
if ( operation === 'get' ) {
// ----------------------------------
// get
// ----------------------------------
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
requestMethod = 'GET' ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
endpoint = 'forums/details.json' ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
const id = this . getNodeParameter ( 'id' , i ) as string ;
qs . forum = id ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) as IDataObject ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
Object . assign ( qs , additionalFields ) ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
try {
const responseData = await disqusApiRequest . call ( this , requestMethod , qs , endpoint ) ;
returnData . push ( responseData . response ) ;
} catch ( error ) {
throw error ;
}
2020-01-23 07:13:57 -08:00
2021-07-19 23:58:54 -07:00
} else if ( operation === 'getPosts' ) {
// ----------------------------------
// getPosts
// ----------------------------------
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
requestMethod = 'GET' ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
endpoint = 'forums/listPosts.json' ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
const id = this . getNodeParameter ( 'id' , i ) as string ;
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) as IDataObject ;
Object . assign ( qs , additionalFields ) ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
const returnAll = this . getNodeParameter ( 'returnAll' , i ) as boolean ;
2020-01-23 07:13:57 -08:00
2021-07-19 23:58:54 -07:00
qs . forum = id ;
qs . limit = 100 ;
2020-01-23 07:13:57 -08:00
2021-07-19 23:58:54 -07:00
try {
let responseData : IDataObject = { } ;
if ( returnAll ) {
responseData . response = await disqusApiRequestAllItems . call ( this , requestMethod , qs , endpoint ) ;
} else {
const limit = this . getNodeParameter ( 'limit' , i ) as string ;
qs . limit = limit ;
responseData = await disqusApiRequest . call ( this , requestMethod , qs , endpoint ) ;
}
returnData . push . apply ( returnData , responseData . response as IDataObject [ ] ) ;
} catch ( error ) {
throw error ;
2020-01-23 07:13:57 -08:00
}
2021-07-19 23:58:54 -07:00
} else if ( operation === 'getCategories' ) {
// ----------------------------------
// getCategories
// ----------------------------------
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
requestMethod = 'GET' ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
endpoint = 'forums/listCategories.json' ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
const id = this . getNodeParameter ( 'id' , i ) as string ;
const returnAll = this . getNodeParameter ( 'returnAll' , i ) as boolean ;
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) as IDataObject ;
Object . assign ( qs , additionalFields ) ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
qs . forum = id ;
qs . limit = 100 ;
2020-01-23 07:13:57 -08:00
2021-07-19 23:58:54 -07:00
try {
let responseData : IDataObject = { } ;
2020-01-23 07:13:57 -08:00
2021-07-19 23:58:54 -07:00
if ( returnAll ) {
responseData . response = await disqusApiRequestAllItems . call ( this , requestMethod , qs , endpoint ) ;
} else {
const limit = this . getNodeParameter ( 'limit' , i ) as string ;
qs . limit = limit ;
responseData = await disqusApiRequest . call ( this , requestMethod , qs , endpoint ) as IDataObject ;
}
returnData . push . apply ( returnData , responseData . response as IDataObject [ ] ) ;
} catch ( error ) {
throw error ;
2020-01-23 07:13:57 -08:00
}
2021-07-19 23:58:54 -07:00
} else if ( operation === 'getThreads' ) {
// ----------------------------------
// getThreads
// ----------------------------------
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
requestMethod = 'GET' ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
endpoint = 'forums/listThreads.json' ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
const id = this . getNodeParameter ( 'id' , i ) as string ;
const returnAll = this . getNodeParameter ( 'returnAll' , i ) as boolean ;
2020-01-23 07:13:57 -08:00
2021-07-19 23:58:54 -07:00
qs . forum = id ;
qs . limit = 100 ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) as IDataObject ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
Object . assign ( qs , additionalFields ) ;
2020-01-21 05:53:54 -08:00
2021-07-19 23:58:54 -07:00
try {
let responseData : IDataObject = { } ;
if ( returnAll ) {
responseData . response = await disqusApiRequestAllItems . call ( this , requestMethod , qs , endpoint ) ;
} else {
const limit = this . getNodeParameter ( 'limit' , i ) as string ;
qs . limit = limit ;
responseData = await disqusApiRequest . call ( this , requestMethod , qs , endpoint ) ;
}
returnData . push . apply ( returnData , responseData . response as IDataObject [ ] ) ;
} catch ( error ) {
throw error ;
2020-01-23 07:13:57 -08:00
}
2021-07-19 23:58:54 -07:00
} else {
throw new NodeOperationError ( this . getNode ( ) , ` The operation " ${ operation } " is not known! ` ) ;
2020-01-23 07:13:57 -08:00
}
2020-01-21 05:53:54 -08:00
} else {
2021-07-19 23:58:54 -07:00
throw new NodeOperationError ( this . getNode ( ) , ` The resource " ${ resource } " is not known! ` ) ;
2020-01-21 06:12:28 -08:00
}
2021-07-19 23:58:54 -07:00
} catch ( error ) {
if ( this . continueOnFail ( ) ) {
returnData . push ( { error : error.message } ) ;
continue ;
}
throw error ;
2020-01-21 05:53:54 -08:00
}
}
2020-01-21 06:12:28 -08:00
return [ this . helpers . returnJsonArray ( returnData ) ] ;
2020-01-21 05:53:54 -08:00
}
}