|
@@ -80,9 +80,23 @@ module.exports = function(crowi) {
|
|
|
return diffText;
|
|
return diffText;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- const prepareSlackMessage = function(page, user, channel, updateType, previousRevision) {
|
|
|
|
|
- var url = config.crowi['app:url'] || '';
|
|
|
|
|
- var body = page.revision.body;
|
|
|
|
|
|
|
+ const prepareAttachmentTextForComment = function(comment) {
|
|
|
|
|
+ let body = comment.comment;
|
|
|
|
|
+ if (body.length > 2000) {
|
|
|
|
|
+ body = body.substr(0, 2000) + '...';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (comment.isMarkdown) {
|
|
|
|
|
+ return convertMarkdownToMrkdwn(body);
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ return body;
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const prepareSlackMessageForPage = function(page, user, channel, updateType, previousRevision) {
|
|
|
|
|
+ const url = config.crowi['app:url'] || '';
|
|
|
|
|
+ let body = page.revision.body;
|
|
|
|
|
|
|
|
if (updateType == 'create') {
|
|
if (updateType == 'create') {
|
|
|
body = prepareAttachmentTextForCreate(page, user);
|
|
body = prepareAttachmentTextForCreate(page, user);
|
|
@@ -91,7 +105,7 @@ module.exports = function(crowi) {
|
|
|
body = prepareAttachmentTextForUpdate(page, user, previousRevision);
|
|
body = prepareAttachmentTextForUpdate(page, user, previousRevision);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var attachment = {
|
|
|
|
|
|
|
+ const attachment = {
|
|
|
color: '#263a3c',
|
|
color: '#263a3c',
|
|
|
author_name: '@' + user.username,
|
|
author_name: '@' + user.username,
|
|
|
author_link: url + '/user/' + user.username,
|
|
author_link: url + '/user/' + user.username,
|
|
@@ -105,17 +119,43 @@ module.exports = function(crowi) {
|
|
|
attachment.author_icon = user.image;
|
|
attachment.author_icon = user.image;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var message = {
|
|
|
|
|
|
|
+ const message = {
|
|
|
channel: '#' + channel,
|
|
channel: '#' + channel,
|
|
|
username: Config.appTitle(config),
|
|
username: Config.appTitle(config),
|
|
|
- text: getSlackMessageText(page.path, user, updateType),
|
|
|
|
|
|
|
+ text: getSlackMessageTextForPage(page.path, user, updateType),
|
|
|
attachments: [attachment],
|
|
attachments: [attachment],
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
return message;
|
|
return message;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- const getSlackMessageText = function(path, user, updateType) {
|
|
|
|
|
|
|
+ const prepareSlackMessageForComment = function(comment, user, channel, path) {
|
|
|
|
|
+ const url = config.crowi['app:url'] || '';
|
|
|
|
|
+ const body = prepareAttachmentTextForComment(comment);
|
|
|
|
|
+
|
|
|
|
|
+ const attachment = {
|
|
|
|
|
+ color: '#263a3c',
|
|
|
|
|
+ author_name: '@' + user.username,
|
|
|
|
|
+ author_link: url + '/user/' + user.username,
|
|
|
|
|
+ author_icon: user.image,
|
|
|
|
|
+ text: body,
|
|
|
|
|
+ mrkdwn_in: ['text'],
|
|
|
|
|
+ };
|
|
|
|
|
+ if (user.image) {
|
|
|
|
|
+ attachment.author_icon = user.image;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const message = {
|
|
|
|
|
+ channel: '#' + channel,
|
|
|
|
|
+ username: Config.appTitle(config),
|
|
|
|
|
+ text: getSlackMessageTextForComment(path, user),
|
|
|
|
|
+ attachments: [attachment],
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ return message;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const getSlackMessageTextForPage = function(path, user, updateType) {
|
|
|
let text;
|
|
let text;
|
|
|
const url = config.crowi['app:url'] || '';
|
|
const url = config.crowi['app:url'] || '';
|
|
|
|
|
|
|
@@ -130,10 +170,28 @@ module.exports = function(crowi) {
|
|
|
return text;
|
|
return text;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+ const getSlackMessageTextForComment = function(path, user) {
|
|
|
|
|
+ const url = config.crowi['app:url'] || '';
|
|
|
|
|
+ const pageUrl = `<${url}${path}|${path}>`;
|
|
|
|
|
+ const text = `:speech_balloon: ${user.username} commented on ${pageUrl}`;
|
|
|
|
|
+
|
|
|
|
|
+ return text;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
// slack.post = function (channel, message, opts) {
|
|
// slack.post = function (channel, message, opts) {
|
|
|
- slack.post = (page, user, channel, updateType, previousRevision) => {
|
|
|
|
|
- const messageObj = prepareSlackMessage(page, user, channel, updateType, previousRevision);
|
|
|
|
|
|
|
+ slack.postPage = (page, user, channel, updateType, previousRevision) => {
|
|
|
|
|
+ const messageObj = prepareSlackMessageForPage(page, user, channel, updateType, previousRevision);
|
|
|
|
|
+
|
|
|
|
|
+ return promisifiedSlackPost(messageObj);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ slack.postComment = (comment, user, channel, path) => {
|
|
|
|
|
+ const messageObj = prepareSlackMessageForComment(comment, user, channel, path);
|
|
|
|
|
+
|
|
|
|
|
+ return promisifiedSlackPost(messageObj);
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
|
|
+ const promisifiedSlackPost = (messageObj) => {
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
// define callback function for Promise
|
|
// define callback function for Promise
|
|
|
const callback = function(err, res) {
|
|
const callback = function(err, res) {
|