From 9bbdacc79a89667e0d6f1ed30c415e5350ad468b Mon Sep 17 00:00:00 2001 From: Liming Xie Date: Tue, 5 Jan 2016 23:26:02 +0800 Subject: Add notification, allow watch boards / lists / cards --- models/activities.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'models/activities.js') diff --git a/models/activities.js b/models/activities.js index 5de07ee5..0aa4fa54 100644 --- a/models/activities.js +++ b/models/activities.js @@ -48,4 +48,77 @@ if (Meteor.isServer) { createdAt: -1, }); }); + + Activities.after.insert((userId, doc) => { + const activity = Activities.findOne(doc._id); + let participants = []; + let watchers = []; + let title = 'Wekan Notification'; + let board = null; + const description = `act-${activity.activityType}`; + const params = { + activityId: activity._id, + }; + if (activity.userId) { + // No need send notification to user of activity + // participants = _.union(participants, [activity.userId]); + params.user = activity.user().getName(); + } + if (activity.boardId) { + board = activity.board(); + params.board = board.title; + title = 'act-withBoardTitle'; + params.url = board.absoluteUrl(); + } + if (activity.memberId) { + participants = _.union(participants, [activity.memberId]); + params.member = activity.member().getName(); + } + if (activity.listId) { + const list = activity.list(); + watchers = _.union(watchers, list.watchers || []); + params.list = list.title; + } + if (activity.oldListId) { + const oldList = activity.oldList(); + watchers = _.union(watchers, oldList.watchers || []); + params.oldList = oldList.title; + } + if (activity.cardId) { + const card = activity.card(); + participants = _.union(participants, [card.userId], card.members || []); + watchers = _.union(watchers, card.watchers || []); + params.card = card.title; + title = 'act-withCardTitle'; + params.url = card.absoluteUrl(); + } + if (activity.commentId) { + const comment = activity.comment(); + params.comment = comment.text; + } + if (activity.attachmentId) { + const attachment = activity.attachment(); + params.attachment = attachment._id; + } + if (board) { + const boardWatching = _.pluck(_.where(board.watchers, {level: 'watching'}), 'userId'); + const boardTracking = _.pluck(_.where(board.watchers, {level: 'tracking'}), 'userId'); + const boardMuted = _.pluck(_.where(board.watchers, {level: 'muted'}), 'userId'); + switch(board.getWatchDefault()) { + case 'muted': + participants = _.intersection(participants, boardTracking); + watchers = _.intersection(watchers, boardTracking); + break; + case 'tracking': + participants = _.difference(participants, boardMuted); + watchers = _.difference(watchers, boardMuted); + break; + } + watchers = _.union(watchers, boardWatching || []); + } + + Notifications.getUsers(participants, watchers).forEach((user) => { + Notifications.notify(user, title, description, params); + }); + }); } -- cgit v1.2.3-1-g7c22