use structures::submission::FlairList;
use client::RedditClient;
use responses::FlairSelectorResponse;
use responses::user::{UserAbout as _UserAbout, UserAboutData};
use traits::Created;
use errors::APIError;
pub struct User<'a> {
client: &'a RedditClient,
pub name: String
}
impl<'a> User<'a> {
pub fn new(client: &'a RedditClient, name: &str) -> User<'a> {
User {
client: client,
name: name.to_owned()
}
}
pub fn about(self) -> Result<UserAbout, APIError> {
UserAbout::new(self.client, self.name)
}
pub fn flair_options(&self, subreddit: &str) -> Result<FlairList, APIError> {
let body = format!("user={}", self.name);
let url = format!("/r/{}/api/flairselector", subreddit);
self.client.post_json::<FlairSelectorResponse>(&url, &body, false)
.and_then(|res| Ok(FlairList::new(res.choices)))
}
pub fn flair(&self, subreddit: &str, template: &str) -> Result<(), APIError> {
let body = format!("api_type=json&user={}&flair_template_id={}", self.name, template);
let url = format!("/r/{}/api/selectflair", subreddit);
self.client.post_success(&url, &body, false)
}
}
pub struct UserAbout {
data: UserAboutData
}
impl UserAbout {
pub fn new(client: &RedditClient, name: String) -> Result<UserAbout, APIError> {
let url = format!("/user/{}/about", name);
client.get_json::<_UserAbout>(&url, false)
.and_then(|res| Ok(UserAbout {
data: res.data
}))
}
pub fn link_karma(&self) -> i64 {
self.data.link_karma
}
pub fn comment_karma(&self) -> i64 {
self.data.comment_karma
}
pub fn id(&self) -> &str {
&self.data.id
}
}
impl Created for UserAbout {
fn created(&self) -> i64 {
self.data.created
}
fn created_utc(&self) -> i64 {
self.data.created_utc
}
}