activities.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. Activities = new Mongo.Collection('activities');
  3. Activities.helpers({
  4. board() {
  5. return ReactiveCache.getBoard(this.boardId);
  6. },
  7. oldBoard() {
  8. return ReactiveCache.getBoard(this.oldBoardId);
  9. },
  10. user() {
  11. return ReactiveCache.getUser(this.userId);
  12. },
  13. member() {
  14. return ReactiveCache.getUser(this.memberId);
  15. },
  16. list() {
  17. return ReactiveCache.getList(this.listId);
  18. },
  19. swimlane() {
  20. return ReactiveCache.getSwimlane(this.swimlaneId);
  21. },
  22. oldSwimlane() {
  23. return ReactiveCache.getSwimlane(this.oldSwimlaneId);
  24. },
  25. oldList() {
  26. return ReactiveCache.getList(this.oldListId);
  27. },
  28. card() {
  29. return ReactiveCache.getCard(this.cardId);
  30. },
  31. comment() {
  32. return ReactiveCache.getCardComment(this.commentId);
  33. },
  34. attachment() {
  35. return ReactiveCache.getAttachment(this.attachmentId);
  36. },
  37. checklist() {
  38. return ReactiveCache.getChecklist(this.checklistId);
  39. },
  40. checklistItem() {
  41. return ReactiveCache.getChecklistItem(this.checklistItemId);
  42. },
  43. subtask() {
  44. return ReactiveCache.getCard(this.subtaskId);
  45. },
  46. customField() {
  47. return ReactiveCache.getCustomField(this.customFieldId);
  48. },
  49. label() {
  50. return ReactiveCache.getLabel(this.labelId); // Fixed: should get a label, not a card
  51. },
  52. });
  53. Activities.before.update((userId, doc, fieldNames, modifier) => {
  54. modifier.$set = modifier.$set || {};
  55. modifier.$set.modifiedAt = new Date();
  56. });
  57. Activities.before.insert((userId, doc) => {
  58. doc.createdAt = new Date();
  59. doc.modifiedAt = doc.createdAt;
  60. });
  61. Activities.after.insert((userId, doc) => {
  62. const activity = Activities._transform(doc);
  63. RulesHelper.executeRules(activity);
  64. });
  65. if (Meteor.isServer) {
  66. Meteor.startup(() => {
  67. Activities._collection.createIndex({ createdAt: -1 });
  68. Activities._collection.createIndex({ modifiedAt: -1 });
  69. Activities._collection.createIndex({ cardId: 1, createdAt: -1 });
  70. Activities._collection.createIndex({ boardId: 1, createdAt: -1 });
  71. Activities._collection.createIndex(
  72. { commentId: 1 },
  73. { partialFilterExpression: { commentId: { $exists: true } } },
  74. );
  75. Activities._collection.createIndex(
  76. { attachmentId: 1 },
  77. { partialFilterExpression: { attachmentId: { $exists: true } } },
  78. );
  79. Activities._collection.createIndex(
  80. { customFieldId: 1 },
  81. { partialFilterExpression: { customFieldId: { $exists: true } } },
  82. );
  83. });
  84. Activities.after.insert((userId, doc) => {
  85. const activity = Activities._transform(doc);
  86. let participants = [];
  87. let watchers = [];
  88. let title = 'act-activity-notify';
  89. const board = ReactiveCache.getBoard(activity.boardId);
  90. const description = `act-${activity.activityType}`;
  91. const params = {
  92. activityId: activity._id,
  93. };
  94. if (activity.userId) {
  95. const user = activity.user();
  96. if (user) {
  97. if (user.getName()) params.user = user.getName();
  98. if (user.emails) params.userEmails = user.emails;
  99. params.userId = activity.userId;
  100. }
  101. }
  102. if (activity.boardId) {
  103. params.board = board?.title || '';
  104. title = 'act-withBoardTitle';
  105. params.url = board?.absoluteUrl();
  106. params.boardId = activity.boardId;
  107. }
  108. if (activity.oldBoardId) {
  109. const oldBoard = activity.oldBoard();
  110. if (oldBoard) {
  111. watchers = _.union(watchers, oldBoard.watchers || []);
  112. params.oldBoard = oldBoard.title;
  113. params.oldBoardId = activity.oldBoardId;
  114. }
  115. }
  116. if (activity.memberId) {
  117. participants = _.union(participants, [activity.memberId]);
  118. const member = activity.member();
  119. if (member) params.member = member.getName();
  120. }
  121. if (activity.listId) {
  122. const list = activity.list();
  123. if (list) {
  124. watchers = _.union(watchers, list.watchers || []);
  125. params.list = list.title;
  126. params.listId = activity.listId;
  127. }
  128. }
  129. if (activity.oldListId) {
  130. const oldList = activity.oldList();
  131. if (oldList) {
  132. watchers = _.union(watchers, oldList.watchers || []);
  133. params.oldList = oldList.title;
  134. params.oldListId = activity.oldListId;
  135. }
  136. }
  137. if (activity.oldSwimlaneId) {
  138. const oldSwimlane = activity.oldSwimlane();
  139. if (oldSwimlane) {
  140. watchers = _.union(watchers, oldSwimlane.watchers || []);
  141. params.oldSwimlane = oldSwimlane.title;
  142. params.oldSwimlaneId = activity.oldSwimlaneId;
  143. }
  144. }
  145. if (activity.cardId) {
  146. const card = activity.card();
  147. participants = _.union(participants, [card.userId], card.members || []);
  148. watchers = _.union(watchers, card.watchers || []);
  149. params.card = card.title;
  150. title = 'act-withCardTitle';
  151. params.url = card.absoluteUrl();
  152. params.cardId = activity.cardId;
  153. }
  154. if (activity.swimlaneId) {
  155. const swimlane = activity.swimlane();
  156. params.swimlane = swimlane?.title;
  157. params.swimlaneId = activity.swimlaneId;
  158. }
  159. if (activity.commentId) {
  160. const comment = activity.comment();
  161. if (comment) {
  162. params.comment = comment.text;
  163. params.commentId = comment._id;
  164. if (board) {
  165. const knownUsers = board.members.map(member => {
  166. const u = ReactiveCache.getUser(member.userId);
  167. if (u) {
  168. member.username = u.username;
  169. member.emails = u.emails;
  170. }
  171. return member;
  172. });
  173. const mentionRegex = /\B@(?:(?:"([\w.\s-]*)")|([\w.-]+))/gi;
  174. let currentMention;
  175. while ((currentMention = mentionRegex.exec(comment.text)) !== null) {
  176. const [ignored, quoteduser, simple] = currentMention;
  177. const username = quoteduser || simple;
  178. if (username === params.user) continue;
  179. if (username === 'board_members') {
  180. const knownUids = knownUsers.map(u => u.userId);
  181. watchers = _.union(watchers, knownUids);
  182. title = 'act-atUserComment';
  183. } else if (username === 'card_members' && activity.cardId) {
  184. const card = activity.card();
  185. watchers = _.union(watchers, card.members);
  186. title = 'act-atUserComment';
  187. } else {
  188. const atUser = _.findWhere(knownUsers, { username });
  189. if (atUser) {
  190. params.atUsername = username;
  191. params.atEmails = atUser.emails;
  192. watchers = _.union(watchers, [atUser.userId]);
  193. title = 'act-atUserComment';
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. if (activity.attachmentId) {
  201. params.attachment = activity.attachmentName;
  202. params.attachmentId = activity.attachmentId;
  203. }
  204. if (activity.checklistId) {
  205. const checklist = activity.checklist();
  206. if (checklist) {
  207. params.checklist = checklist.title;
  208. params.checklistId = activity.checklistId;
  209. }
  210. }
  211. if (activity.checklistItemId) {
  212. const checklistItem = activity.checklistItem();
  213. if (checklistItem) {
  214. params.checklistItem = checklistItem.text;
  215. params.checklistItemId = activity.checklistItemId;
  216. }
  217. }
  218. if (activity.subtaskId) {
  219. const subtask = activity.subtask();
  220. if (subtask) {
  221. params.subtask = subtask.title;
  222. params.subtaskId = activity.subtaskId;
  223. }
  224. }
  225. if (activity.labelId) {
  226. const label = activity.label();
  227. if (label) {
  228. params.label = label.name || label.color;
  229. params.labelId = label._id;
  230. }
  231. }
  232. Notifications.insert({
  233. userId,
  234. participants,
  235. watchers,
  236. title,
  237. description,
  238. params,
  239. });
  240. });
  241. }