@@ -10,7 +10,7 @@ const User = require('./user')
1010// 2. If the original object was a User object, the original object
1111// 3. If the original object was a plain JavaScript object, return
1212// a User object with all of the original object's properties.
13- let reconstructUserIfNecessary = function ( user ) {
13+ let reconstructUserIfNecessary = function ( user , robot ) {
1414 if ( ! user ) {
1515 return null
1616 }
@@ -20,6 +20,9 @@ let reconstructUserIfNecessary = function (user) {
2020 delete user . id
2121 // Use the old user as the "options" object,
2222 // populating the new user with its values.
23+ // Also add the `robot` field so it gets a reference.
24+ user . robot = robot
25+
2326 return new User ( id , user )
2427 } else {
2528 return user
@@ -36,6 +39,7 @@ class Brain extends EventEmitter {
3639 users : { } ,
3740 _private : { }
3841 }
42+ this . robot = robot
3943
4044 this . autoSave = true
4145
@@ -142,7 +146,7 @@ class Brain extends EventEmitter {
142146 if ( data && data . users ) {
143147 for ( let k in data . users ) {
144148 let user = this . data . users [ k ]
145- this . data . users [ k ] = reconstructUserIfNecessary ( user )
149+ this . data . users [ k ] = reconstructUserIfNecessary ( user , this . robot )
146150 }
147151 }
148152
@@ -161,6 +165,10 @@ class Brain extends EventEmitter {
161165 // Returns a User instance of the specified user.
162166 userForId ( id , options ) {
163167 let user = this . data . users [ id ]
168+ if ( ! options ) {
169+ options = { }
170+ }
171+ options . robot = this . robot
164172
165173 if ( ! user ) {
166174 user = new User ( id , options )
0 commit comments