activities.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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.createIndex({ createdAt: -1 });
  81. Activities._collection.createIndex({ modifiedAt: -1 });
  82. Activities._collection.createIndex({ cardId: 1, createdAt: -1 });
  83. Activities._collection.createIndex({ boardId: 1, createdAt: -1 });
  84. Activities._collection.createIndex(
  85. { commentId: 1 },
  86. { partialFilterExpression: { commentId: { $exists: true } } },
  87. );
  88. Activities._collection.createIndex(
  89. { attachmentId: 1 },
  90. { partialFilterExpression: { attachmentId: { $exists: true } } },
  91. );
  92. Activities._collection.createIndex(
  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. if (user) {
  115. if (user.getName()) {
  116. params.user = user.getName();
  117. }
  118. if (user.emails) {
  119. params.userEmails = user.emails;
  120. }
  121. if (activity.userId) {
  122. params.userId = activity.userId;
  123. }
  124. }
  125. }
  126. if (activity.boardId) {
  127. if (board.title) {
  128. if (board.title.length > 0) {
  129. params.board = board.title;
  130. } else {
  131. params.board = '';
  132. }
  133. } else {
  134. params.board = '';
  135. }
  136. title = 'act-withBoardTitle';
  137. params.url = board.absoluteUrl();
  138. params.boardId = activity.boardId;
  139. }
  140. if (activity.oldBoardId) {
  141. const oldBoard = activity.oldBoard();
  142. if (oldBoard) {
  143. watchers = _.union(watchers, oldBoard.watchers || []);
  144. params.oldBoard = oldBoard.title;
  145. params.oldBoardId = activity.oldBoardId;
  146. }
  147. }
  148. if (activity.memberId) {
  149. participants = _.union(participants, [activity.memberId]);
  150. params.member = activity.member().getName();
  151. }
  152. if (activity.listId) {
  153. const list = activity.list();
  154. if (list) {
  155. if (list.watchers !== undefined) {
  156. watchers = _.union(watchers, list.watchers || []);
  157. }
  158. params.list = list.title;
  159. params.listId = activity.listId;
  160. }
  161. }
  162. if (activity.oldListId) {
  163. const oldList = activity.oldList();
  164. if (oldList) {
  165. watchers = _.union(watchers, oldList.watchers || []);
  166. params.oldList = oldList.title;
  167. params.oldListId = activity.oldListId;
  168. }
  169. }
  170. if (activity.oldSwimlaneId) {
  171. const oldSwimlane = activity.oldSwimlane();
  172. if (oldSwimlane) {
  173. watchers = _.union(watchers, oldSwimlane.watchers || []);
  174. params.oldSwimlane = oldSwimlane.title;
  175. params.oldSwimlaneId = activity.oldSwimlaneId;
  176. }
  177. }
  178. if (activity.cardId) {
  179. const card = activity.card();
  180. participants = _.union(participants, [card.userId], card.members || []);
  181. watchers = _.union(watchers, card.watchers || []);
  182. params.card = card.title;
  183. title = 'act-withCardTitle';
  184. params.url = card.absoluteUrl();
  185. params.cardId = activity.cardId;
  186. }
  187. if (activity.swimlaneId) {
  188. const swimlane = activity.swimlane();
  189. params.swimlane = swimlane.title;
  190. params.swimlaneId = activity.swimlaneId;
  191. }
  192. if (activity.commentId) {
  193. const comment = activity.comment();
  194. params.comment = comment.text;
  195. if (board) {
  196. const comment = params.comment;
  197. const knownUsers = board.members.map(member => {
  198. const u = Users.findOne(member.userId);
  199. if (u) {
  200. member.username = u.username;
  201. member.emails = u.emails;
  202. }
  203. return member;
  204. });
  205. const mentionRegex = /\B@(?:(?:"([\w.\s-]*)")|([\w.-]+))/gi; // including space in username
  206. let currentMention;
  207. while ((currentMention = mentionRegex.exec(comment)) !== null) {
  208. /*eslint no-unused-vars: ["error", { "varsIgnorePattern": "[iI]gnored" }]*/
  209. const [ignored, quoteduser, simple] = currentMention;
  210. const username = quoteduser || simple;
  211. if (username === params.user) {
  212. // ignore commenter mention himself?
  213. continue;
  214. }
  215. if (activity.boardId && username === 'board_members') {
  216. // mentions all board members
  217. const knownUids = knownUsers.map(u => u.userId);
  218. watchers = _.union(watchers, [...knownUids]);
  219. title = 'act-atUserComment';
  220. } else if (activity.cardId && username === 'card_members') {
  221. // mentions all card members if assigned
  222. const card = activity.card();
  223. watchers = _.union(watchers, [...card.members]);
  224. title = 'act-atUserComment';
  225. } else {
  226. const atUser = _.findWhere(knownUsers, { username });
  227. if (!atUser) {
  228. continue;
  229. }
  230. const uid = atUser.userId;
  231. params.atUsername = username;
  232. params.atEmails = atUser.emails;
  233. title = 'act-atUserComment';
  234. watchers = _.union(watchers, [uid]);
  235. }
  236. }
  237. }
  238. params.commentId = comment._id;
  239. }
  240. if (activity.attachmentId) {
  241. params.attachment = activity.attachmentName;
  242. params.attachmentId = activity.attachmentId;
  243. }
  244. if (activity.checklistId) {
  245. const checklist = activity.checklist();
  246. if (checklist) {
  247. if (checklist.title) {
  248. params.checklist = checklist.title;
  249. }
  250. }
  251. }
  252. if (activity.checklistItemId) {
  253. const checklistItem = activity.checklistItem();
  254. if (checklistItem) {
  255. if (checklistItem.title) {
  256. params.checklistItem = checklistItem.title;
  257. }
  258. }
  259. }
  260. if (activity.customFieldId) {
  261. const customField = activity.customField();
  262. if (customField) {
  263. if (customField.name) {
  264. params.customField = customField.name;
  265. }
  266. if (activity.value) {
  267. params.customFieldValue = activity.value;
  268. }
  269. }
  270. }
  271. // Label activity did not work yet, unable to edit labels when tried this.
  272. //if (activity.labelId) {
  273. // const label = activity.label();
  274. // params.label = label.name;
  275. // params.labelId = activity.labelId;
  276. //}
  277. if (
  278. (!activity.timeKey || activity.timeKey === 'dueAt') &&
  279. activity.timeValue
  280. ) {
  281. // due time reminder, if it doesn't have old value, it's a brand new set, need some differentiation
  282. title = activity.timeOldValue ? 'act-withDue' : 'act-newDue';
  283. }
  284. ['timeValue', 'timeOldValue'].forEach(key => {
  285. // copy time related keys & values to params
  286. const value = activity[key];
  287. if (value) params[key] = value;
  288. });
  289. if (board) {
  290. const BIGEVENTS = process.env.BIGEVENTS_PATTERN; // if environment BIGEVENTS_PATTERN is set, any activityType matching it is important
  291. if (BIGEVENTS) {
  292. try {
  293. const atype = activity.activityType;
  294. if (new RegExp(BIGEVENTS).exec(atype)) {
  295. watchers = _.union(
  296. watchers,
  297. board.activeMembers().map(member => member.userId),
  298. ); // notify all active members for important events
  299. }
  300. } catch (e) {
  301. // passed env var BIGEVENTS_PATTERN is not a valid regex
  302. }
  303. }
  304. const watchingUsers = _.pluck(
  305. _.where(board.watchers, { level: 'watching' }),
  306. 'userId',
  307. );
  308. const trackingUsers = _.pluck(
  309. _.where(board.watchers, { level: 'tracking' }),
  310. 'userId',
  311. );
  312. watchers = _.union(
  313. watchers,
  314. watchingUsers,
  315. _.intersection(participants, trackingUsers),
  316. );
  317. }
  318. Notifications.getUsers(watchers).forEach(user => {
  319. // don't notify a user of their own behavior
  320. if (user._id !== userId) {
  321. Notifications.notify(user, title, description, params);
  322. }
  323. });
  324. const integrations = Integrations.find({
  325. boardId: { $in: [board._id, Integrations.Const.GLOBAL_WEBHOOK_ID] },
  326. // type: 'outgoing-webhooks', // all types
  327. enabled: true,
  328. activities: { $in: [description, 'all'] },
  329. }).fetch();
  330. if (integrations.length > 0) {
  331. params.watchers = watchers;
  332. integrations.forEach(integration => {
  333. Meteor.call(
  334. 'outgoingWebhooks',
  335. integration,
  336. description,
  337. params,
  338. () => {
  339. return;
  340. },
  341. );
  342. });
  343. }
  344. });
  345. }
  346. export default Activities;