slack.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. const debug = require('debug')('growi:util:slack');
  2. const urljoin = require('url-join');
  3. /**
  4. * slack
  5. */
  6. /* eslint-disable no-use-before-define */
  7. module.exports = function(crowi) {
  8. const config = crowi.getConfig();
  9. const Config = crowi.model('Config');
  10. const Slack = require('slack-node');
  11. const { configManager } = crowi;
  12. const slack = {};
  13. const postWithIwh = function(messageObj) {
  14. return new Promise((resolve, reject) => {
  15. const client = new Slack();
  16. client.setWebhook(config.notification['slack:incomingWebhookUrl']);
  17. client.webhook(messageObj, (err, res) => {
  18. if (err) {
  19. debug('Post error', err, res);
  20. debug('Sent data to slack is:', messageObj);
  21. return reject(err);
  22. }
  23. resolve(res);
  24. });
  25. });
  26. };
  27. const postWithWebApi = function(messageObj) {
  28. return new Promise((resolve, reject) => {
  29. const client = new Slack(config.notification['slack:token']);
  30. // stringify attachments
  31. if (messageObj.attachments != null) {
  32. messageObj.attachments = JSON.stringify(messageObj.attachments);
  33. }
  34. client.api('chat.postMessage', messageObj, (err, res) => {
  35. if (err) {
  36. debug('Post error', err, res);
  37. debug('Sent data to slack is:', messageObj);
  38. return reject(err);
  39. }
  40. resolve(res);
  41. });
  42. });
  43. };
  44. const convertMarkdownToMarkdown = function(body) {
  45. const url = crowi.appService.getSiteUrl();
  46. return body
  47. .replace(/\n\*\s(.+)/g, '\n• $1')
  48. .replace(/#{1,}\s?(.+)/g, '\n*$1*')
  49. .replace(/(\[(.+)\]\((https?:\/\/.+)\))/g, '<$3|$2>')
  50. .replace(/(\[(.+)\]\((\/.+)\))/g, `<${url}$3|$2>`);
  51. };
  52. const prepareAttachmentTextForCreate = function(page, user) {
  53. let body = page.revision.body;
  54. if (body.length > 2000) {
  55. body = `${body.substr(0, 2000)}...`;
  56. }
  57. return convertMarkdownToMarkdown(body);
  58. };
  59. const prepareAttachmentTextForUpdate = function(page, user, previousRevision) {
  60. const diff = require('diff');
  61. let diffText = '';
  62. diff.diffLines(previousRevision.body, page.revision.body).forEach((line) => {
  63. debug('diff line', line);
  64. const value = line.value.replace(/\r\n|\r/g, '\n'); // eslint-disable-line no-unused-vars
  65. if (line.added) {
  66. diffText += `${line.value} ... :lower_left_fountain_pen:`;
  67. }
  68. else if (line.removed) {
  69. // diffText += '-' + line.value.replace(/(.+)?\n/g, '- $1\n');
  70. // 1以下は無視
  71. if (line.count > 1) {
  72. diffText += `:wastebasket: ... ${line.count} lines\n`;
  73. }
  74. }
  75. else {
  76. // diffText += '...\n';
  77. }
  78. });
  79. debug('diff is', diffText);
  80. return diffText;
  81. };
  82. const prepareAttachmentTextForComment = function(comment) {
  83. let body = comment.comment;
  84. if (body.length > 2000) {
  85. body = `${body.substr(0, 2000)}...`;
  86. }
  87. if (comment.isMarkdown) {
  88. return convertMarkdownToMarkdown(body);
  89. }
  90. return body;
  91. };
  92. const prepareSlackMessageForPage = function(page, user, channel, updateType, previousRevision) {
  93. const url = crowi.appService.getSiteUrl();
  94. let body = page.revision.body;
  95. if (updateType === 'create') {
  96. body = prepareAttachmentTextForCreate(page, user);
  97. }
  98. else {
  99. body = prepareAttachmentTextForUpdate(page, user, previousRevision);
  100. }
  101. const attachment = {
  102. color: '#263a3c',
  103. author_name: `@${user.username}`,
  104. author_link: urljoin(url, 'user', user.username),
  105. author_icon: user.image,
  106. title: page.path,
  107. title_link: urljoin(url, page.id),
  108. text: body,
  109. mrkdwn_in: ['text'],
  110. };
  111. if (user.image) {
  112. attachment.author_icon = user.image;
  113. }
  114. const message = {
  115. channel: `#${channel}`,
  116. username: Config.appTitle(config),
  117. text: getSlackMessageTextForPage(page.path, page.id, user, updateType),
  118. attachments: [attachment],
  119. };
  120. return message;
  121. };
  122. const prepareSlackMessageForComment = function(comment, user, channel, path) {
  123. const url = crowi.appService.getSiteUrl();
  124. const body = prepareAttachmentTextForComment(comment);
  125. const attachment = {
  126. color: '#263a3c',
  127. author_name: `@${user.username}`,
  128. author_link: urljoin(url, 'user', user.username),
  129. author_icon: user.image,
  130. text: body,
  131. mrkdwn_in: ['text'],
  132. };
  133. if (user.image) {
  134. attachment.author_icon = user.image;
  135. }
  136. const message = {
  137. channel: `#${channel}`,
  138. username: Config.appTitle(config),
  139. text: getSlackMessageTextForComment(path, String(comment.page), user),
  140. attachments: [attachment],
  141. };
  142. return message;
  143. };
  144. const getSlackMessageTextForPage = function(path, pageId, user, updateType) {
  145. let text;
  146. const url = crowi.appService.getSiteUrl();
  147. const pageUrl = `<${urljoin(url, pageId)}|${path}>`;
  148. if (updateType === 'create') {
  149. text = `:rocket: ${user.username} created a new page! ${pageUrl}`;
  150. }
  151. else {
  152. text = `:heavy_check_mark: ${user.username} updated ${pageUrl}`;
  153. }
  154. return text;
  155. };
  156. const getSlackMessageTextForComment = function(path, pageId, user) {
  157. const url = crowi.appService.getSiteUrl();
  158. const pageUrl = `<${urljoin(url, pageId)}|${path}>`;
  159. const text = `:speech_balloon: ${user.username} commented on ${pageUrl}`;
  160. return text;
  161. };
  162. // slack.post = function (channel, message, opts) {
  163. slack.postPage = (page, user, channel, updateType, previousRevision) => {
  164. const messageObj = prepareSlackMessageForPage(page, user, channel, updateType, previousRevision);
  165. return slackPost(messageObj);
  166. };
  167. slack.postComment = (comment, user, channel, path) => {
  168. const messageObj = prepareSlackMessageForComment(comment, user, channel, path);
  169. return slackPost(messageObj);
  170. };
  171. const slackPost = (messageObj) => {
  172. // when incoming Webhooks is prioritized
  173. if (Config.isIncomingWebhookPrioritized(config)) {
  174. if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
  175. debug('posting message with IncomingWebhook');
  176. return postWithIwh(messageObj);
  177. }
  178. if (configManager.getConfig('notification', 'slack:token')) {
  179. debug('posting message with Web API');
  180. return postWithWebApi(messageObj);
  181. }
  182. }
  183. // else
  184. else {
  185. if (configManager.getConfig('notification', 'slack:token')) {
  186. debug('posting message with Web API');
  187. return postWithWebApi(messageObj);
  188. }
  189. if (configManager.getConfig('notification', 'slack:incomingWebhookUrl')) {
  190. debug('posting message with IncomingWebhook');
  191. return postWithIwh(messageObj);
  192. }
  193. }
  194. };
  195. return slack;
  196. };