|
|
@@ -10,7 +10,7 @@ const User = require('./user') |
|
|
|
// 2. If the original object was a User object, the original object |
|
|
|
// 3. If the original object was a plain JavaScript object, return |
|
|
|
// a User object with all of the original object's properties. |
|
|
|
let reconstructUserIfNecessary = function (user) { |
|
|
|
let reconstructUserIfNecessary = function (user, robot) { |
|
|
|
if (!user) { |
|
|
|
return null |
|
|
|
} |
|
|
@@ -20,6 +20,9 @@ let reconstructUserIfNecessary = function (user) { |
|
|
|
delete user.id |
|
|
|
// Use the old user as the "options" object, |
|
|
|
// populating the new user with its values. |
|
|
|
// Also add the `robot` field so it gets a reference. |
|
|
|
user.robot = robot |
|
|
|
|
|
|
|
return new User(id, user) |
|
|
|
} else { |
|
|
|
return user |
|
|
@@ -36,6 +39,7 @@ class Brain extends EventEmitter { |
|
|
|
users: {}, |
|
|
|
_private: {} |
|
|
|
} |
|
|
|
this.robot = robot |
|
|
|
|
|
|
|
this.autoSave = true |
|
|
|
|
|
|
@@ -142,7 +146,7 @@ class Brain extends EventEmitter { |
|
|
|
if (data && data.users) { |
|
|
|
for (let k in data.users) { |
|
|
|
let user = this.data.users[k] |
|
|
|
this.data.users[k] = reconstructUserIfNecessary(user) |
|
|
|
this.data.users[k] = reconstructUserIfNecessary(user, this.robot) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@@ -161,6 +165,10 @@ class Brain extends EventEmitter { |
|
|
|
// Returns a User instance of the specified user. |
|
|
|
userForId (id, options) { |
|
|
|
let user = this.data.users[id] |
|
|
|
if (!options) { |
|
|
|
options = {} |
|
|
|
} |
|
|
|
options.robot = this.robot |
|
|
|
|
|
|
|
if (!user) { |
|
|
|
user = new User(id, options) |
|
|
|
0 comments on commit
40712e1