activities.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. const user = activity.user();
  109. params.user = user.getName();
  110. params.userEmails = user.emails;
  111. params.userId = activity.userId;
  112. }
  113. if (activity.boardId) {
  114. board = activity.board();
  115. params.board = board.title;
  116. title = 'act-withBoardTitle';
  117. params.url = board.absoluteUrl();
  118. params.boardId = activity.boardId;
  119. }
  120. if (activity.oldBoardId) {
  121. const oldBoard = activity.oldBoard();
  122. if (oldBoard) {
  123. watchers = _.union(watchers, oldBoard.watchers || []);
  124. params.oldBoard = oldBoard.title;
  125. params.oldBoardId = activity.oldBoardId;
  126. }
  127. }
  128. if (activity.memberId) {
  129. participants = _.union(participants, [activity.memberId]);
  130. params.member = activity.member().getName();
  131. }
  132. if (activity.listId) {
  133. const list = activity.list();
  134. watchers = _.union(watchers, list.watchers || []);
  135. params.list = list.title;
  136. params.listId = activity.listId;
  137. }
  138. if (activity.oldListId) {
  139. const oldList = activity.oldList();
  140. if (oldList) {
  141. watchers = _.union(watchers, oldList.watchers || []);
  142. params.oldList = oldList.title;
  143. params.oldListId = activity.oldListId;
  144. }
  145. }
  146. if (activity.oldSwimlaneId) {
  147. const oldSwimlane = activity.oldSwimlane();
  148. if (oldSwimlane) {
  149. watchers = _.union(watchers, oldSwimlane.watchers || []);
  150. params.oldSwimlane = oldSwimlane.title;
  151. params.oldSwimlaneId = activity.oldSwimlaneId;
  152. }
  153. }
  154. if (activity.cardId) {
  155. const card = activity.card();
  156. participants = _.union(participants, [card.userId], card.members || []);
  157. watchers = _.union(watchers, card.watchers || []);
  158. params.card = card.title;
  159. title = 'act-withCardTitle';
  160. params.url = card.absoluteUrl();
  161. params.cardId = activity.cardId;
  162. }
  163. if (activity.swimlaneId) {
  164. const swimlane = activity.swimlane();
  165. params.swimlane = swimlane.title;
  166. params.swimlaneId = activity.swimlaneId;
  167. }
  168. if (activity.commentId) {
  169. const comment = activity.comment();
  170. params.comment = comment.text;
  171. if (board) {
  172. const atUser = /(?:^|>|\b|\s)@(\S+)(?:\s|$|<|\b)/g;
  173. const comment = params.comment;
  174. if (comment.match(atUser)) {
  175. const commenter = params.user;
  176. while (atUser.exec(comment)) {
  177. const username = RegExp.$1;
  178. if (commenter === username) {
  179. // it's person at himself, ignore it?
  180. continue;
  181. }
  182. const atUser =
  183. Users.findOne(username) || Users.findOne({ username });
  184. const uid = atUser && atUser._id;
  185. params.atUsername = username;
  186. params.atEmails = atUser.emails;
  187. if (board.hasMember(uid)) {
  188. title = 'act-atUserComment';
  189. watchers = _.union(watchers, [uid]);
  190. }
  191. }
  192. }
  193. }
  194. params.commentId = comment._id;
  195. }
  196. if (activity.attachmentId) {
  197. const attachment = activity.attachment();
  198. params.attachment = attachment.original.name;
  199. params.attachmentId = attachment._id;
  200. }
  201. if (activity.checklistId) {
  202. const checklist = activity.checklist();
  203. params.checklist = checklist.title;
  204. }
  205. if (activity.checklistItemId) {
  206. const checklistItem = activity.checklistItem();
  207. params.checklistItem = checklistItem.title;
  208. }
  209. if (activity.customFieldId) {
  210. const customField = activity.customField();
  211. params.customField = customField.name;
  212. params.customFieldValue = Activities.findOne({
  213. customFieldId: customField._id,
  214. }).value;
  215. }
  216. // Label activity did not work yet, unable to edit labels when tried this.
  217. //if (activity.labelId) {
  218. // const label = activity.label();
  219. // params.label = label.name;
  220. // params.labelId = activity.labelId;
  221. //}
  222. if (
  223. (!activity.timeKey || activity.timeKey === 'dueAt') &&
  224. activity.timeValue
  225. ) {
  226. // due time reminder
  227. title = 'act-withDue';
  228. }
  229. ['timeValue', 'timeOldValue'].forEach(key => {
  230. // copy time related keys & values to params
  231. const value = activity[key];
  232. if (value) params[key] = value;
  233. });
  234. if (board) {
  235. const BIGEVENTS = process.env.BIGEVENTS_PATTERN || 'due'; // if environment BIGEVENTS_PATTERN is set or default, any activityType matching it is important
  236. try {
  237. const atype = activity.activityType;
  238. if (new RegExp(BIGEVENTS).exec(atype)) {
  239. watchers = _.union(
  240. watchers,
  241. board.activeMembers().map(member => member.userId),
  242. ); // notify all active members for important events system defined or default to all activity related to due date
  243. }
  244. } catch (e) {
  245. // passed env var BIGEVENTS_PATTERN is not a valid regex
  246. }
  247. const watchingUsers = _.pluck(
  248. _.where(board.watchers, { level: 'watching' }),
  249. 'userId',
  250. );
  251. const trackingUsers = _.pluck(
  252. _.where(board.watchers, { level: 'tracking' }),
  253. 'userId',
  254. );
  255. watchers = _.union(
  256. watchers,
  257. watchingUsers,
  258. _.intersection(participants, trackingUsers),
  259. );
  260. }
  261. Notifications.getUsers(watchers).forEach(user => {
  262. Notifications.notify(user, title, description, params);
  263. });
  264. const integrations = Integrations.find({
  265. boardId: { $in: [board._id, Integrations.Const.GLOBAL_WEBHOOK_ID] },
  266. // type: 'outgoing-webhooks', // all types
  267. enabled: true,
  268. activities: { $in: [description, 'all'] },
  269. }).fetch();
  270. if (integrations.length > 0) {
  271. integrations.forEach(integration => {
  272. Meteor.call(
  273. 'outgoingWebhooks',
  274. integration,
  275. description,
  276. params,
  277. () => {
  278. return;
  279. },
  280. );
  281. });
  282. }
  283. });
  284. }
  285. export default Activities;