slack.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 prepareSlackMessage = function(page, user, channel, updateType, previousRevision) {
  70. var url = config.crowi['app:url'] || '';
  71. var body = page.revision.body;
  72. if (updateType == 'create') {
  73. body = prepareAttachmentTextForCreate(page, user);
  74. }
  75. else {
  76. body = prepareAttachmentTextForUpdate(page, user, previousRevision);
  77. }
  78. var attachment = {
  79. color: '#263a3c',
  80. author_name: '@' + user.username,
  81. author_link: url + '/user/' + user.username,
  82. author_icon: user.image,
  83. title: page.path,
  84. title_link: url + '/' + page._id,
  85. text: body,
  86. mrkdwn_in: ['text'],
  87. };
  88. if (user.image) {
  89. attachment.author_icon = user.image;
  90. }
  91. var message = {
  92. channel: '#' + channel,
  93. username: Config.appTitle(config),
  94. text: getSlackMessageText(page.path, user, updateType),
  95. attachments: [attachment],
  96. };
  97. return message;
  98. };
  99. const getSlackMessageText = function(path, user, updateType) {
  100. let text;
  101. const url = config.crowi['app:url'] || '';
  102. const pageUrl = `<${url}${path}|${path}>`;
  103. if (updateType == 'create') {
  104. text = `:white_check_mark: ${user.username} created a new page! ${pageUrl}`;
  105. }
  106. else {
  107. text = `:up: ${user.username} updated ${pageUrl}`;
  108. }
  109. return text;
  110. };
  111. // slack.post = function (channel, message, opts) {
  112. slack.post = (page, user, channel, updateType, previousRevision) => {
  113. const messageObj = prepareSlackMessage(page, user, channel, updateType, previousRevision);
  114. return new Promise((resolve, reject) => {
  115. // define callback function for Promise
  116. const callback = function(err, res) {
  117. if (err) {
  118. debug('Post error', err, res);
  119. debug('Sent data to slack is:', messageObj);
  120. return reject(err);
  121. }
  122. resolve(res);
  123. };
  124. // when incoming Webhooks is prioritized
  125. if (Config.isIncomingWebhookPrioritized(config)) {
  126. if (Config.hasSlackIwhUrl(config)) {
  127. debug('posting message with IncomingWebhook');
  128. postWithIwh(messageObj, callback);
  129. }
  130. else if (Config.hasSlackToken(config)) {
  131. debug('posting message with Web API');
  132. postWithWebApi(messageObj, callback);
  133. }
  134. }
  135. // else
  136. else {
  137. if (Config.hasSlackToken(config)) {
  138. debug('posting message with Web API');
  139. postWithWebApi(messageObj, callback);
  140. }
  141. else if (Config.hasSlackIwhUrl(config)) {
  142. debug('posting message with IncomingWebhook');
  143. postWithIwh(messageObj, callback);
  144. }
  145. }
  146. resolve();
  147. });
  148. };
  149. return slack;
  150. };