diff options
Diffstat (limited to 'models/boards.js')
-rw-r--r-- | models/boards.js | 82 |
1 files changed, 71 insertions, 11 deletions
diff --git a/models/boards.js b/models/boards.js index 029ff7d1..3051ef1e 100644 --- a/models/boards.js +++ b/models/boards.js @@ -449,17 +449,76 @@ if (Meteor.isServer) { ); }); + const foreachRemovedMember = (doc, modifier, callback) => { + Object.keys(modifier).forEach((set) => { + if (modifier[set] !== false) { + return; + } + + const parts = set.split('.'); + if (parts.length === 3 && parts[0] === 'members' && parts[2] === 'isActive') { + callback(doc.members[parts[1]].userId); + } + }); + }; + + // Remove a member from all objects of the board before leaving the board + Boards.before.update((userId, doc, fieldNames, modifier) => { + if (!_.contains(fieldNames, 'members')) { + return; + } + + if (modifier.$set) { + const boardId = doc._id; + foreachRemovedMember(doc, modifier.$set, (memberId) => { + Cards.update( + { boardId }, + { + $pull: { + members: memberId, + watchers: memberId, + }, + }, + { multi: true } + ); + + Lists.update( + { boardId }, + { + $pull: { + watchers: memberId, + }, + }, + { multi: true } + ); + + const board = Boards._transform(doc); + board.setWatcher(memberId, false); + + // Remove board from users starred list + if (!board.isPublic()) { + Users.update( + memberId, + { + $pull: { + 'profile.starredBoards': boardId, + }, + } + ); + } + }); + } + }); + // Add a new activity if we add or remove a member to the board Boards.after.update((userId, doc, fieldNames, modifier) => { if (!_.contains(fieldNames, 'members')) { return; } - let memberId; - // Say hello to the new member if (modifier.$push && modifier.$push.members) { - memberId = modifier.$push.members.userId; + const memberId = modifier.$push.members.userId; Activities.insert({ userId, memberId, @@ -470,14 +529,15 @@ if (Meteor.isServer) { } // Say goodbye to the former member - if (modifier.$pull && modifier.$pull.members) { - memberId = modifier.$pull.members.userId; - Activities.insert({ - userId, - memberId, - type: 'member', - activityType: 'removeBoardMember', - boardId: doc._id, + if (modifier.$set) { + foreachRemovedMember(doc, modifier.$set, (memberId) => { + Activities.insert({ + userId, + memberId, + type: 'member', + activityType: 'removeBoardMember', + boardId: doc._id, + }); }); } }); |