Skip to content

Commit

Permalink
Support webp/gif avatars
Browse files Browse the repository at this point in the history
If the avatar hash begins with "a_", then the avatar is animated and is
a GIF. Otherwise, use WEBP.
  • Loading branch information
Austin Hellyer committed Dec 28, 2016
1 parent ada07fa commit ab778f8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
28 changes: 24 additions & 4 deletions src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,19 @@ use ::client::CACHE;

impl CurrentUser {
/// Returns the formatted URL of the user's icon, if one exists.
///
/// This will produce a WEBP image URL, or GIF if the user has a GIF avatar.
pub fn avatar_url(&self) -> Option<String> {
self.avatar.as_ref().map(|av|
format!(cdn!("/avatars/{}/{}.jpg"), self.id.0, av))
self.avatar.as_ref()
.map(|av| {
let ext = if av.starts_with("a_") {
"gif"
} else {
"webp"
};

format!(cdn!("/avatars/{}/{}.{}?size=1024"), self.id.0, av, ext)
})
}

/// Edits the current user's profile settings.
Expand Down Expand Up @@ -99,9 +109,19 @@ impl CurrentUser {

impl User {
/// Returns the formatted URL of the user's icon, if one exists.
///
/// This will produce a WEBP image URL, or GIF if the user has a GIF avatar.
pub fn avatar_url(&self) -> Option<String> {
self.avatar.as_ref().map(|av|
format!(cdn!("/avatars/{}/{}.jpg"), self.id.0, av))
self.avatar.as_ref()
.map(|av| {
let ext = if av.starts_with("a_") {
"gif"
} else {
"webp"
};

format!(cdn!("/avatars/{}/{}.{}?size=1024"), self.id.0, av, ext)
})
}

/// Gets user as `Member` of a guild.
Expand Down
6 changes: 4 additions & 2 deletions tests/test_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ fn gen() -> User {
fn test_core() {
let mut user = gen();

assert!(user.avatar_url().unwrap().ends_with("/avatars/210/abc.jpg"));
assert!(user.avatar_url().unwrap().ends_with("/avatars/210/abc.webp?size=1024"));

user.avatar = None;
user.avatar = Some("a_aaa".to_owned());
assert!(user.avatar_url().unwrap().ends_with("/avatars/210/a_aaa.gif?size=1024"));

user.avatar = None;
assert!(user.avatar_url().is_none());
}

0 comments on commit ab778f8

Please sign in to comment.