From c6bcf69dc35849f0c85b11e7136895c6318c39a2 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Wed, 26 Oct 2016 10:23:39 -0500 Subject: [PATCH] added User#fetchProfile (#835) * add User#fetchProfile * fix merge conflicts? --- src/client/rest/RESTMethods.js | 10 +++++++ src/structures/User.js | 8 ++++++ src/structures/UserConnection.js | 48 +++++++++++++++++++++++++++++++ src/structures/UserProfile.js | 49 ++++++++++++++++++++++++++++++++ src/util/Constants.js | 1 + 5 files changed, 116 insertions(+) create mode 100644 src/structures/UserConnection.js create mode 100644 src/structures/UserProfile.js diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index 7c11c8e00..1555d4e95 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -8,6 +8,7 @@ const GuildMember = requireStructure('GuildMember'); const Role = requireStructure('Role'); const Invite = requireStructure('Invite'); const Webhook = requireStructure('Webhook'); +const UserProfile = requireStructure('UserProfile'); class RESTMethods { constructor(restManager) { @@ -680,6 +681,15 @@ class RESTMethods { }); } + fetchUserProfile(user) { + return new Promise((resolve, reject) => { + this.rest.makeRequest('get', Constants.Endpoints.userProfile(user.id), true) + .then(data => { + resolve(new UserProfile(user, data)); + }).catch(reject); + }); + } + blockUser(user) { return new Promise((resolve, reject) => { this.rest.makeRequest('put', `${Constants.Endpoints.relationships('@me')}/${user.id}`, true, { type: 2 }) diff --git a/src/structures/User.js b/src/structures/User.js index 5eaea7aba..b9ac5b522 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -167,6 +167,14 @@ class User { return this.client.rest.methods.unblockUser(this); } + /** + * Get the profile of the user + * @returns {Promise} + */ + fetchProfile() { + return this.client.rest.methods.fetchUserProfile(this); + } + /** * Checks if the user is equal to another. It compares username, ID, discriminator, status and the game being played. * It is recommended to compare equality by using `user.id === user2.id` unless you want to compare all properties. diff --git a/src/structures/UserConnection.js b/src/structures/UserConnection.js new file mode 100644 index 000000000..d25df0300 --- /dev/null +++ b/src/structures/UserConnection.js @@ -0,0 +1,48 @@ +/** + * Represents a User Connection object (or "platform identity") + */ +class UserConnection { + constructor(user, data) { + /** + * The user that owns the Connection + * @type {User} + */ + this.user = user; + + this.setup(data); + } + + setup(data) { + /** + * The type of the Connection + * @type {string} + */ + this.type = data.type; + + /** + * The username of the connection account + * @type {string} + */ + this.name = data.name; + + /** + * The id of the connection account + * @type {string} + */ + this.id = data.id; + + /** + * Whether the connection is revoked + * @type {Boolean} + */ + this.revoked = data.revoked; + + /** + * an array of partial server integrations (not yet implemented in this lib) + * @type {Object[]} + */ + this.integrations = data.integrations; + } +} + +module.exports = UserConnection; diff --git a/src/structures/UserProfile.js b/src/structures/UserProfile.js new file mode 100644 index 000000000..4150b3a72 --- /dev/null +++ b/src/structures/UserProfile.js @@ -0,0 +1,49 @@ +const Collection = require('../util/Collection'); +const UserConnection = require('./UserConnection'); + +/** + * Represents a user's profile on Discord. + */ +class UserProfile { + constructor(user, data) { + /** + * The owner of the profile + * @type {User} + */ + this.user = user; + + /** + * The Client that created the instance of the the User. + * @type {Client} + */ + this.client = this.user.client; + Object.defineProperty(this, 'client', { enumerable: false, configurable: false }); + + /** + * Guilds that the ClientUser and the User share + * @type {Collection} + */ + this.mutualGuilds = new Collection(); + + /** + * The user's connections + * @type {Collection} + */ + this.connections = new Collection(); + + this.setup(data); + } + + setup(data) { + for (const guild of data.mutual_guilds) { + if (this.client.guilds.has(guild.id)) { + this.mutualGuilds.set(guild.id, this.client.guilds.get(guild.id)); + } + } + for (const connection of data.connected_accounts) { + this.connections.set(connection.id, new UserConnection(this.user, connection)); + } + } +} + +module.exports = UserProfile; diff --git a/src/util/Constants.js b/src/util/Constants.js index 95d203319..60575cd61 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -82,6 +82,7 @@ const Endpoints = exports.Endpoints = { // users user: (userID) => `${API}/users/${userID}`, userChannels: (userID) => `${Endpoints.user(userID)}/channels`, + userProfile: (userID) => `${Endpoints.user(userID)}/profile`, avatar: (userID, avatar) => userID === '1' ? avatar : `${Endpoints.user(userID)}/avatars/${avatar}.jpg`, me: `${API}/users/@me`, meGuild: (guildID) => `${Endpoints.me}/guilds/${guildID}`,