slack.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * slack
  3. */
  4. module.exports = function(crowi) {
  5. 'use strict';
  6. const debug = require('debug')('growi:util:slack'),
  7. config = crowi.getConfig(),
  8. Config = crowi.model('Config'),
  9. Slack = require('slack-node'),
  10. slack = {};
  11. const postWithIwh = function(messageObj, callback) {
  12. const client = new Slack();
  13. client.setWebhook(config.notification['slack:incomingWebhookUrl']);
  14. client.webhook(messageObj, callback);
  15. };
  16. const postWithWebApi = function(messageObj, callback) {
  17. const client = new Slack(config.notification['slack:token']);
  18. // stringify attachments
  19. if (messageObj.attachments != null) {
  20. messageObj.attachments = JSON.stringify(messageObj.attachments);
  21. }
  22. client.api('chat.postMessage', messageObj, callback);
  23. };
  24. const convertMarkdownToMrkdwn = function(body) {
  25. var url = '';
  26. if (config.crowi && config.crowi['app:url']) {
  27. url = config.crowi['app:url'];
  28. }
  29. body = body
  30. .replace(/\n\*\s(.+)/g, '\n• $1')
  31. .replace(/#{1,}\s?(.+)/g, '\n*$1*')
  32. .replace(/(\[(.+)\]\((https?:\/\/.+)\))/g, '<$3|$2>')
  33. .replace(/(\[(.+)\]\((\/.+)\))/g, '<' + url + '$3|$2>')
  34. ;
  35. return body;
  36. };
  37. const prepareAttachmentTextForCreate = function(page, user) {
  38. var body = page.revision.body;
  39. if (body.length > 2000) {
  40. body = body.substr(0, 2000) + '...';
  41. }
  42. return convertMarkdownToMrkdwn(body);
  43. };
  44. const prepareAttachmentTextForUpdate = function(page, user, previousRevision) {
  45. var diff = require('diff');
  46. var diffText = '';
  47. diff.diffLines(previousRevision.body, page.revision.body).forEach(function(line) {
  48. debug('diff line', line);
  49. /* eslint-disable no-unused-vars */
  50. var value = line.value.replace(/\r\n|\r/g, '\n');
  51. /* eslint-enable */
  52. if (line.added) {
  53. diffText += `:pencil2: ...\n${line.value}`;
  54. }
  55. else if (line.removed) {
  56. // diffText += '-' + line.value.replace(/(.+)?\n/g, '- $1\n');
  57. // 1以下は無視
  58. if (line.count > 1) {
  59. diffText += `:wastebasket: ... ${line.count} lines\n`;
  60. }
  61. }
  62. else {
  63. //diffText += '...\n';
  64. }
  65. });
  66. debug('diff is', diffText);
  67. return diffText;
  68. };
  69. const prepareAttachmentTextForComment = function(page, user, channel, updateType, previousRevision) {
  70. return {
  71. channel: '#general',
  72. username: 'Growi',
  73. text: '`comment uploaded`'
  74. };
  75. };
  76. const prepareSlackMessage = function(page, user, channel, updateType, previousRevision) {
  77. var url = config.crowi['app:url'] || '';
  78. var body = page.revision.body;
  79. if (updateType == 'create') {
  80. body = prepareAttachmentTextForCreate(page, user);
  81. }
  82. else if (updateType == 'comment') {
  83. body = prepareAttachmentTextForComment(page, user);
  84. }
  85. else {
  86. body = prepareAttachmentTextForUpdate(page, user, previousRevision);
  87. }
  88. var attachment = {
  89. color: '#263a3c',
  90. author_name: '@' + user.username,
  91. author_link: url + '/user/' + user.username,
  92. author_icon: user.image,
  93. title: page.path,
  94. title_link: url + '/' + page._id,
  95. text: body,
  96. mrkdwn_in: ['text'],
  97. };
  98. if (user.image) {
  99. attachment.author_icon = user.image;
  100. }
  101. var message = {
  102. channel: '#' + channel,
  103. username: Config.appTitle(config),
  104. text: getSlackMessageText(page.path, user, updateType),
  105. attachments: [attachment],
  106. };
  107. return message;
  108. };
  109. const getSlackMessageText = function(path, user, updateType) {
  110. let text;
  111. const url = config.crowi['app:url'] || '';
  112. const pageUrl = `<${url}${path}|${path}>`;
  113. if (updateType == 'create') {
  114. text = `:white_check_mark: ${user.username} created a new page! ${pageUrl}`;
  115. }
  116. else {
  117. text = `:up: ${user.username} updated ${pageUrl}`;
  118. }
  119. return text;
  120. };
  121. // slack.post = function (channel, message, opts) {
  122. slack.post = (page, user, channel, updateType, previousRevision) => {
  123. const messageObj = prepareSlackMessage(page, user, channel, updateType, previousRevision);
  124. return new Promise((resolve, reject) => {
  125. // define callback function for Promise
  126. const callback = function(err, res) {
  127. if (err) {
  128. debug('Post error', err, res);
  129. debug('Sent data to slack is:', messageObj);
  130. return reject(err);
  131. }
  132. resolve(res);
  133. };
  134. // when incoming Webhooks is prioritized
  135. if (Config.isIncomingWebhookPrioritized(config)) {
  136. if (Config.hasSlackIwhUrl(config)) {
  137. debug('posting message with IncomingWebhook');
  138. postWithIwh(messageObj, callback);
  139. }
  140. else if (Config.hasSlackToken(config)) {
  141. debug('posting message with Web API');
  142. postWithWebApi(messageObj, callback);
  143. }
  144. }
  145. // else
  146. else {
  147. if (Config.hasSlackToken(config)) {
  148. debug('posting message with Web API');
  149. postWithWebApi(messageObj, callback);
  150. }
  151. else if (Config.hasSlackIwhUrl(config)) {
  152. debug('posting message with IncomingWebhook');
  153. postWithIwh(messageObj, callback);
  154. }
  155. }
  156. resolve();
  157. });
  158. };
  159. return slack;
  160. };