activities.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.insert((userId, doc) => {
  64. doc.createdAt = new Date();
  65. });
  66. Activities.after.insert((userId, doc) => {
  67. const activity = Activities._transform(doc);
  68. RulesHelper.executeRules(activity);
  69. });
  70. if (Meteor.isServer) {
  71. // For efficiency create indexes on the date of creation, and on the date of
  72. // creation in conjunction with the card or board id, as corresponding views
  73. // are largely used in the App. See #524.
  74. Meteor.startup(() => {
  75. Activities._collection._ensureIndex({ createdAt: -1 });
  76. Activities._collection._ensureIndex({ modifiedAt: -1 });
  77. Activities._collection._ensureIndex({ cardId: 1, createdAt: -1 });
  78. Activities._collection._ensureIndex({ boardId: 1, createdAt: -1 });
  79. Activities._collection._ensureIndex(
  80. { commentId: 1 },
  81. { partialFilterExpression: { commentId: { $exists: true } } },
  82. );
  83. Activities._collection._ensureIndex(
  84. { attachmentId: 1 },
  85. { partialFilterExpression: { attachmentId: { $exists: true } } },
  86. );
  87. Activities._collection._ensureIndex(
  88. { customFieldId: 1 },
  89. { partialFilterExpression: { customFieldId: { $exists: true } } },
  90. );
  91. // Label activity did not work yet, unable to edit labels when tried this.
  92. //Activities._collection._dropIndex({ labelId: 1 }, { "indexKey": -1 });
  93. //Activities._collection._dropIndex({ labelId: 1 }, { partialFilterExpression: { labelId: { $exists: true } } });
  94. });
  95. Activities.after.insert((userId, doc) => {
  96. const activity = Activities._transform(doc);
  97. let participants = [];
  98. let watchers = [];
  99. let title = 'act-activity-notify';
  100. let board = null;
  101. const description = `act-${activity.activityType}`;
  102. const params = {
  103. activityId: activity._id,
  104. };
  105. if (activity.userId) {
  106. // No need send notification to user of activity
  107. // participants = _.union(participants, [activity.userId]);
  108. params.user = activity.user().getName();
  109. params.userId = activity.userId;
  110. }
  111. if (activity.boardId) {
  112. board = activity.board();
  113. params.board = board.title;
  114. title = 'act-withBoardTitle';
  115. params.url = board.absoluteUrl();
  116. params.boardId = activity.boardId;
  117. }
  118. if (activity.oldBoardId) {
  119. const oldBoard = activity.oldBoard();
  120. if (oldBoard) {
  121. watchers = _.union(watchers, oldBoard.watchers || []);
  122. params.oldBoard = oldBoard.title;
  123. params.oldBoardId = activity.oldBoardId;
  124. }
  125. }
  126. if (activity.memberId) {
  127. participants = _.union(participants, [activity.memberId]);
  128. params.member = activity.member().getName();
  129. }
  130. if (activity.listId) {
  131. const list = activity.list();
  132. watchers = _.union(watchers, list.watchers || []);
  133. params.list = list.title;
  134. params.listId = activity.listId;
  135. }
  136. if (activity.oldListId) {
  137. const oldList = activity.oldList();
  138. if (oldList) {
  139. watchers = _.union(watchers, oldList.watchers || []);
  140. params.oldList = oldList.title;
  141. params.oldListId = activity.oldListId;
  142. }
  143. }
  144. if (activity.oldSwimlaneId) {
  145. const oldSwimlane = activity.oldSwimlane();
  146. if (oldSwimlane) {
  147. watchers = _.union(watchers, oldSwimlane.watchers || []);
  148. params.oldSwimlane = oldSwimlane.title;
  149. params.oldSwimlaneId = activity.oldSwimlaneId;
  150. }
  151. }
  152. if (activity.cardId) {
  153. const card = activity.card();
  154. participants = _.union(participants, [card.userId], card.members || []);
  155. watchers = _.union(watchers, card.watchers || []);
  156. params.card = card.title;
  157. title = 'act-withCardTitle';
  158. params.url = card.absoluteUrl();
  159. params.cardId = activity.cardId;
  160. }
  161. if (activity.swimlaneId) {
  162. const swimlane = activity.swimlane();
  163. params.swimlane = swimlane.title;
  164. params.swimlaneId = activity.swimlaneId;
  165. }
  166. if (activity.commentId) {
  167. const comment = activity.comment();
  168. params.comment = comment.text;
  169. params.commentId = comment._id;
  170. }
  171. if (activity.attachmentId) {
  172. const attachment = activity.attachment();
  173. params.attachment = attachment.original.name;
  174. params.attachmentId = attachment._id;
  175. }
  176. if (activity.checklistId) {
  177. const checklist = activity.checklist();
  178. params.checklist = checklist.title;
  179. }
  180. if (activity.checklistItemId) {
  181. const checklistItem = activity.checklistItem();
  182. params.checklistItem = checklistItem.title;
  183. }
  184. if (activity.customFieldId) {
  185. const customField = activity.customField();
  186. params.customField = customField.name;
  187. params.customFieldValue = customField.text;
  188. }
  189. // Label activity did not work yet, unable to edit labels when tried this.
  190. //if (activity.labelId) {
  191. // const label = activity.label();
  192. // params.label = label.name;
  193. // params.labelId = activity.labelId;
  194. //}
  195. if (board) {
  196. const watchingUsers = _.pluck(
  197. _.where(board.watchers, { level: 'watching' }),
  198. 'userId',
  199. );
  200. const trackingUsers = _.pluck(
  201. _.where(board.watchers, { level: 'tracking' }),
  202. 'userId',
  203. );
  204. watchers = _.union(
  205. watchers,
  206. watchingUsers,
  207. _.intersection(participants, trackingUsers),
  208. );
  209. }
  210. Notifications.getUsers(watchers).forEach(user => {
  211. Notifications.notify(user, title, description, params);
  212. });
  213. const integrations = Integrations.find({
  214. boardId: board._id,
  215. type: 'outgoing-webhooks',
  216. enabled: true,
  217. activities: { $in: [description, 'all'] },
  218. }).fetch();
  219. if (integrations.length > 0) {
  220. Meteor.call('outgoingWebhooks', integrations, description, params);
  221. }
  222. });
  223. }
  224. export default Activities;