Procházet zdrojové kódy

Fix initial board creation

We cannot rely on the automatic userId setting of the collection hooks.
If a user is created during invitation, the userId field will contain
the id of the inviting user.

This fix this, by mocking the CollectionHooks.getUserId function and
returning the userId of the new user for all new documents after
creating the user.
Alexander Sulfrian před 9 roky
rodič
revize
ae2c1fb77f
2 změnil soubory, kde provedl 20 přidání a 13 odebrání
  1. 1 0
      .eslintrc.json
  2. 19 13
      models/users.js

+ 1 - 0
.eslintrc.json

@@ -74,6 +74,7 @@
     "Avatars": true,
     "Avatars": true,
     "BlazeComponent": false,
     "BlazeComponent": false,
     "BlazeLayout": false,
     "BlazeLayout": false,
+    "CollectionHooks": false,
     "DocHead": false,
     "DocHead": false,
     "ESSearchResults": false,
     "ESSearchResults": false,
     "FastRender": false,
     "FastRender": false,

+ 19 - 13
models/users.js

@@ -388,25 +388,31 @@ if (Meteor.isServer) {
     incrementBoards(_.difference(newIds, oldIds), +1);
     incrementBoards(_.difference(newIds, oldIds), +1);
   });
   });
 
 
+  const fakeUserId = new Meteor.EnvironmentVariable();
+  const getUserId = CollectionHooks.getUserId;
+  CollectionHooks.getUserId = () => {
+    return fakeUserId.get() || getUserId();
+  };
+
   // XXX i18n
   // XXX i18n
   Users.after.insert((userId, doc) => {
   Users.after.insert((userId, doc) => {
-    const ExampleBoard = {
-      title: 'Welcome Board',
-      userId: doc._id,
-      permission: 'private',
+    const fakeUser = {
+      extendAutoValueContext: {
+        userId: doc._id,
+      },
     };
     };
 
 
-    // Insert the Welcome Board
-    Boards.insert(ExampleBoard, (err, boardId) => {
+    fakeUserId.withValue(doc._id, () => {
+      // Insert the Welcome Board
+      Boards.insert({
+        title: 'Welcome Board',
+        permission: 'private',
+      }, fakeUser, (err, boardId) => {
 
 
-      ['Basics', 'Advanced'].forEach((title) => {
-        const list = {
-          title,
-          boardId,
-          userId: ExampleBoard.userId,
-        };
+        ['Basics', 'Advanced'].forEach((title) => {
+          Lists.insert({ title, boardId }, fakeUser);
+        });
 
 
-        Lists.insert(list);
       });
       });
     });
     });
   });
   });