From f944dce5c7824bbceca232ca4281fb6590a17078 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Sat, 27 Aug 2016 10:22:07 -0400 Subject: [PATCH] Add client.cacheUser method (#548) * Add client.cacheUser method * Fixed ESLint issues * Added existence check first --- src/client/Client.js | 10 ++++++++++ src/client/actions/ActionsManager.js | 1 + src/client/actions/UserGet.js | 15 +++++++++++++++ src/client/rest/RESTMethods.js | 10 ++++++++++ src/util/Constants.js | 11 +++++++---- 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/client/actions/UserGet.js diff --git a/src/client/Client.js b/src/client/Client.js index f08446265..c9567f3fe 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -135,6 +135,16 @@ class Client extends EventEmitter { return this.rest.methods.loginToken(email); } + /** + * Forces a user to be cached. + * @param {String} id The ID of the user to cache + * @return {Promise} + */ + cacheUser(id) { + if (this.users.has(id)) return Promise.resolve(this.users.get(id)); + return this.rest.methods.getUser(id); + } + /** * Returns a Collection, mapping Guild ID to Voice Connections. * @readonly diff --git a/src/client/actions/ActionsManager.js b/src/client/actions/ActionsManager.js index ef94f7787..42605be56 100644 --- a/src/client/actions/ActionsManager.js +++ b/src/client/actions/ActionsManager.js @@ -17,6 +17,7 @@ class ActionsManager { this.register('GuildRoleCreate'); this.register('GuildRoleDelete'); this.register('GuildRoleUpdate'); + this.register('UserGet'); this.register('UserUpdate'); } diff --git a/src/client/actions/UserGet.js b/src/client/actions/UserGet.js new file mode 100644 index 000000000..435d046a5 --- /dev/null +++ b/src/client/actions/UserGet.js @@ -0,0 +1,15 @@ +const Action = require('./Action'); + +class UserGetAction extends Action { + + handle(data) { + const client = this.client; + const user = client.dataManager.newUser(data); + + return { + user, + }; + } +} + +module.exports = UserGetAction; diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index 25dff5b32..0a53d07c0 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -193,6 +193,16 @@ class RESTMethods { }); } + getUser(userID) { + return new Promise((resolve, reject) => { + this.rest.makeRequest('get', Constants.Endpoints.user(userID), true) + .then((data) => { + resolve(this.rest.client.actions.UserGet.handle(data).user); + }) + .catch(reject); + }); + } + updateCurrentUser(_data) { return new Promise((resolve, reject) => { const user = this.rest.client.user; diff --git a/src/util/Constants.js b/src/util/Constants.js index 80fcb292b..8cf7358a8 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -75,12 +75,15 @@ const Endpoints = exports.Endpoints = { // general endpoints login: `${API}/auth/login`, logout: `${API}/auth/logout`, + gateway: `${API}/gateway`, + invite: (id) => `${API}/invite/${id}`, + + // users + user: (userID) => `${API}/users/${userID}`, + userChannels: (userID) => `${Endpoints.user(userID)}/channels`, + avatar: (userID, avatar) => `${Endpoints.user(userID)}/avatars/${avatar}.jpg`, me: `${API}/users/@me`, meGuild: (guildID) => `${Endpoints.me}/guilds/${guildID}`, - gateway: `${API}/gateway`, - userChannels: (userID) => `${API}/users/${userID}/channels`, - avatar: (userID, avatar) => `${API}/users/${userID}/avatars/${avatar}.jpg`, - invite: (id) => `${API}/invite/${id}`, // guilds guilds: `${API}/guilds`,