2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2020-11-10 10:15:56 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
|
2020-11-10 10:15:56 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { gotifyApiRequest, gotifyApiRequestAllItems } from './GenericFunctions';
|
2020-11-10 10:15:56 -08:00
|
|
|
|
|
|
|
export class Gotify implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Gotify',
|
|
|
|
name: 'gotify',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-11-10 10:15:56 -08:00
|
|
|
icon: 'file:gotify.png',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Consume Gotify API',
|
2020-11-10 10:15:56 -08:00
|
|
|
defaults: {
|
|
|
|
name: 'Gotify',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'gotifyApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-11-10 10:15:56 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Message',
|
|
|
|
value: 'message',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'message',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-11-10 10:15:56 -08:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['message'],
|
2020-11-10 10:15:56 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Create',
|
|
|
|
value: 'create',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Create a message',
|
2020-11-10 10:15:56 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Delete',
|
|
|
|
value: 'delete',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Delete a message',
|
2020-11-10 10:15:56 -08:00
|
|
|
},
|
|
|
|
{
|
2022-09-07 07:51:14 -07:00
|
|
|
name: 'Get Many',
|
2020-11-10 10:15:56 -08:00
|
|
|
value: 'getAll',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Get all messages',
|
2020-11-10 10:15:56 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'create',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Message',
|
|
|
|
name: 'message',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['message'],
|
|
|
|
operation: ['create'],
|
2020-11-10 10:15:56 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The message. Markdown (excluding html) is allowed.',
|
2020-11-10 10:15:56 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Additional Fields',
|
|
|
|
name: 'additionalFields',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Field',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['message'],
|
|
|
|
operation: ['create'],
|
2020-11-10 10:15:56 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Priority',
|
|
|
|
name: 'priority',
|
|
|
|
type: 'number',
|
|
|
|
default: 1,
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The priority of the message',
|
2020-11-10 10:15:56 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Title',
|
|
|
|
name: 'title',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The title of the message',
|
2020-11-10 10:15:56 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Message ID',
|
|
|
|
name: 'messageId',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['message'],
|
|
|
|
operation: ['delete'],
|
2020-11-10 10:15:56 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Return All',
|
|
|
|
name: 'returnAll',
|
|
|
|
type: 'boolean',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['message'],
|
|
|
|
operation: ['getAll'],
|
2020-11-10 10:15:56 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: false,
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Whether to return all results or only up to a given limit',
|
2020-11-10 10:15:56 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Limit',
|
|
|
|
name: 'limit',
|
|
|
|
type: 'number',
|
2022-05-20 14:47:24 -07:00
|
|
|
typeOptions: {
|
|
|
|
minValue: 1,
|
|
|
|
},
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Max number of results to return',
|
2020-11-10 10:15:56 -08:00
|
|
|
default: 20,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['message'],
|
|
|
|
operation: ['getAll'],
|
|
|
|
returnAll: [false],
|
2020-11-10 10:15:56 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-11-10 10:15:56 -08:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'message') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const message = this.getNodeParameter('message', i) as string;
|
2020-11-10 10:15:56 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
2020-11-10 10:15:56 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
message,
|
|
|
|
};
|
2020-11-10 10:16:35 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, additionalFields);
|
2020-11-10 10:15:56 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await gotifyApiRequest.call(this, 'POST', `/message`, body);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const messageId = this.getNodeParameter('messageId', i) as string;
|
2020-11-10 10:15:56 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await gotifyApiRequest.call(this, 'DELETE', `/message/${messageId}`);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
|
|
|
|
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await gotifyApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'messages',
|
|
|
|
'GET',
|
|
|
|
'/message',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
qs.limit = this.getNodeParameter('limit', i) as number;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await gotifyApiRequest.call(this, 'GET', `/message`, {}, qs);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.messages;
|
|
|
|
}
|
2020-11-10 10:15:56 -08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
returnData.push({json:{ error: error.message }});
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-11-10 10:15:56 -08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2020-11-10 10:15:56 -08:00
|
|
|
}
|
|
|
|
}
|