2020-04-22 10:08:53 -07:00
import {
IExecuteFunctions ,
2020-10-03 10:18:42 -07:00
} from 'n8n-core' ;
2020-04-22 10:08:53 -07:00
2019-10-20 13:57:06 -07:00
import {
IDataObject ,
ILoadOptionsFunctions ,
INodeExecutionData ,
2020-10-01 05:01:39 -07:00
INodePropertyOptions ,
2019-10-20 13:57:06 -07:00
INodeType ,
2020-10-01 05:01:39 -07:00
INodeTypeDescription ,
2021-04-16 09:33:36 -07:00
NodeOperationError ,
2019-10-20 13:57:06 -07:00
} from 'n8n-workflow' ;
import {
apiRequest ,
2020-04-22 10:08:53 -07:00
apiRequestAllItems ,
2019-10-21 15:12:35 -07:00
IAttachment ,
2019-10-20 13:57:06 -07:00
} from './GenericFunctions' ;
2020-04-22 10:08:53 -07:00
import {
snakeCase ,
} from 'change-case' ;
2019-10-20 13:57:06 -07:00
export class Mattermost implements INodeType {
description : INodeTypeDescription = {
displayName : 'Mattermost' ,
name : 'mattermost' ,
2021-03-24 09:59:45 -07:00
icon : 'file:mattermost.svg' ,
2019-10-20 13:57:06 -07:00
group : [ 'output' ] ,
version : 1 ,
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
description : 'Sends data to Mattermost' ,
defaults : {
name : 'Mattermost' ,
2020-04-23 23:14:19 -07:00
color : '#000000' ,
2019-10-20 13:57:06 -07:00
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'mattermostApi' ,
required : true ,
2020-10-22 06:46:03 -07:00
} ,
2019-10-20 13:57:06 -07:00
] ,
properties : [
{
displayName : 'Resource' ,
name : 'resource' ,
type : 'options' ,
options : [
{
name : 'Channel' ,
value : 'channel' ,
} ,
{
name : 'Message' ,
value : 'message' ,
} ,
2021-03-24 09:59:45 -07:00
{
name : 'Reaction' ,
value : 'reaction' ,
} ,
2019-12-04 12:57:56 -08:00
{
name : 'User' ,
value : 'user' ,
} ,
2019-10-20 13:57:06 -07:00
] ,
default : 'message' ,
2020-07-07 02:12:23 -07:00
description : 'The resource to operate on' ,
2019-10-20 13:57:06 -07:00
} ,
// ----------------------------------
// operations
// ----------------------------------
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
displayOptions : {
show : {
resource : [
'channel' ,
] ,
} ,
} ,
options : [
{
name : 'Add User' ,
value : 'addUser' ,
description : 'Add a user to a channel' ,
} ,
{
name : 'Create' ,
value : 'create' ,
description : 'Create a new channel' ,
} ,
2020-01-09 18:05:57 -08:00
{
name : 'Delete' ,
value : 'delete' ,
2020-07-07 02:12:23 -07:00
description : 'Soft delete a channel' ,
2020-01-09 18:05:57 -08:00
} ,
2020-05-05 17:50:01 -07:00
{
2020-05-22 14:10:52 -07:00
name : 'Member' ,
2020-05-06 01:29:09 -07:00
value : 'members' ,
2020-07-07 02:12:23 -07:00
description : 'Get a page of members for a channel' ,
2020-05-05 17:50:01 -07:00
} ,
2020-01-09 18:05:57 -08:00
{
name : 'Restore' ,
value : 'restore' ,
2020-07-07 02:12:23 -07:00
description : 'Restores a soft deleted channel' ,
2020-01-09 18:05:57 -08:00
} ,
2019-12-04 12:57:56 -08:00
{
name : 'Statistics' ,
value : 'statistics' ,
2020-07-07 02:12:23 -07:00
description : 'Get statistics for a channel' ,
2019-12-04 12:57:56 -08:00
} ,
2019-10-20 13:57:06 -07:00
] ,
default : 'create' ,
description : 'The operation to perform.' ,
} ,
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
displayOptions : {
show : {
resource : [
'message' ,
] ,
} ,
} ,
options : [
2019-12-06 13:33:26 -08:00
{
name : 'Delete' ,
value : 'delete' ,
2020-07-07 02:12:23 -07:00
description : 'Soft delete a post, by marking the post as deleted in the database' ,
2019-12-06 13:33:26 -08:00
} ,
2019-10-20 13:57:06 -07:00
{
name : 'Post' ,
value : 'post' ,
description : 'Post a message into a channel' ,
} ,
2021-03-24 09:59:45 -07:00
{
name : 'Post Ephemeral' ,
value : 'postEphemeral' ,
description : 'Post an ephemeral message into a channel' ,
} ,
2019-10-20 13:57:06 -07:00
] ,
default : 'post' ,
2020-07-07 02:12:23 -07:00
description : 'The operation to perform' ,
2019-10-20 13:57:06 -07:00
} ,
2021-03-24 09:59:45 -07:00
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
displayOptions : {
show : {
resource : [
'reaction' ,
] ,
} ,
} ,
options : [
{
name : 'Create' ,
value : 'create' ,
description : 'Add a reaction to a post.' ,
} ,
{
name : 'Delete' ,
value : 'delete' ,
description : 'Remove a reaction from a post' ,
} ,
{
name : 'Get All' ,
value : 'getAll' ,
description : 'Get all the reactions to one or more posts' ,
} ,
] ,
default : 'create' ,
description : 'The operation to perform' ,
} ,
2019-10-20 13:57:06 -07:00
// ----------------------------------
// channel
// ----------------------------------
// ----------------------------------
// channel:create
// ----------------------------------
{
displayName : 'Team ID' ,
name : 'teamId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getTeams' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'create' ,
2019-10-20 13:57:06 -07:00
] ,
resource : [
'channel' ,
] ,
} ,
} ,
description : 'The Mattermost Team.' ,
} ,
{
displayName : 'Display Name' ,
name : 'displayName' ,
type : 'string' ,
default : '' ,
placeholder : 'Announcements' ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'create' ,
2019-10-20 13:57:06 -07:00
] ,
resource : [
'channel' ,
] ,
} ,
} ,
required : true ,
2020-07-07 02:12:23 -07:00
description : 'The non-unique UI name for the channel' ,
2019-10-20 13:57:06 -07:00
} ,
{
displayName : 'Name' ,
name : 'channel' ,
type : 'string' ,
default : '' ,
placeholder : 'announcements' ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'create' ,
2019-10-20 13:57:06 -07:00
] ,
resource : [
'channel' ,
] ,
} ,
} ,
required : true ,
2020-07-07 02:12:23 -07:00
description : 'The unique handle for the channel, will be present in the channel URL' ,
2019-10-20 13:57:06 -07:00
} ,
2019-10-21 15:12:35 -07:00
{
displayName : 'Type' ,
name : 'type' ,
type : 'options' ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'create' ,
2019-10-21 15:12:35 -07:00
] ,
resource : [
'channel' ,
] ,
} ,
} ,
options : [
{
name : 'Private' ,
value : 'private' ,
} ,
{
name : 'Public' ,
value : 'public' ,
} ,
] ,
default : 'public' ,
description : 'The type of channel to create.' ,
} ,
2019-10-20 13:57:06 -07:00
2020-01-09 18:05:57 -08:00
// ----------------------------------
// channel:delete
// ----------------------------------
{
displayName : 'Channel ID' ,
name : 'channelId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getChannels' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'delete' ,
2020-01-09 18:05:57 -08:00
] ,
resource : [
'channel' ,
] ,
} ,
} ,
2020-07-07 02:12:23 -07:00
description : 'The ID of the channel to soft delete' ,
2020-01-09 18:05:57 -08:00
} ,
2020-05-05 17:50:01 -07:00
// ----------------------------------
2020-05-06 01:29:09 -07:00
// channel:members
2020-05-05 17:50:01 -07:00
// ----------------------------------
{
displayName : 'Team ID' ,
name : 'teamId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getTeams' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-05-06 01:29:09 -07:00
'members' ,
2020-05-05 17:50:01 -07:00
] ,
resource : [
'channel' ,
] ,
} ,
} ,
description : 'The Mattermost Team.' ,
} ,
{
displayName : 'Channel ID' ,
name : 'channelId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getChannelsInTeam' ,
loadOptionsDependsOn : [
'teamId' ,
] ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-05-06 01:29:09 -07:00
'members' ,
2020-05-05 17:50:01 -07:00
] ,
resource : [
'channel' ,
] ,
} ,
} ,
description : 'The Mattermost Team.' ,
} ,
2020-05-22 14:10:52 -07:00
{
displayName : 'Resolve Data' ,
name : 'resolveData' ,
type : 'boolean' ,
displayOptions : {
show : {
resource : [
'channel' ,
] ,
operation : [
'members' ,
] ,
} ,
} ,
default : true ,
description : 'By default the response only contain the ID of the user.<br />If this option gets activated it will resolve the user automatically.' ,
} ,
2020-05-05 17:50:01 -07:00
{
displayName : 'Return All' ,
name : 'returnAll' ,
type : 'boolean' ,
displayOptions : {
show : {
2020-05-06 01:29:09 -07:00
operation : [
'members' ,
] ,
2020-05-05 17:50:01 -07:00
resource : [
'channel' ,
] ,
} ,
} ,
default : true ,
description : 'If all results should be returned or only up to a given limit.' ,
} ,
{
displayName : 'Limit' ,
name : 'limit' ,
type : 'number' ,
displayOptions : {
show : {
2020-05-06 01:29:09 -07:00
operation : [
'members' ,
] ,
2020-05-05 17:50:01 -07:00
resource : [
'channel' ,
] ,
returnAll : [
false ,
] ,
} ,
} ,
typeOptions : {
minValue : 1 ,
maxValue : 100 ,
} ,
default : 100 ,
description : 'How many results to return.' ,
} ,
2020-01-09 18:05:57 -08:00
// ----------------------------------
// channel:restore
// ----------------------------------
{
displayName : 'Channel ID' ,
name : 'channelId' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'restore' ,
2020-01-09 18:05:57 -08:00
] ,
resource : [
'channel' ,
] ,
} ,
} ,
description : 'The ID of the channel to restore.' ,
} ,
2019-10-20 13:57:06 -07:00
// ----------------------------------
// channel:addUser
// ----------------------------------
{
displayName : 'Channel ID' ,
name : 'channelId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getChannels' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'addUser' ,
2019-10-20 13:57:06 -07:00
] ,
resource : [
'channel' ,
] ,
} ,
} ,
description : 'The ID of the channel to invite user to.' ,
} ,
{
displayName : 'User ID' ,
name : 'userId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getUsers' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'addUser' ,
2019-10-20 13:57:06 -07:00
] ,
resource : [
'channel' ,
] ,
} ,
} ,
description : 'The ID of the user to invite into channel.' ,
} ,
2020-01-09 18:05:57 -08:00
2019-12-04 12:57:56 -08:00
// ----------------------------------
// channel:statistics
// ----------------------------------
{
displayName : 'Channel ID' ,
name : 'channelId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getChannels' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'statistics' ,
2019-12-04 12:57:56 -08:00
] ,
resource : [
'channel' ,
] ,
} ,
} ,
description : 'The ID of the channel to get the statistics from.' ,
} ,
2019-10-20 13:57:06 -07:00
// ----------------------------------
// message
// ----------------------------------
2019-12-06 13:33:26 -08:00
// ----------------------------------
// message:delete
// ----------------------------------
{
displayName : 'Post ID' ,
name : 'postId' ,
type : 'string' ,
required : true ,
displayOptions : {
show : {
resource : [
'message' ,
] ,
operation : [
'delete' ,
] ,
} ,
} ,
default : '' ,
description : 'ID of the post to delete' ,
} ,
2019-10-20 13:57:06 -07:00
// ----------------------------------
// message:post
// ----------------------------------
{
displayName : 'Channel ID' ,
name : 'channelId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getChannels' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'post' ,
2019-10-20 13:57:06 -07:00
] ,
resource : [
'message' ,
] ,
} ,
} ,
description : 'The ID of the channel to post to.' ,
} ,
{
displayName : 'Message' ,
name : 'message' ,
type : 'string' ,
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
default : '' ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'post' ,
2019-10-20 13:57:06 -07:00
] ,
resource : [
'message' ,
] ,
} ,
} ,
description : 'The text to send.' ,
} ,
2019-10-21 15:12:35 -07:00
{
displayName : 'Attachments' ,
name : 'attachments' ,
type : 'collection' ,
typeOptions : {
multipleValues : true ,
multipleValueButtonText : 'Add attachment' ,
} ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'post' ,
2019-10-21 15:12:35 -07:00
] ,
resource : [
'message' ,
] ,
} ,
} ,
default : { } ,
description : 'The attachment to add' ,
placeholder : 'Add attachment item' ,
options : [
{
2020-04-14 08:46:56 -07:00
displayName : 'Actions' ,
name : 'actions' ,
placeholder : 'Add Actions' ,
2020-04-14 09:23:43 -07:00
description : 'Actions to add to message. More information can be found <a href="https://docs.mattermost.com/developer/interactive-messages.html" target="_blank">here</a>' ,
2020-04-14 08:46:56 -07:00
type : 'fixedCollection' ,
2019-10-21 15:12:35 -07:00
typeOptions : {
2020-04-14 08:46:56 -07:00
multipleValues : true ,
2019-10-21 15:12:35 -07:00
} ,
2020-04-14 08:46:56 -07:00
default : { } ,
options : [
{
displayName : 'Item' ,
name : 'item' ,
values : [
2020-04-14 09:23:43 -07:00
{
displayName : 'Type' ,
name : 'type' ,
type : 'options' ,
options : [
{
name : 'Button' ,
value : 'button' ,
} ,
{
name : 'Select' ,
value : 'select' ,
} ,
] ,
default : 'button' ,
description : 'The type of the action.' ,
} ,
{
displayName : 'Data Source' ,
name : 'data_source' ,
type : 'options' ,
displayOptions : {
show : {
type : [
2020-10-22 06:46:03 -07:00
'select' ,
2020-04-14 09:23:43 -07:00
] ,
} ,
} ,
options : [
{
name : 'Channels' ,
value : 'channels' ,
} ,
{
name : 'Custom' ,
value : 'custom' ,
} ,
{
name : 'Users' ,
value : 'users' ,
} ,
] ,
default : 'custom' ,
description : 'The type of the action.' ,
} ,
{
displayName : 'Options' ,
name : 'options' ,
placeholder : 'Add Option' ,
description : 'Adds a new option to select field.' ,
type : 'fixedCollection' ,
typeOptions : {
multipleValues : true ,
} ,
displayOptions : {
show : {
data_source : [
2020-10-22 06:46:03 -07:00
'custom' ,
2020-04-14 09:23:43 -07:00
] ,
2020-04-14 10:17:55 -07:00
type : [
2020-10-22 06:46:03 -07:00
'select' ,
2020-04-14 10:17:55 -07:00
] ,
2020-04-14 09:23:43 -07:00
} ,
} ,
default : { } ,
options : [
{
name : 'option' ,
displayName : 'Option' ,
default : { } ,
values : [
{
displayName : 'Option Text' ,
name : 'text' ,
type : 'string' ,
default : '' ,
description : 'Text of the option.' ,
} ,
{
displayName : 'Option Value' ,
name : 'value' ,
type : 'string' ,
default : '' ,
description : 'Value of the option.' ,
} ,
2020-10-22 06:46:03 -07:00
] ,
2020-04-14 09:23:43 -07:00
} ,
] ,
} ,
2020-04-14 08:46:56 -07:00
{
displayName : 'Name' ,
name : 'name' ,
type : 'string' ,
default : '' ,
description : 'Name of the Action.' ,
} ,
{
displayName : 'Integration' ,
name : 'integration' ,
placeholder : 'Add Integration' ,
description : 'Integration to add to message.' ,
type : 'fixedCollection' ,
typeOptions : {
multipleValues : false ,
} ,
default : { } ,
options : [
{
displayName : 'Item' ,
name : 'item' ,
default : { } ,
values : [
{
displayName : 'URL' ,
name : 'url' ,
type : 'string' ,
default : '' ,
description : 'URL of the Integration.' ,
} ,
{
displayName : 'Context' ,
name : 'context' ,
placeholder : 'Add Context to Integration' ,
description : 'Adds a Context values set.' ,
type : 'fixedCollection' ,
typeOptions : {
multipleValues : true ,
} ,
default : { } ,
options : [
{
name : 'property' ,
displayName : 'Property' ,
default : { } ,
values : [
{
displayName : 'Property Name' ,
name : 'name' ,
type : 'string' ,
default : '' ,
description : 'Name of the property to set.' ,
} ,
{
displayName : 'Property Value' ,
name : 'value' ,
type : 'string' ,
default : '' ,
description : 'Value of the property to set.' ,
} ,
2020-10-22 06:46:03 -07:00
] ,
2020-04-14 08:46:56 -07:00
} ,
] ,
} ,
2020-10-22 06:46:03 -07:00
] ,
2020-04-14 08:46:56 -07:00
} ,
] ,
} ,
2020-10-22 06:46:03 -07:00
] ,
2020-04-14 08:46:56 -07:00
} ,
] ,
2019-10-21 15:12:35 -07:00
} ,
{
2020-04-14 08:46:56 -07:00
displayName : 'Author Icon' ,
name : 'author_icon' ,
2019-10-21 15:12:35 -07:00
type : 'string' ,
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
default : '' ,
2020-04-14 08:46:56 -07:00
description : 'Icon which should appear for the user.' ,
2019-10-21 15:12:35 -07:00
} ,
{
2020-04-14 08:46:56 -07:00
displayName : 'Author Link' ,
name : 'author_link' ,
2019-10-21 15:12:35 -07:00
type : 'string' ,
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
default : '' ,
2020-04-14 08:46:56 -07:00
description : 'Link for the author.' ,
2019-10-21 15:12:35 -07:00
} ,
{
2020-04-14 08:46:56 -07:00
displayName : 'Author Name' ,
name : 'author_name' ,
2019-10-21 15:12:35 -07:00
type : 'string' ,
default : '' ,
2020-04-14 08:46:56 -07:00
description : 'Name that should appear.' ,
2019-10-21 15:12:35 -07:00
} ,
{
displayName : 'Color' ,
name : 'color' ,
type : 'color' ,
default : '#ff0000' ,
description : 'Color of the line left of text.' ,
} ,
{
2020-04-14 08:46:56 -07:00
displayName : 'Fallback Text' ,
name : 'fallback' ,
2019-10-21 15:12:35 -07:00
type : 'string' ,
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
default : '' ,
2020-04-14 08:46:56 -07:00
description : 'Required plain-text summary of the attachment.' ,
2019-10-21 15:12:35 -07:00
} ,
{
2020-04-14 08:46:56 -07:00
displayName : 'Fields' ,
name : 'fields' ,
placeholder : 'Add Fields' ,
description : 'Fields to add to message.' ,
type : 'fixedCollection' ,
typeOptions : {
multipleValues : true ,
} ,
default : { } ,
options : [
{
name : 'item' ,
displayName : 'Item' ,
values : [
{
displayName : 'Title' ,
name : 'title' ,
type : 'string' ,
default : '' ,
description : 'Title of the item.' ,
} ,
{
displayName : 'Value' ,
name : 'value' ,
type : 'string' ,
default : '' ,
description : 'Value of the item.' ,
} ,
{
displayName : 'Short' ,
name : 'short' ,
type : 'boolean' ,
default : true ,
description : 'If items can be displayed next to each other.' ,
} ,
2020-10-22 06:46:03 -07:00
] ,
2020-04-14 08:46:56 -07:00
} ,
] ,
2019-10-21 15:12:35 -07:00
} ,
{
2020-04-14 08:46:56 -07:00
displayName : 'Footer' ,
name : 'footer' ,
2019-10-21 15:12:35 -07:00
type : 'string' ,
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
default : '' ,
2020-04-14 08:46:56 -07:00
description : 'Text of footer to add.' ,
2019-10-21 15:12:35 -07:00
} ,
{
2020-04-14 08:46:56 -07:00
displayName : 'Footer Icon' ,
name : 'footer_icon' ,
2019-10-21 15:12:35 -07:00
type : 'string' ,
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
default : '' ,
2020-04-14 08:46:56 -07:00
description : 'Icon which should appear next to footer.' ,
2019-10-21 15:12:35 -07:00
} ,
{
displayName : 'Image URL' ,
name : 'image_url' ,
type : 'string' ,
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
default : '' ,
description : 'URL of image.' ,
} ,
{
2020-04-14 08:46:56 -07:00
displayName : 'Pretext' ,
name : 'pretext' ,
2019-10-21 15:12:35 -07:00
type : 'string' ,
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
default : '' ,
2020-04-14 08:46:56 -07:00
description : 'Text which appears before the message block.' ,
2019-10-21 15:12:35 -07:00
} ,
{
2020-04-14 08:46:56 -07:00
displayName : 'Text' ,
name : 'text' ,
2019-10-21 15:12:35 -07:00
type : 'string' ,
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
default : '' ,
2020-04-14 08:46:56 -07:00
description : 'Text to send.' ,
2019-10-21 15:12:35 -07:00
} ,
{
2020-04-14 08:46:56 -07:00
displayName : 'Thumbnail URL' ,
name : 'thumb_url' ,
2019-10-21 15:12:35 -07:00
type : 'string' ,
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
default : '' ,
2020-04-14 08:46:56 -07:00
description : 'URL of thumbnail.' ,
2019-10-21 15:12:35 -07:00
} ,
2020-04-14 08:46:56 -07:00
{
displayName : 'Title' ,
name : 'title' ,
type : 'string' ,
2020-04-11 00:42:00 -07:00
typeOptions : {
2020-04-14 08:46:56 -07:00
alwaysOpenEditWindow : true ,
2020-04-11 00:42:00 -07:00
} ,
2020-04-14 08:46:56 -07:00
default : '' ,
description : 'Title of the message.' ,
2020-04-11 00:42:00 -07:00
} ,
2019-10-21 15:12:35 -07:00
{
2020-04-14 08:46:56 -07:00
displayName : 'Title Link' ,
name : 'title_link' ,
type : 'string' ,
2019-10-21 15:12:35 -07:00
typeOptions : {
2020-04-14 08:46:56 -07:00
alwaysOpenEditWindow : true ,
2019-10-21 15:12:35 -07:00
} ,
2020-04-14 08:46:56 -07:00
default : '' ,
description : 'Link of the title.' ,
} ,
2019-10-21 15:12:35 -07:00
] ,
} ,
2021-03-24 09:59:45 -07:00
// ----------------------------------
// message:post (ephemeral)
// ----------------------------------
{
displayName : 'User ID' ,
name : 'userId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getUsers' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
'postEphemeral' ,
] ,
resource : [
'message' ,
] ,
} ,
} ,
description : 'ID of the user to send the ephemeral message to.' ,
} ,
{
displayName : 'Channel ID' ,
name : 'channelId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getChannels' ,
} ,
default : '' ,
required : true ,
displayOptions : {
show : {
operation : [
'postEphemeral' ,
] ,
resource : [
'message' ,
] ,
} ,
} ,
description : 'ID of the channel to send the ephemeral message in.' ,
} ,
{
displayName : 'Message' ,
name : 'message' ,
type : 'string' ,
typeOptions : {
alwaysOpenEditWindow : true ,
} ,
default : '' ,
displayOptions : {
show : {
operation : [
'postEphemeral' ,
] ,
resource : [
'message' ,
] ,
} ,
} ,
description : 'Text to send in the ephemeral message.' ,
} ,
2019-10-21 15:12:35 -07:00
{
displayName : 'Other Options' ,
name : 'otherOptions' ,
type : 'collection' ,
displayOptions : {
show : {
operation : [
2020-10-22 06:46:03 -07:00
'post' ,
2019-10-21 15:12:35 -07:00
] ,
resource : [
'message' ,
] ,
} ,
} ,
default : { } ,
description : 'Other options to set' ,
placeholder : 'Add options' ,
options : [
{
displayName : 'Make Comment' ,
name : 'root_id' ,
type : 'string' ,
default : '' ,
description : 'The post ID to comment on' ,
} ,
] ,
} ,
2021-03-24 09:59:45 -07:00
// ----------------------------------
// reaction
// ----------------------------------
{
displayName : 'User ID' ,
name : 'userId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getUsers' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
resource : [
'reaction' ,
] ,
operation : [
'create' ,
] ,
} ,
} ,
description : 'ID of the user sending the reaction.' ,
} ,
{
displayName : 'Post ID' ,
name : 'postId' ,
type : 'string' ,
default : '' ,
placeholder : '3moacfqxmbdw38r38fjprh6zsr' ,
required : true ,
displayOptions : {
show : {
resource : [
'reaction' ,
] ,
operation : [
'create' ,
] ,
} ,
} ,
description : 'ID of the post to react to.<br>Obtainable from the post link:<br><code>https://mattermost.internal.n8n.io/[server]/pl/[postId]</code>' ,
} ,
{
displayName : 'Emoji Name' ,
name : 'emojiName' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
resource : [
'reaction' ,
] ,
operation : [
'create' ,
] ,
} ,
} ,
description : 'Emoji to use for this reaction.' ,
} ,
{
displayName : 'User ID' ,
name : 'userId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getUsers' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
displayOptions : {
show : {
resource : [
'reaction' ,
] ,
operation : [
'delete' ,
] ,
} ,
} ,
description : 'ID of the user whose reaction to delete.' ,
} ,
{
displayName : 'Post ID' ,
name : 'postId' ,
type : 'string' ,
default : '' ,
placeholder : '3moacfqxmbdw38r38fjprh6zsr' ,
required : true ,
displayOptions : {
show : {
resource : [
'reaction' ,
] ,
operation : [
'delete' ,
] ,
} ,
} ,
description : 'ID of the post whose reaction to delete.<br>Obtainable from the post link:<br><code>https://mattermost.internal.n8n.io/[server]/pl/[postId]</code>' ,
} ,
{
displayName : 'Emoji Name' ,
name : 'emojiName' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
resource : [
'reaction' ,
] ,
operation : [
'delete' ,
] ,
} ,
} ,
description : 'Name of the emoji to delete.' ,
} ,
{
displayName : 'Post ID' ,
name : 'postId' ,
type : 'string' ,
default : '' ,
required : true ,
displayOptions : {
show : {
resource : [
'reaction' ,
] ,
operation : [
'getAll' ,
] ,
} ,
} ,
description : 'One or more (comma-separated) posts to retrieve reactions from.' ,
} ,
{
displayName : 'Return All' ,
name : 'returnAll' ,
type : 'boolean' ,
displayOptions : {
show : {
operation : [
'getAll' ,
] ,
resource : [
'reaction' ,
] ,
} ,
} ,
default : true ,
description : 'If all results should be returned or only up to a given limit.' ,
} ,
{
displayName : 'Limit' ,
name : 'limit' ,
type : 'number' ,
displayOptions : {
show : {
operation : [
'getAll' ,
] ,
resource : [
'reaction' ,
] ,
returnAll : [
false ,
] ,
} ,
} ,
typeOptions : {
minValue : 1 ,
maxValue : 100 ,
} ,
default : 100 ,
description : 'How many results to return.' ,
} ,
2019-12-04 12:57:56 -08:00
// ----------------------------------
// user
// ----------------------------------
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
} ,
} ,
options : [
2020-10-03 10:15:14 -07:00
{
name : 'Create' ,
value : 'create' ,
description : 'Create a new user' ,
} ,
2019-12-04 12:57:56 -08:00
{
2020-01-17 05:06:11 -08:00
name : 'Deactive' ,
value : 'deactive' ,
2019-12-04 12:57:56 -08:00
description : 'Deactivates the user and revokes all its sessions by archiving its user object.' ,
} ,
2020-04-22 10:08:53 -07:00
{
name : 'Get All' ,
value : 'getAll' ,
description : 'Retrieve all users' ,
} ,
2020-04-23 23:14:19 -07:00
{
name : 'Get By Email' ,
value : 'getByEmail' ,
description : 'Get a user by email' ,
} ,
2020-05-22 14:10:52 -07:00
{
name : 'Get By ID' ,
value : 'getById' ,
description : 'Get a user by id' ,
} ,
2020-10-03 10:15:14 -07:00
{
name : 'Invite' ,
value : 'invite' ,
description : 'Invite user to team' ,
} ,
2019-12-04 12:57:56 -08:00
] ,
default : '' ,
description : 'The operation to perform.' ,
} ,
2020-10-03 10:15:14 -07:00
// ----------------------------------
// user:create
// ----------------------------------
{
displayName : 'Username' ,
name : 'username' ,
type : 'string' ,
required : true ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'create' ,
] ,
} ,
} ,
default : '' ,
} ,
{
displayName : 'Auth Service' ,
2020-10-03 10:18:42 -07:00
name : 'authService' ,
2020-10-03 10:15:14 -07:00
type : 'options' ,
options : [
{
name : 'Email' ,
value : 'email' ,
} ,
{
name : 'Gitlab' ,
value : 'gitlab' ,
} ,
{
2020-10-03 10:18:42 -07:00
name : 'Google' ,
value : 'google' ,
2020-10-03 10:15:14 -07:00
} ,
{
2020-10-03 10:18:42 -07:00
name : 'LDAP' ,
value : 'ldap' ,
2020-10-03 10:15:14 -07:00
} ,
{
name : 'Office365' ,
value : 'office365' ,
} ,
{
2020-10-03 10:18:42 -07:00
name : 'SAML' ,
value : 'saml' ,
2020-10-03 10:15:14 -07:00
} ,
] ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'create' ,
] ,
} ,
} ,
default : '' ,
} ,
{
displayName : 'Auth Data' ,
2020-10-03 10:18:42 -07:00
name : 'authData' ,
2020-10-03 10:15:14 -07:00
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'create' ,
] ,
} ,
hide : {
2020-10-03 10:18:42 -07:00
authService : [
2020-10-03 10:15:14 -07:00
'email' ,
] ,
} ,
} ,
type : 'string' ,
default : '' ,
} ,
{
displayName : 'Email' ,
name : 'email' ,
type : 'string' ,
default : '' ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'create' ,
] ,
2020-10-03 10:18:42 -07:00
authService : [
2020-10-03 10:15:14 -07:00
'email' ,
] ,
} ,
} ,
} ,
{
displayName : 'Password' ,
name : 'password' ,
type : 'string' ,
typeOptions : {
password : true ,
} ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'create' ,
] ,
2020-10-03 10:18:42 -07:00
authService : [
2020-10-03 10:15:14 -07:00
'email' ,
] ,
} ,
} ,
default : '' ,
2020-10-22 06:46:03 -07:00
description : 'The password used for email authentication.' ,
2020-10-03 10:15:14 -07:00
} ,
{
displayName : 'Additional Fields' ,
name : 'additionalFields' ,
type : 'collection' ,
placeholder : 'Add Field' ,
displayOptions : {
show : {
operation : [
'create' ,
] ,
resource : [
'user' ,
] ,
} ,
} ,
default : { } ,
options : [
{
displayName : 'First Name' ,
name : 'first_name' ,
type : 'string' ,
default : '' ,
} ,
{
displayName : 'Last Name' ,
name : 'last_name' ,
type : 'string' ,
default : '' ,
} ,
{
displayName : 'Locale' ,
name : 'locale' ,
type : 'string' ,
default : '' ,
} ,
{
displayName : 'Nickname' ,
name : 'nickname' ,
type : 'string' ,
default : '' ,
} ,
{
displayName : 'Notification Settings' ,
name : 'notificationUi' ,
type : 'fixedCollection' ,
placeholder : 'Add Notification Setting' ,
default : { } ,
typeOptions : {
multipleValues : false ,
} ,
options : [
{
displayName : 'Notify' ,
name : 'notificationValues' ,
values : [
{
2020-10-03 10:18:42 -07:00
displayName : 'Channel' ,
name : 'channel' ,
2020-10-03 10:15:14 -07:00
type : 'boolean' ,
2020-10-03 10:18:42 -07:00
default : true ,
description : ` Set to "true" to enable channel-wide notifications (@channel, @all, etc.), "false" to disable. Defaults to "true". ` ,
2020-10-03 10:15:14 -07:00
} ,
{
displayName : 'Desktop' ,
name : 'desktop' ,
type : 'options' ,
options : [
{
name : 'All' ,
value : 'all' ,
description : 'Notifications for all activity' ,
} ,
{
name : 'Mention' ,
value : 'mention' ,
description : 'Mentions and direct messages only' ,
} ,
{
name : 'None' ,
value : 'none' ,
description : 'Mentions and direct messages only' ,
} ,
] ,
default : 'all' ,
} ,
{
displayName : 'Desktop Sound' ,
name : 'desktop_sound' ,
type : 'boolean' ,
default : true ,
description : ` Set to "true" to enable sound on desktop notifications, "false" to disable. Defaults to "true". ` ,
} ,
{
2020-10-03 10:18:42 -07:00
displayName : 'Email' ,
name : 'email' ,
2020-10-03 10:15:14 -07:00
type : 'boolean' ,
2020-10-03 10:18:42 -07:00
default : false ,
2020-10-22 06:46:03 -07:00
description : ` Set to "true" to enable email notifications, "false" to disable. Defaults to "true". ` ,
2020-10-03 10:15:14 -07:00
} ,
{
displayName : 'First Name' ,
name : 'first_name' ,
type : 'boolean' ,
default : false ,
description : ` Set to "true" to enable mentions for first name. Defaults to "true" if a first name is set, "false" otherwise. ` ,
} ,
2020-10-03 10:18:42 -07:00
{
displayName : 'Mention Keys' ,
name : 'mention_keys' ,
type : 'string' ,
default : '' ,
description : ` A comma-separated list of words to count as mentions. Defaults to username and @username. ` ,
} ,
{
displayName : 'Push' ,
name : 'push' ,
type : 'options' ,
options : [
{
name : 'All' ,
value : 'all' ,
description : 'Notifications for all activity' ,
} ,
{
name : 'Mention' ,
value : 'mention' ,
description : 'Mentions and direct messages only' ,
} ,
{
name : 'None' ,
value : 'none' ,
description : 'Mentions and direct messages only' ,
} ,
] ,
default : 'mention' ,
} ,
2020-10-03 10:15:14 -07:00
] ,
} ,
] ,
} ,
] ,
} ,
2019-12-04 12:57:56 -08:00
// ----------------------------------
2020-01-17 05:06:11 -08:00
// user:deactivate
2019-12-04 12:57:56 -08:00
// ----------------------------------
{
displayName : 'User ID' ,
name : 'userId' ,
type : 'string' ,
required : true ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
2020-01-17 05:06:11 -08:00
'deactive' ,
2019-12-04 12:57:56 -08:00
] ,
} ,
} ,
default : '' ,
2020-10-22 06:46:03 -07:00
description : 'User GUID' ,
2019-12-04 12:57:56 -08:00
} ,
2019-12-06 13:33:26 -08:00
2020-10-03 10:15:14 -07:00
// ----------------------------------
// user:invite
// ----------------------------------
{
displayName : 'Team ID' ,
name : 'teamId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getTeams' ,
} ,
required : true ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'invite' ,
] ,
} ,
} ,
default : '' ,
} ,
{
displayName : 'Emails' ,
name : 'emails' ,
type : 'string' ,
required : true ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'invite' ,
] ,
} ,
} ,
default : '' ,
description : ` User's email. Multiple can be set separated by comma. ` ,
} ,
2020-04-22 10:08:53 -07:00
// ----------------------------------
// user:getAll
// ----------------------------------
{
displayName : 'Return All' ,
name : 'returnAll' ,
type : 'boolean' ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'getAll' ,
] ,
} ,
} ,
default : true ,
description : 'If all results should be returned or only up to a given limit.' ,
} ,
{
displayName : 'Limit' ,
name : 'limit' ,
type : 'number' ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'getAll' ,
] ,
returnAll : [
false ,
] ,
} ,
} ,
typeOptions : {
minValue : 1 ,
maxValue : 100 ,
} ,
default : 100 ,
description : 'How many results to return.' ,
} ,
{
displayName : 'Additional Fields' ,
name : 'additionalFields' ,
type : 'collection' ,
placeholder : 'Add Field' ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'getAll' ,
] ,
} ,
} ,
default : { } ,
options : [
{
displayName : 'In Channel' ,
name : 'inChannel' ,
type : 'string' ,
default : '' ,
description : 'The ID of the channel to get users for.' ,
} ,
{
displayName : 'In Team' ,
name : 'inTeam' ,
type : 'string' ,
default : '' ,
description : 'The ID of the team to get users for.' ,
} ,
{
displayName : 'Not In Team' ,
name : 'notInTeam' ,
type : 'string' ,
default : '' ,
description : 'The ID of the team to exclude users for.' ,
} ,
{
displayName : 'Not In Channel' ,
name : 'notInChannel' ,
type : 'string' ,
default : '' ,
description : 'The ID of the channel to exclude users for.' ,
} ,
{
displayName : 'Sort' ,
name : 'sort' ,
type : 'options' ,
options : [
{
name : 'Created At' ,
value : 'createdAt' ,
} ,
{
name : 'Last Activity At' ,
value : 'lastActivityAt' ,
} ,
{
name : 'Status' ,
value : 'status' ,
} ,
{
name : 'username' ,
value : 'username' ,
} ,
] ,
default : 'username' ,
description : 'The ID of the channel to exclude users for.' ,
} ,
] ,
} ,
2020-04-23 23:14:19 -07:00
// ----------------------------------
// user:getByEmail
// ----------------------------------
{
displayName : 'Email' ,
name : 'email' ,
type : 'string' ,
required : true ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'getByEmail' ,
] ,
} ,
} ,
default : '' ,
description : ` User's email ` ,
} ,
2020-05-22 14:10:52 -07:00
// ----------------------------------
// user:getById
// ----------------------------------
{
displayName : 'User IDs' ,
name : 'userIds' ,
type : 'string' ,
required : true ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'getById' ,
] ,
} ,
} ,
default : '' ,
description : ` User's ID ` ,
} ,
{
displayName : 'Additional Fields' ,
name : 'additionalFields' ,
type : 'collection' ,
placeholder : 'Add Field' ,
displayOptions : {
show : {
resource : [
'user' ,
] ,
operation : [
'getById' ,
] ,
} ,
} ,
default : { } ,
options : [
{
displayName : 'Since' ,
name : 'since' ,
type : 'dateTime' ,
default : '' ,
description : 'Only return users that have been modified since the given Unix timestamp (in milliseconds).' ,
} ,
] ,
} ,
2019-10-20 13:57:06 -07:00
] ,
} ;
methods = {
loadOptions : {
2020-01-09 18:05:57 -08:00
// Get all the available channels
2019-10-20 13:57:06 -07:00
async getChannels ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const endpoint = 'channels' ;
const responseData = await apiRequest . call ( this , 'GET' , endpoint , { } ) ;
if ( responseData === undefined ) {
2021-04-16 09:33:36 -07:00
throw new NodeOperationError ( this . getNode ( ) , 'No data got returned' ) ;
2019-10-20 13:57:06 -07:00
}
const returnData : INodePropertyOptions [ ] = [ ] ;
let name : string ;
for ( const data of responseData ) {
2020-11-06 04:39:38 -08:00
if ( data . delete_at !== 0 || ( ! data . display_name || ! data . name ) ) {
2019-10-20 13:57:06 -07:00
continue ;
}
2020-11-06 04:39:38 -08:00
name = ` ${ data . team_display_name } - ${ data . display_name || data . name } ( ${ data . type === 'O' ? 'public' : 'private' } ) ` ;
2019-10-20 13:57:06 -07:00
returnData . push ( {
name ,
value : data.id ,
} ) ;
}
2020-11-06 04:39:38 -08:00
returnData . sort ( ( a , b ) = > {
if ( a . name < b . name ) { return - 1 ; }
if ( a . name > b . name ) { return 1 ; }
return 0 ;
} ) ;
2019-10-20 13:57:06 -07:00
return returnData ;
} ,
2020-05-05 17:50:01 -07:00
// Get all the channels in a team
async getChannelsInTeam ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const teamId = this . getCurrentNodeParameter ( 'teamId' ) ;
const endpoint = ` users/me/teams/ ${ teamId } /channels ` ;
const responseData = await apiRequest . call ( this , 'GET' , endpoint , { } ) ;
if ( responseData === undefined ) {
2021-04-16 09:33:36 -07:00
throw new NodeOperationError ( this . getNode ( ) , 'No data got returned' ) ;
2020-05-05 17:50:01 -07:00
}
const returnData : INodePropertyOptions [ ] = [ ] ;
let name : string ;
for ( const data of responseData ) {
2020-11-06 04:39:38 -08:00
if ( data . delete_at !== 0 || ( ! data . display_name || ! data . name ) ) {
2020-05-05 17:50:01 -07:00
continue ;
}
const channelTypes : IDataObject = {
2020-11-06 04:39:38 -08:00
'D' : 'direct' ,
'G' : 'group' ,
2020-05-05 17:50:01 -07:00
'O' : 'public' ,
'P' : 'private' ,
} ;
2019-10-20 13:57:06 -07:00
2020-11-06 04:39:38 -08:00
name = ` ${ data . display_name } ( ${ channelTypes [ data . type as string ] } ) ` ;
2020-05-05 17:50:01 -07:00
returnData . push ( {
name ,
value : data.id ,
} ) ;
}
2020-11-06 04:39:38 -08:00
returnData . sort ( ( a , b ) = > {
if ( a . name < b . name ) { return - 1 ; }
if ( a . name > b . name ) { return 1 ; }
return 0 ;
} ) ;
2020-05-05 17:50:01 -07:00
return returnData ;
} ,
2019-10-20 13:57:06 -07:00
async getTeams ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
2020-05-05 17:50:01 -07:00
const endpoint = 'users/me/teams' ;
2019-10-20 13:57:06 -07:00
const responseData = await apiRequest . call ( this , 'GET' , endpoint , { } ) ;
if ( responseData === undefined ) {
2021-04-16 09:33:36 -07:00
throw new NodeOperationError ( this . getNode ( ) , 'No data got returned' ) ;
2019-10-20 13:57:06 -07:00
}
const returnData : INodePropertyOptions [ ] = [ ] ;
let name : string ;
for ( const data of responseData ) {
if ( data . delete_at !== 0 ) {
continue ;
}
name = ` ${ data . display_name } ( ${ data . type === 'O' ? 'public' : 'private' } ) ` ;
returnData . push ( {
name ,
value : data.id ,
} ) ;
}
2020-11-06 04:39:38 -08:00
returnData . sort ( ( a , b ) = > {
if ( a . name < b . name ) { return - 1 ; }
if ( a . name > b . name ) { return 1 ; }
return 0 ;
} ) ;
2019-10-20 13:57:06 -07:00
return returnData ;
} ,
async getUsers ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const endpoint = 'users' ;
const responseData = await apiRequest . call ( this , 'GET' , endpoint , { } ) ;
if ( responseData === undefined ) {
2021-04-16 09:33:36 -07:00
throw new NodeOperationError ( this . getNode ( ) , 'No data got returned' ) ;
2019-10-20 13:57:06 -07:00
}
const returnData : INodePropertyOptions [ ] = [ ] ;
for ( const data of responseData ) {
if ( data . delete_at !== 0 ) {
continue ;
}
returnData . push ( {
name : data.username ,
value : data.id ,
} ) ;
}
2020-11-06 04:39:38 -08:00
returnData . sort ( ( a , b ) = > {
if ( a . name < b . name ) { return - 1 ; }
if ( a . name > b . name ) { return 1 ; }
return 0 ;
} ) ;
2019-10-20 13:57:06 -07:00
return returnData ;
} ,
} ,
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
const items = this . getInputData ( ) ;
const returnData : IDataObject [ ] = [ ] ;
const credentials = this . getCredentials ( 'mattermostApi' ) ;
if ( credentials === undefined ) {
2021-04-16 09:33:36 -07:00
throw new NodeOperationError ( this . getNode ( ) , 'No credentials got returned!' ) ;
2019-10-20 13:57:06 -07:00
}
let operation : string ;
let resource : string ;
let requestMethod = 'POST' ;
2020-04-22 10:08:53 -07:00
let returnAll = false ;
2020-05-22 14:10:52 -07:00
let userIds : string [ ] = [ ] ;
resource = this . getNodeParameter ( 'resource' , 0 ) as string ;
operation = this . getNodeParameter ( 'operation' , 0 ) as string ;
2019-10-20 13:57:06 -07:00
// For Post
let body : IDataObject ;
// For Query string
let qs : IDataObject ;
for ( let i = 0 ; i < items . length ; i ++ ) {
2021-07-19 23:58:54 -07:00
try {
let endpoint = '' ;
body = { } ;
qs = { } ;
if ( resource === 'channel' ) {
if ( operation === 'create' ) {
// ----------------------------------
// channel:create
// ----------------------------------
requestMethod = 'POST' ;
endpoint = 'channels' ;
body . team_id = this . getNodeParameter ( 'teamId' , i ) as string ;
body . display_name = this . getNodeParameter ( 'displayName' , i ) as string ;
body . name = this . getNodeParameter ( 'channel' , i ) as string ;
const type = this . getNodeParameter ( 'type' , i ) as string ;
body . type = type === 'public' ? 'O' : 'P' ;
} else if ( operation === 'delete' ) {
// ----------------------------------
// channel:delete
// ----------------------------------
requestMethod = 'DELETE' ;
const channelId = this . getNodeParameter ( 'channelId' , i ) as string ;
endpoint = ` channels/ ${ channelId } ` ;
} else if ( operation === 'members' ) {
// ----------------------------------
// channel:members
// ----------------------------------
requestMethod = 'GET' ;
const channelId = this . getNodeParameter ( 'channelId' , i ) as string ;
const returnAll = this . getNodeParameter ( 'returnAll' , i ) as boolean ;
endpoint = ` channels/ ${ channelId } /members ` ;
if ( returnAll === false ) {
qs . per_page = this . getNodeParameter ( 'limit' , i ) as number ;
}
2019-10-20 13:57:06 -07:00
2021-07-19 23:58:54 -07:00
} else if ( operation === 'restore' ) {
// ----------------------------------
// channel:restore
// ----------------------------------
2019-10-20 13:57:06 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'POST' ;
const channelId = this . getNodeParameter ( 'channelId' , i ) as string ;
endpoint = ` channels/ ${ channelId } /restore ` ;
2019-10-20 13:57:06 -07:00
2021-07-19 23:58:54 -07:00
} else if ( operation === 'addUser' ) {
// ----------------------------------
// channel:addUser
// ----------------------------------
2019-10-21 15:12:35 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'POST' ;
2019-10-20 13:57:06 -07:00
2021-07-19 23:58:54 -07:00
const channelId = this . getNodeParameter ( 'channelId' , i ) as string ;
body . user_id = this . getNodeParameter ( 'userId' , i ) as string ;
2020-01-09 18:05:57 -08:00
2021-07-19 23:58:54 -07:00
endpoint = ` channels/ ${ channelId } /members ` ;
2020-01-09 18:05:57 -08:00
2021-07-19 23:58:54 -07:00
} else if ( operation === 'statistics' ) {
// ----------------------------------
// channel:statistics
// ----------------------------------
2020-05-05 17:50:01 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'GET' ;
const channelId = this . getNodeParameter ( 'channelId' , i ) as string ;
endpoint = ` channels/ ${ channelId } /stats ` ;
2020-05-05 17:50:01 -07:00
}
2021-07-19 23:58:54 -07:00
} else if ( resource === 'message' ) {
if ( operation === 'delete' ) {
// ----------------------------------
// message:delete
// ----------------------------------
const postId = this . getNodeParameter ( 'postId' , i ) as string ;
requestMethod = 'DELETE' ;
endpoint = ` posts/ ${ postId } ` ;
} else if ( operation === 'post' ) {
// ----------------------------------
// message:post
// ----------------------------------
requestMethod = 'POST' ;
endpoint = 'posts' ;
body . channel_id = this . getNodeParameter ( 'channelId' , i ) as string ;
body . message = this . getNodeParameter ( 'message' , i ) as string ;
const attachments = this . getNodeParameter ( 'attachments' , i , [ ] ) as unknown as IAttachment [ ] ;
// The node does save the fields data differently than the API
// expects so fix the data befre we send the request
for ( const attachment of attachments ) {
if ( attachment . fields !== undefined ) {
if ( attachment . fields . item !== undefined ) {
// Move the field-content up
// @ts-ignore
attachment . fields = attachment . fields . item ;
} else {
// If it does not have any items set remove it
// @ts-ignore
delete attachment . fields ;
}
2019-10-21 15:12:35 -07:00
}
2020-04-11 04:49:12 -07:00
}
2021-07-19 23:58:54 -07:00
for ( const attachment of attachments ) {
if ( attachment . actions !== undefined ) {
if ( attachment . actions . item !== undefined ) {
// Move the field-content up
// @ts-ignore
attachment . actions = attachment . actions . item ;
} else {
// If it does not have any items set remove it
// @ts-ignore
delete attachment . actions ;
}
2020-04-11 04:07:37 -07:00
}
2020-04-11 04:49:12 -07:00
}
2020-04-14 08:46:56 -07:00
2021-07-19 23:58:54 -07:00
for ( const attachment of attachments ) {
if ( Array . isArray ( attachment . actions ) ) {
for ( const attaction of attachment . actions ) {
2020-04-14 09:23:43 -07:00
2021-07-19 23:58:54 -07:00
if ( attaction . type === 'button' ) {
delete attaction . type ;
}
if ( attaction . data_source === 'custom' ) {
delete attaction . data_source ;
}
if ( attaction . options ) {
attaction . options = attaction . options . option ;
}
2020-04-14 09:23:43 -07:00
2021-07-19 23:58:54 -07:00
if ( attaction . integration . item !== undefined ) {
attaction . integration = attaction . integration . item ;
if ( Array . isArray ( attaction . integration . context . property ) ) {
const tmpcontex = { } ;
for ( const attactionintegprop of attaction . integration . context . property ) {
Object . assign ( tmpcontex , { [ attactionintegprop . name ] : attactionintegprop . value } ) ;
}
delete attaction . integration . context ;
attaction . integration . context = tmpcontex ;
2020-04-14 08:46:56 -07:00
}
}
}
2020-04-13 14:14:16 -07:00
}
2020-04-14 08:46:56 -07:00
}
2020-04-12 09:48:01 -07:00
2021-07-19 23:58:54 -07:00
body . props = {
attachments ,
} ;
2019-10-21 15:12:35 -07:00
2021-07-19 23:58:54 -07:00
// Add all the other options to the request
const otherOptions = this . getNodeParameter ( 'otherOptions' , i ) as IDataObject ;
Object . assign ( body , otherOptions ) ;
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
} else if ( operation === 'postEphemeral' ) {
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
// ----------------------------------
// message:post (ephemeral)
// ----------------------------------
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
// https://api.mattermost.com/#tag/posts/paths/~1posts~1ephemeral/post
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
body = {
user_id : this.getNodeParameter ( 'userId' , i ) ,
post : {
channel_id : this.getNodeParameter ( 'channelId' , i ) ,
message : this.getNodeParameter ( 'message' , i ) ,
} ,
} as IDataObject ;
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'POST' ;
endpoint = 'posts/ephemeral' ;
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
}
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
} else if ( resource === 'reaction' ) {
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
// ----------------------------------
// reaction:create
// ----------------------------------
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
// https://api.mattermost.com/#tag/reactions/paths/~1reactions/post
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
if ( operation === 'create' ) {
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
body = {
user_id : this.getNodeParameter ( 'userId' , i ) ,
post_id : this.getNodeParameter ( 'postId' , i ) ,
emoji_name : ( this . getNodeParameter ( 'emojiName' , i ) as string ) . replace ( /:/g , '' ) ,
create_at : Date.now ( ) ,
} as { user_id : string ; post_id : string ; emoji_name : string ; create_at : number } ;
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'POST' ;
endpoint = 'reactions' ;
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
} else if ( operation === 'delete' ) {
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
// ----------------------------------
// reaction:delete
// ----------------------------------
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
// https://api.mattermost.com/#tag/reactions/paths/~1users~1{user_id}~1posts~1{post_id}~1reactions~1{emoji_name}/delete
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
const userId = this . getNodeParameter ( 'userId' , i ) as string ;
const postId = this . getNodeParameter ( 'postId' , i ) as string ;
const emojiName = ( this . getNodeParameter ( 'emojiName' , i ) as string ) . replace ( /:/g , '' ) ;
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'DELETE' ;
endpoint = ` users/ ${ userId } /posts/ ${ postId } /reactions/ ${ emojiName } ` ;
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
} else if ( operation === 'getAll' ) {
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
// ----------------------------------
// reaction:getAll
// ----------------------------------
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
// https://api.mattermost.com/#tag/reactions/paths/~1posts~1ids~1reactions/post
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
const postId = this . getNodeParameter ( 'postId' , i ) as string ;
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'GET' ;
endpoint = ` posts/ ${ postId } /reactions ` ;
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
qs . limit = this . getNodeParameter ( 'limit' , 0 , 0 ) as number ;
}
2021-03-24 09:59:45 -07:00
2021-07-19 23:58:54 -07:00
} else if ( resource === 'user' ) {
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
if ( operation === 'create' ) {
// ----------------------------------
// user:create
// ----------------------------------
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
const username = this . getNodeParameter ( 'username' , i ) as string ;
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
const authService = this . getNodeParameter ( 'authService' , i ) as string ;
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
body . auth_service = authService ;
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
if ( authService === 'email' ) {
body . email = this . getNodeParameter ( 'email' , i ) as string ;
body . password = this . getNodeParameter ( 'password' , i ) as string ;
} else {
body . auth_data = this . getNodeParameter ( 'authData' , i ) as string ;
}
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) as IDataObject ;
body . username = username ;
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
Object . assign ( body , additionalFields ) ;
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
if ( body . notificationUi ) {
body . notify_props = ( body . notificationUi as IDataObject ) . notificationValues ;
}
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'POST' ;
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
endpoint = 'users' ;
2020-10-03 10:15:14 -07:00
}
2021-07-19 23:58:54 -07:00
// TODO: Remove the "deactive" again in the future. In here temporary
// to not break workflows for people which set the option before
// typo got fixed. JO 2020-01-17
if ( operation === 'deactive' || operation === 'desactive' ) {
// ----------------------------------
// user:deactive
// ----------------------------------
const userId = this . getNodeParameter ( 'userId' , i ) as string ;
requestMethod = 'DELETE' ;
endpoint = ` users/ ${ userId } ` ;
}
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
if ( operation === 'getAll' ) {
// ----------------------------------
// user:getAll
// ----------------------------------
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'GET' ;
2020-04-22 10:08:53 -07:00
2021-07-19 23:58:54 -07:00
returnAll = this . getNodeParameter ( 'returnAll' , i ) as boolean ;
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) as IDataObject ;
2020-04-22 10:08:53 -07:00
2021-07-19 23:58:54 -07:00
if ( additionalFields . inTeam ) {
qs . in_team = additionalFields . inTeam ;
}
2020-04-22 10:08:53 -07:00
2021-07-19 23:58:54 -07:00
if ( additionalFields . notInTeam ) {
qs . not_in_team = additionalFields . notInTeam ;
}
2020-04-22 10:08:53 -07:00
2021-07-19 23:58:54 -07:00
if ( additionalFields . inChannel ) {
qs . in_channel = additionalFields . inChannel ;
}
2020-04-22 10:08:53 -07:00
2021-07-19 23:58:54 -07:00
if ( additionalFields . notInChannel ) {
qs . not_in_channel = additionalFields . notInChannel ;
}
2020-04-22 10:08:53 -07:00
2021-07-19 23:58:54 -07:00
if ( additionalFields . sort ) {
qs . sort = snakeCase ( additionalFields . sort as string ) ;
}
2020-04-22 10:08:53 -07:00
2021-07-19 23:58:54 -07:00
const validRules = {
inTeam : [ 'last_activity_at' , 'created_at' , 'username' ] ,
inChannel : [ 'status' , 'username' ] ,
} ;
2020-04-22 10:08:53 -07:00
2021-07-19 23:58:54 -07:00
if ( additionalFields . sort ) {
if ( additionalFields . inTeam !== undefined || additionalFields . inChannel !== undefined ) {
2020-04-22 10:08:53 -07:00
2021-07-19 23:58:54 -07:00
if ( additionalFields . inTeam !== undefined
&& ! validRules . inTeam . includes ( snakeCase ( additionalFields . sort as string ) ) ) {
throw new NodeOperationError ( this . getNode ( ) , ` When In Team is set the only valid values for sorting are ${ validRules . inTeam . join ( ',' ) } ` ) ;
}
if ( additionalFields . inChannel !== undefined
&& ! validRules . inChannel . includes ( snakeCase ( additionalFields . sort as string ) ) ) {
throw new NodeOperationError ( this . getNode ( ) , ` When In Channel is set the only valid values for sorting are ${ validRules . inChannel . join ( ',' ) } ` ) ;
}
if ( additionalFields . inChannel !== undefined
&& additionalFields . inChannel === ''
&& additionalFields . sort !== 'username' ) {
throw new NodeOperationError ( this . getNode ( ) , 'When sort is different than username In Channel must be set' ) ;
}
2020-04-22 10:08:53 -07:00
2021-07-19 23:58:54 -07:00
if ( additionalFields . inTeam !== undefined
&& additionalFields . inTeam === ''
&& additionalFields . sort !== 'username' ) {
throw new NodeOperationError ( this . getNode ( ) , 'When sort is different than username In Team must be set' ) ;
}
2020-04-22 10:08:53 -07:00
2021-07-19 23:58:54 -07:00
} else {
throw new NodeOperationError ( this . getNode ( ) , ` When sort is defined either 'in team' or 'in channel' must be defined ` ) ;
2020-04-22 10:08:53 -07:00
}
2021-07-19 23:58:54 -07:00
}
2020-04-22 10:08:53 -07:00
2021-07-19 23:58:54 -07:00
if ( additionalFields . sort === 'username' ) {
qs . sort = '' ;
}
2020-04-22 10:08:53 -07:00
2021-07-19 23:58:54 -07:00
if ( returnAll === false ) {
qs . per_page = this . getNodeParameter ( 'limit' , i ) as number ;
2020-04-22 10:08:53 -07:00
}
2021-07-19 23:58:54 -07:00
endpoint = ` /users ` ;
2020-04-22 10:08:53 -07:00
}
2021-07-19 23:58:54 -07:00
if ( operation === 'getByEmail' ) {
// ----------------------------------
// user:getByEmail
// ----------------------------------
const email = this . getNodeParameter ( 'email' , i ) as string ;
requestMethod = 'GET' ;
endpoint = ` users/email/ ${ email } ` ;
2020-04-22 10:08:53 -07:00
}
2021-07-19 23:58:54 -07:00
if ( operation === 'getById' ) {
// ----------------------------------
// user:getById
// ----------------------------------
userIds = ( this . getNodeParameter ( 'userIds' , i ) as string ) . split ( ',' ) as string [ ] ;
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) as IDataObject ;
2020-04-23 23:14:19 -07:00
2021-07-19 23:58:54 -07:00
if ( additionalFields . since ) {
qs . since = new Date ( additionalFields . since as string ) . getTime ( ) ;
}
2020-05-22 14:10:52 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'POST' ;
2020-05-22 14:10:52 -07:00
2021-07-19 23:58:54 -07:00
endpoint = 'users/ids' ;
2020-05-22 14:10:52 -07:00
2021-07-19 23:58:54 -07:00
//@ts-ignore
body = userIds ;
2020-05-22 14:10:52 -07:00
2021-07-19 23:58:54 -07:00
}
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
if ( operation === 'invite' ) {
// ----------------------------------
// user:invite
// ----------------------------------
const teamId = this . getNodeParameter ( 'teamId' , i ) as string ;
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
const emails = ( this . getNodeParameter ( 'emails' , i ) as string ) . split ( ',' ) ;
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
//@ts-ignore
body = emails ;
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
requestMethod = 'POST' ;
2020-10-03 10:15:14 -07:00
2021-07-19 23:58:54 -07:00
endpoint = ` teams/ ${ teamId } /invite/email ` ;
}
2020-10-03 10:15:14 -07:00
}
2021-07-19 23:58:54 -07:00
else {
throw new NodeOperationError ( this . getNode ( ) , ` The resource " ${ resource } " is not known! ` ) ;
2021-03-24 09:59:45 -07:00
}
2021-07-19 23:58:54 -07:00
let responseData ;
if ( returnAll ) {
responseData = await apiRequestAllItems . call ( this , requestMethod , endpoint , body , qs ) ;
} else {
responseData = await apiRequest . call ( this , requestMethod , endpoint , body , qs ) ;
if ( qs . limit ) {
responseData = responseData . slice ( 0 , qs . limit ) ;
}
if ( resource === 'channel' && operation === 'members' ) {
const resolveData = this . getNodeParameter ( 'resolveData' , i ) as boolean ;
if ( resolveData ) {
const userIds : string [ ] = [ ] ;
for ( const data of responseData ) {
userIds . push ( data . user_id ) ;
}
if ( userIds . length > 0 ) {
responseData = await apiRequest . call ( this , 'POST' , 'users/ids' , userIds , qs ) ;
}
2020-05-22 14:10:52 -07:00
}
}
}
2021-07-19 23:58:54 -07:00
if ( Array . isArray ( responseData ) ) {
returnData . push . apply ( returnData , responseData ) ;
} else {
returnData . push ( responseData ) ;
}
} catch ( error ) {
if ( this . continueOnFail ( ) ) {
returnData . push ( { error : error.message } ) ;
continue ;
}
throw error ;
2020-04-22 10:08:53 -07:00
}
2019-10-20 13:57:06 -07:00
}
return [ this . helpers . returnJsonArray ( returnData ) ] ;
}
}