activities.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Activities don't need a schema because they are always set from the a trusted
  2. // environment - the server - and there is no risk that a user change the logic
  3. // we use with this collection. Moreover using a schema for this collection
  4. // would be difficult (different activities have different fields) and wouldn't
  5. // bring any direct advantage.
  6. //
  7. // XXX The activities API is not so nice and need some functionalities. For
  8. // instance if a user archive a card, and un-archive it a few seconds later we
  9. // should remove both activities assuming it was an error the user decided to
  10. // revert.
  11. Activities = new Mongo.Collection('activities');
  12. Activities.helpers({
  13. board() {
  14. return Boards.findOne(this.boardId);
  15. },
  16. oldBoard() {
  17. return Boards.findOne(this.oldBoardId);
  18. },
  19. user() {
  20. return Users.findOne(this.userId);
  21. },
  22. member() {
  23. return Users.findOne(this.memberId);
  24. },
  25. list() {
  26. return Lists.findOne(this.listId);
  27. },
  28. swimlane() {
  29. return Swimlanes.findOne(this.swimlaneId);
  30. },
  31. oldSwimlane() {
  32. return Swimlanes.findOne(this.oldSwimlaneId);
  33. },
  34. oldList() {
  35. return Lists.findOne(this.oldListId);
  36. },
  37. card() {
  38. return Cards.findOne(this.cardId);
  39. },
  40. comment() {
  41. return CardComments.findOne(this.commentId);
  42. },
  43. attachment() {
  44. return Attachments.findOne(this.attachmentId);
  45. },
  46. checklist() {
  47. return Checklists.findOne(this.checklistId);
  48. },
  49. checklistItem() {
  50. return ChecklistItems.findOne(this.checklistItemId);
  51. },
  52. subtasks() {
  53. return Cards.findOne(this.subtaskId);
  54. },
  55. customField() {
  56. return CustomFields.findOne(this.customFieldId);
  57. },
  58. // Label activity did not work yet, unable to edit labels when tried this.
  59. //label() {
  60. // return Cards.findOne(this.labelId);
  61. //},
  62. });
  63. Activities.before.update((userId, doc, fieldNames, modifier) => {
  64. modifier.$set = modifier.$set || {};
  65. modifier.$set.modifiedAt = new Date();
  66. });
  67. Activities.before.insert((userId, doc) => {
  68. doc.createdAt = new Date();
  69. doc.modifiedAt = doc.createdAt;
  70. });
  71. Activities.after.insert((userId, doc) => {
  72. const activity = Activities._transform(doc);
  73. RulesHelper.executeRules(activity);
  74. });
  75. if (Meteor.isServer) {
  76. // For efficiency create indexes on the date of creation, and on the date of
  77. // creation in conjunction with the card or board id, as corresponding views
  78. // are largely used in the App. See #524.
  79. Meteor.startup(() => {
  80. Activities._collection._ensureIndex({ createdAt: -1 });
  81. Activities._collection._ensureIndex({ modifiedAt: -1 });
  82. Activities._collection._ensureIndex({ cardId: 1, createdAt: -1 });
  83. Activities._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  84. Activities._collection._ensureIndex(
  85. { commentId: 1 },
  86. { partialFilterExpression: { commentId: { $exists: true } } },
  87. );
  88. Activities._collection._ensureIndex(
  89. { attachmentId: 1 },
  90. { partialFilterExpression: { attachmentId: { $exists: true } } },
  91. );
  92. Activities._collection._ensureIndex(
  93. { customFieldId: 1 },
  94. { partialFilterExpression: { customFieldId: { $exists: true } } },
  95. );
  96. // Label activity did not work yet, unable to edit labels when tried this.
  97. //Activities._collection._dropIndex({ labelId: 1 }, { "indexKey": -1 });
  98. //Activities._collection._dropIndex({ labelId: 1 }, { partialFilterExpression: { labelId: { $exists: true } } });
  99. });
  100. Activities.after.insert((userId, doc) => {
  101. const activity = Activities._transform(doc);
  102. let participants = [];
  103. let watchers = [];
  104. let title = 'act-activity-notify';
  105. const board = Boards.findOne(activity.boardId);
  106. const description = `act-${activity.activityType}`;
  107. const params = {
  108. activityId: activity._id,
  109. };
  110. if (activity.userId) {
  111. // No need send notification to user of activity
  112. // participants = _.union(participants, [activity.userId]);
  113. const user = activity.user();
  114. params.user = user.getName();
  115. params.userEmails = user.emails;
  116. params.userId = activity.userId;
  117. }
  118. if (activity.boardId) {
  119. if (board.title.length > 0) {
  120. params.board = board.title;
  121. } else {
  122. params.board = '';
  123. }
  124. title = 'act-withBoardTitle';
  125. params.url = board.absoluteUrl();
  126. params.boardId = activity.boardId;
  127. }
  128. if (activity.oldBoardId) {
  129. const oldBoard = activity.oldBoard();
  130. if (oldBoard) {
  131. watchers = _.union(watchers, oldBoard.watchers || []);
  132. params.oldBoard = oldBoard.title;
  133. params.oldBoardId = activity.oldBoardId;
  134. }
  135. }
  136. if (activity.memberId) {
  137. participants = _.union(participants, [activity.memberId]);
  138. params.member = activity.member().getName();
  139. }
  140. if (activity.listId) {
  141. const list = activity.list();
  142. watchers = _.union(watchers, list.watchers || []);
  143. params.list = list.title;
  144. params.listId = activity.listId;
  145. }
  146. if (activity.oldListId) {
  147. const oldList = activity.oldList();
  148. if (oldList) {
  149. watchers = _.union(watchers, oldList.watchers || []);
  150. params.oldList = oldList.title;
  151. params.oldListId = activity.oldListId;
  152. }
  153. }
  154. if (activity.oldSwimlaneId) {
  155. const oldSwimlane = activity.oldSwimlane();
  156. if (oldSwimlane) {
  157. watchers = _.union(watchers, oldSwimlane.watchers || []);
  158. params.oldSwimlane = oldSwimlane.title;
  159. params.oldSwimlaneId = activity.oldSwimlaneId;
  160. }
  161. }
  162. if (activity.cardId) {
  163. const card = activity.card();
  164. participants = _.union(participants, [card.userId], card.members || []);
  165. watchers = _.union(watchers, card.watchers || []);
  166. params.card = card.title;
  167. title = 'act-withCardTitle';
  168. params.url = card.absoluteUrl();
  169. params.cardId = activity.cardId;
  170. }
  171. if (activity.swimlaneId) {
  172. const swimlane = activity.swimlane();
  173. params.swimlane = swimlane.title;
  174. params.swimlaneId = activity.swimlaneId;
  175. }
  176. if (activity.commentId) {
  177. const comment = activity.comment();
  178. params.comment = comment.text;
  179. if (board) {
  180. const comment = params.comment;
  181. const knownUsers = board.members.map(member => {
  182. const u = Users.findOne(member.userId);
  183. if (u) {
  184. member.username = u.username;
  185. member.emails = u.emails;
  186. }
  187. return member;
  188. });
  189. const mentionRegex = /\B@(?:(?:"([\w.\s]*)")|([\w.]+))/gi; // including space in username
  190. let currentMention;
  191. while ((currentMention = mentionRegex.exec(comment)) !== null) {
  192. /*eslint no-unused-vars: ["error", { "varsIgnorePattern": "[iI]gnored" }]*/
  193. const [ignored, quoteduser, simple] = currentMention;
  194. const username = quoteduser || simple;
  195. if (username === params.user) {
  196. // ignore commenter mention himself?
  197. continue;
  198. }
  199. const atUser = _.findWhere(knownUsers, { username });
  200. if (!atUser) {
  201. continue;
  202. }
  203. const uid = atUser.userId;
  204. params.atUsername = username;
  205. params.atEmails = atUser.emails;
  206. title = 'act-atUserComment';
  207. watchers = _.union(watchers, [uid]);
  208. }
  209. }
  210. params.commentId = comment._id;
  211. }
  212. if (activity.attachmentId) {
  213. const attachment = activity.attachment();
  214. params.attachment = attachment.original.name;
  215. params.attachmentId = attachment._id;
  216. }
  217. if (activity.checklistId) {
  218. const checklist = activity.checklist();
  219. params.checklist = checklist.title;
  220. }
  221. if (activity.checklistItemId) {
  222. const checklistItem = activity.checklistItem();
  223. params.checklistItem = checklistItem.title;
  224. }
  225. if (activity.customFieldId) {
  226. const customField = activity.customField();
  227. params.customField = customField.name;
  228. params.customFieldValue = Activities.findOne({
  229. customFieldId: customField._id,
  230. }).value;
  231. }
  232. // Label activity did not work yet, unable to edit labels when tried this.
  233. //if (activity.labelId) {
  234. // const label = activity.label();
  235. // params.label = label.name;
  236. // params.labelId = activity.labelId;
  237. //}
  238. if (
  239. (!activity.timeKey || activity.timeKey === 'dueAt') &&
  240. activity.timeValue
  241. ) {
  242. // due time reminder, if it doesn't have old value, it's a brand new set, need some differentiation
  243. title = activity.timeOldValue ? 'act-withDue' : 'act-newDue';
  244. }
  245. ['timeValue', 'timeOldValue'].forEach(key => {
  246. // copy time related keys & values to params
  247. const value = activity[key];
  248. if (value) params[key] = value;
  249. });
  250. if (board) {
  251. const BIGEVENTS = process.env.BIGEVENTS_PATTERN || 'due'; // if environment BIGEVENTS_PATTERN is set or default, any activityType matching it is important
  252. try {
  253. const atype = activity.activityType;
  254. if (new RegExp(BIGEVENTS).exec(atype)) {
  255. watchers = _.union(
  256. watchers,
  257. board.activeMembers().map(member => member.userId),
  258. ); // notify all active members for important events system defined or default to all activity related to due date
  259. }
  260. } catch (e) {
  261. // passed env var BIGEVENTS_PATTERN is not a valid regex
  262. }
  263. const watchingUsers = _.pluck(
  264. _.where(board.watchers, { level: 'watching' }),
  265. 'userId',
  266. );
  267. const trackingUsers = _.pluck(
  268. _.where(board.watchers, { level: 'tracking' }),
  269. 'userId',
  270. );
  271. watchers = _.union(
  272. watchers,
  273. watchingUsers,
  274. _.intersection(participants, trackingUsers),
  275. );
  276. }
  277. Notifications.getUsers(watchers).forEach(user => {
  278. // don't notify a user of their own behavior
  279. if (user._id !== userId) {
  280. Notifications.notify(user, title, description, params);
  281. }
  282. });
  283. const integrations = Integrations.find({
  284. boardId: { $in: [board._id, Integrations.Const.GLOBAL_WEBHOOK_ID] },
  285. // type: 'outgoing-webhooks', // all types
  286. enabled: true,
  287. activities: { $in: [description, 'all'] },
  288. }).fetch();
  289. if (integrations.length > 0) {
  290. params.watchers = watchers;
  291. integrations.forEach(integration => {
  292. Meteor.call(
  293. 'outgoingWebhooks',
  294. integration,
  295. description,
  296. params,
  297. () => {
  298. return;
  299. },
  300. );
  301. });
  302. }
  303. });
  304. }
  305. export default Activities;