Explorar o código

Models: Remove user from all objects on board leave (Fixes: #667)

Remove the user as member and watcher of all lists, cards and the board itself
when leaving the board.
Alexander Sulfrian %!s(int64=8) %!d(string=hai) anos
pai
achega
7b0e57380a
Modificáronse 1 ficheiros con 36 adicións e 0 borrados
  1. 36 0
      models/boards.js

+ 36 - 0
models/boards.js

@@ -462,6 +462,42 @@ if (Meteor.isServer) {
     });
   };
 
+  // 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);
+      });
+    }
+  });
+
   // 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')) {