2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2023-01-27 03:22:44 -08:00
|
|
|
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-09-08 08:10:13 -07:00
|
|
|
action: 'Get many 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: '',
|
2024-01-26 02:33:23 -08:00
|
|
|
description: 'The message to send, If using Markdown add the Content Type option',
|
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
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2024-01-26 02:33:23 -08:00
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Option',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
resource: ['message'],
|
|
|
|
operation: ['create'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Content Type',
|
|
|
|
name: 'contentType',
|
|
|
|
type: 'options',
|
|
|
|
default: 'text/plain',
|
|
|
|
description: 'The message content type',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Plain',
|
|
|
|
value: 'text/plain',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Markdown',
|
|
|
|
value: 'text/markdown',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
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;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-11-10 10:15:56 -08:00
|
|
|
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
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2024-01-26 02:33:23 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
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
|
|
|
|
2024-01-26 02:33:23 -08:00
|
|
|
if (options.contentType) {
|
|
|
|
body.extras = {
|
|
|
|
'client::display': {
|
|
|
|
contentType: options.contentType,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, additionalFields);
|
2020-11-10 10:15:56 -08:00
|
|
|
|
2022-12-29 03:20:43 -08: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') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await gotifyApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'messages',
|
|
|
|
'GET',
|
|
|
|
'/message',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.limit = this.getNodeParameter('limit', i);
|
2022-12-29 03:20:43 -08: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(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
2024-06-19 22:45:00 -07:00
|
|
|
if (this.continueOnFail(error)) {
|
2022-11-18 06:26:22 -08: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
|
|
|
}
|
|
|
|
}
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2020-11-10 10:15:56 -08:00
|
|
|
}
|
|
|
|
}
|