Skip to content

Commit

Permalink
Add parametrized methods for avatar/icon/splash urls
Browse files Browse the repository at this point in the history
  • Loading branch information
qeled committed Apr 8, 2017
1 parent 4c18e56 commit 45e0419
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,9 +759,9 @@ const Constants = {
ME: "@me",
Endpoints: {
CDN_AVATAR: (userId, hash, format) => `/avatars/${userId}/${hash}.${format || "jpg"}`,
CDN_DM_ICON: (channelId, hash) => `/channel-icons/${channelId}/${hash}.jpg`,
CDN_GUILD_ICON: (guildId, hash) => `/icons/${guildId}/${hash}.jpg`,
CDN_GUILD_SPLASH: (guildId, hash) => `/splashes/${guildId}/${hash}.jpg`,
CDN_DM_ICON: (channelId, hash, format) => `/channel-icons/${channelId}/${hash}.${format || "jpg"}`,
CDN_GUILD_ICON: (guildId, hash, format) => `/icons/${guildId}/${hash}.${format || "jpg"}`,
CDN_GUILD_SPLASH: (guildId, hash, format) => `/splashes/${guildId}/${hash}.${format || "jpg"}`,
CDN_EMOJI: emojiId => `/emojis/${emojiId}.png`,
LOGIN: "/auth/login",
ME: "/users/@me",
Expand Down
20 changes: 20 additions & 0 deletions lib/core/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,25 @@ module.exports = {
emojiToCode(emoji) {
if (typeof emoji === "string") return emoji;
return emoji.id != null ? `${emoji.name}:${emoji.id}` : emoji.name;
},
cdnImage(cdn, endpoint, id, hash, options) {
options = options || {};

const format =
options.format === "jpg" ||
options.format === "webp" ||
options.format === "gif" ||
options.format === "png" ?
options.format : null;

const size = options.size;

const validSizes = (16 | 32 | 64 | 128 | 256 | 512 | 1024 | 2048);

const isPowerOfTwo = size > 0 && (size & (size - 1)) == 0;
if (isPowerOfTwo && (size & validSizes))
return `${cdn}${endpoint(id, hash, format)}?size=${size}`;

return `${cdn}${endpoint(id, hash, format)}`;
}
};
15 changes: 15 additions & 0 deletions lib/interfaces/IDirectMessageChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ class IDirectMessageChannel extends IBase {
return Constants.CDN_ENDPOINT + Endpoints.CDN_DM_ICON(this.id, this.icon);
}

/**
* Creates a string URL of image icon of this channel.
* @param {Object} [options] Object with optional fields:
* `format: ("jpg", "webp", "gif", "png"),
* size: (16, 32, 64, 128, 256, 512, 1024, 2048)`
* @returns {String|null}
*/
getIconURL(options) {
const icon = this.icon;
if (!icon) return null;

return Utils.cdnImage(Constants.CDN_ENDPOINT,
Endpoints.CDN_GUILD_ICON, this.id, icon, options);
}

/**
* Gets first recipient of this channel.
*
Expand Down
30 changes: 30 additions & 0 deletions lib/interfaces/IGuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,36 @@ class IGuild extends IBase {
Endpoints.CDN_GUILD_SPLASH(this.id, this.splash);
}

/**
* Creates a string URL of image icon of this guild.
* @param {Object} [options] Object with optional fields:
* `format: ("jpg", "webp", "gif", "png"),
* size: (16, 32, 64, 128, 256, 512, 1024, 2048)`
* @returns {String|null}
*/
getIconURL(options) {
const icon = this.icon;
if (!icon) return null;

return Utils.cdnImage(Constants.CDN_ENDPOINT,
Endpoints.CDN_GUILD_ICON, this.id, icon, options);
}

/**
* Creates a string URL of invite splash image of this guild.
* @param {Object} [options] Object with optional fields:
* `format: ("jpg", "webp", "gif", "png"),
* size: (16, 32, 64, 128, 256, 512, 1024, 2048)`
* @returns {String|null}
*/
getSplashURL(options) {
const splash = this.splash;
if (!splash) return null;

return Utils.cdnImage(Constants.CDN_ENDPOINT,
Endpoints.CDN_GUILD_SPLASH, this.id, splash, options);
}

/**
* Checks whether the `user` is the owner of this guild.
* @param {IGuildMember|IUser|IAuthenticatedUser|String} user
Expand Down
15 changes: 15 additions & 0 deletions lib/interfaces/IUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ class IUser extends IBase {
return Constants.CDN_ENDPOINT + Endpoints.CDN_AVATAR(this.id, avatar, fmt);
}

/**
* Creates a string URL of the user avatar.
* @param {Object} [options] Object with optional fields:
* `format: ("jpg", "webp", "gif", "png"),
* size: (16, 32, 64, 128, 256, 512, 1024, 2048)`
* @returns {String|null}
*/
getAvatarURL(options) {
const avatar = this.avatar;
if (!avatar) return null;

return Utils.cdnImage(Constants.CDN_ENDPOINT,
Endpoints.CDN_AVATAR, this.id, avatar, options);
}

/**
* Gets current JPG avatar URL.
* @returns {String|null}
Expand Down

0 comments on commit 45e0419

Please sign in to comment.