Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ in development
(improvement)
* Split out the functionality of ``src/stackstorm.js`` into ``stackstorm_api.js`` and refactor it
to be a JS old style class with a wrapper [PR #187, PR #190] (improvement)
* Fix the Mattermost adapter to use the ``adapter.post`` function instead of emitting the
``slack-attachment`` event [PR #192] (bug fix)

0.9.6
-----
Expand Down
81 changes: 67 additions & 14 deletions src/lib/adapters/mattermost.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
var env = process.env;
var util = require('util');
var utils = require('./../utils');
var messages = require('./../slack-messages');
var SlackLikeAdapter = require('./slack-like');


Expand All @@ -29,15 +30,21 @@ util.inherits(MattermostAdapter, SlackLikeAdapter);

MattermostAdapter.prototype.postData = function(data) {
var self = this;

var recipient, attachment_color, split_message,
var attachment_color, split_message, envelope,
attachment, pretext = "";
// We capture robot here so the `sendChunk` closure captures the correct
// `this`
var robot = self.robot;

// If we are supposed to whisper to a single user, use a direct message
if (data.whisper && data.user) {
recipient = data.user;
envelope = {
room: data.user
};
} else { // Otherwise, message the channel
recipient = data.channel;
envelope = {
room: data.channel
};
// If we aren't supposed to whisper, then we at least at-mention the user
pretext = (data.user && !data.whisper) ? util.format('@%s: ', data.user) : "";
}
Expand All @@ -56,7 +63,6 @@ MattermostAdapter.prototype.postData = function(data) {
}

split_message = utils.splitMessage(self.formatData(data.message));

if (split_message.text) {
// Default values
var content = {
Expand All @@ -65,12 +71,57 @@ MattermostAdapter.prototype.postData = function(data) {
};
// Override the default values with values from `data.extra.mattermost`
if (data.extra && data.extra.mattermost) {
// Backwards compatibility

// Action:
//
// result:
// format: ...
// message: Message text
// extra:
// mattermost:
// author_name: Jira_Bot
// author_link: "https://stackstorm.com"
// author_icon: "https://stackstorm.com/favicon.ico"
// color: "#042A60"
// fallback: "Info about Jira ticket {{ execution.result.result.key }}"
// title: "{{ execution.result.result.key }}"
// title_link: "{{ execution.result.result.url }}"
// fields:
// -
// title: Summary
// value: "{{ execution.result.result.summary }}"
// short: false
//
// becomes:
//
// {
// "message": "Message text",
// "props": {
// "attachments": [
// {
// "author_name": "Jira Bot",
// "author_link": "https://stackstorm.com",
// "author_icon": "https://stackstorm.com/favicon.ico",
// "color": "#042A60",
// "fallback": "Info about Jira ticket {{ execution.result.result.key }}",
// "title": "{{ execution.result.result.key }}",
// "title_link": "{{ execution.result.result.url }}",
// "fields": [
// {
// "title": "Summary",
// "value": "{{ execution.result.result.summary }}",
// "short": false
// }
// ]
// }
// ]
// }
// }

for (var attrname in data.extra.mattermost) { content[attrname] = data.extra.mattermost[attrname]; }
}

// We capture robot here so the `sendMessage` closure captures the correct
// `this`
var robot = self.robot;
var chunks = split_message.text.match(/[\s\S]{1,3800}/g);

// We define a recursive closure that calls itself with the next data to
Expand All @@ -85,23 +136,25 @@ MattermostAdapter.prototype.postData = function(data) {
Inorder to accept the matteruser adapter message attachments changed the attachement json
*/

attachment = {
room: recipient,
attachments: content.attachments ? content.attachments : content,
var message = {
props: {
attachments: (content.attachments ? content.attachments : [content])
},
// There is likely a bug here - `split_message.text` being a true-y
// value does not imply that `split_message.pretext` is also non-empty,
// but we unconditionally set `text` to
// `pretext + split_message.pretext` on the first message
text: i === 0 ? pretext + split_message.pretext : null
message: i === 0 ? pretext + split_message.pretext : null
};
robot.emit('slack-attachment', attachment);

robot.adapter.send(envelope, message);
if (chunks.length > ++i) {
setTimeout(function(){ sendChunk(i); }, 300);
}
};
sendChunk(0);
} else {
self.robot.messageRoom.call(self.robot, recipient, pretext + split_message.pretext);
robot.messageRoom.call(self.robot, envelope.room, pretext + split_message.pretext);
}
};

Expand Down
5 changes: 5 additions & 0 deletions src/lib/slack-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

var utils = require('./utils.js');

// NOTE: This is being reused between the Slack adapter and the Mattermost
// adapter. If we need to tweak it for Mattermost, we should refactor it
// into a class, then inherit from it and override specific methods in
// Mattermost adapter.

var buildMessagesWithChunkedFieldValue = function (msg) {
var msgs;

Expand Down
15 changes: 13 additions & 2 deletions test/dummy-adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ function SlackBot(logger) {
this.client = new MockSlackClient(logger);
}

function MockMattermostAdapter(logger) {
this.logger = logger;
}

MockMattermostAdapter.prototype.send = function(envelope, message) {
this.logger.debug('Sending ' + JSON.stringify(message) + ' to ' + JSON.stringify(envelope));
};

function MockBotFrameworkAdapter(logger) {
this.logger = logger;
}
Expand All @@ -35,5 +43,8 @@ MockBotFrameworkAdapter.prototype.send = function(envelope, message) {
this.logger.info('Sending ' + JSON.stringify(message) + ' to ' + JSON.stringify(envelope));
};

module.exports.MockSlackAdapter = SlackBot;
module.exports.MockBotFrameworkAdapter = MockBotFrameworkAdapter;
module.exports = {
MockSlackAdapter: SlackBot,
MockMattermostAdapter: MockMattermostAdapter,
MockBotFrameworkAdapter: MockBotFrameworkAdapter
};
Loading