Skip to content

Commit

Permalink
Add user static avatar url methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Austin Hellyer committed Dec 29, 2016
1 parent 6a887b2 commit c36841d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ impl CurrentUser {
pub fn guilds(&self) -> Result<Vec<GuildInfo>> {
rest::get_guilds(GuildPagination::After(GuildId(0)), 100)
}

/// Returns a static formatted URL of the user's icon, if one exists.
///
/// This will always produce a WEBP image URL.
pub fn static_avatar_url(&self) -> Option<String> {
self.avatar.as_ref()
.map(|av| format!(cdn!("/avatars/{}/{}.webp?size=1024"), self.id.0, av))
}
}

impl User {
Expand Down Expand Up @@ -235,6 +243,14 @@ impl User {
},
}
}

/// Returns a static formatted URL of the user's icon, if one exists.
///
/// This will always produce a WEBP image URL.
pub fn static_avatar_url(&self) -> Option<String> {
self.avatar.as_ref()
.map(|av| format!(cdn!("/avatars/{}/{}.webp?size=1024"), self.id.0, av))
}
}

impl fmt::Display for User {
Expand Down
2 changes: 2 additions & 0 deletions tests/test_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ fn test_core() {
let mut user = gen();

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

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

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

0 comments on commit c36841d

Please sign in to comment.