activities.js 11 KB

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