From ca0762652e293ad9d35b03b537c02d218e44a13f Mon Sep 17 00:00:00 2001 From: Andy Wortman Date: Fri, 10 Nov 2017 04:04:00 -0800 Subject: very hackily add notion of user credentials and PIN auth also fix bug where cached user info takes precedence over (possibly updated) api json user info --- src/tw/mod.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++------ src/tw/user.rs | 2 +- 2 files changed, 48 insertions(+), 7 deletions(-) (limited to 'src/tw') diff --git a/src/tw/mod.rs b/src/tw/mod.rs index b1bc9c2..767aae9 100644 --- a/src/tw/mod.rs +++ b/src/tw/mod.rs @@ -92,12 +92,24 @@ pub fn full_twete_text(twete: &serde_json::map::Map) twete_text } +#[derive(Clone, Serialize, Deserialize)] +pub struct Credential { + pub key: String, + pub secret: String +} + #[derive(Serialize, Deserialize)] pub struct TwitterCache { #[serde(skip)] pub users: HashMap, #[serde(skip)] pub tweets: HashMap, + #[serde(skip)] + pub WIP_auth: Option, + pub app_key: Credential, + // right now we're stuck assuming one profile. + // alts and such will be others here. + pub profile: Option, following: HashSet, following_history: HashMap, // userid:date?? pub followers: HashSet, @@ -255,12 +267,18 @@ impl TwitterCache { const PROFILE_DIR: &'static str = "cache/"; const TWEET_CACHE: &'static str = "cache/tweets.json"; const USERS_CACHE: &'static str = "cache/users.json"; - const PROFILE_CACHE: &'static str = "cache/profile.json"; // this should involve MY user id.. + const PROFILE_CACHE: &'static str = "cache/profile.json"; fn new() -> TwitterCache { TwitterCache { users: HashMap::new(), tweets: HashMap::new(), + WIP_auth: None, + app_key: Credential { + key: "".to_owned(), + secret: "".to_owned() + }, + profile: None, // this will become a HashMap when multiple profiles are supported following: HashSet::new(), following_history: HashMap::new(), followers: HashSet::new(), @@ -293,8 +311,19 @@ impl TwitterCache { cache.caching_permitted = false; cache } + pub fn add_profile(&mut self, creds: Credential) { + self.profile = Some(creds); + if self.caching_permitted { + self.store_cache(); + } + } fn cache_user(&mut self, user: User) { - if !self.users.contains_key(&user.id) { + let update_cache = match self.users.get(&user.id) { + Some(cached_user) => &user != cached_user, + None => true + }; + + if update_cache { let mut file = OpenOptions::new() .create(true) @@ -583,20 +612,32 @@ impl TwitterCache { fn look_up_user(&mut self, id: &str, queryer: &mut ::Queryer) -> Result { let url = &format!("{}?user_id={}", ::USER_LOOKUP_URL, id); - queryer.do_api_get(url) + match self.profile { + Some(ref user_creds) => queryer.do_api_get(url, &self.app_key, &user_creds), + None => Err("No authorized user to conduct lookup".to_owned()) + } } fn look_up_tweet(&mut self, id: &str, queryer: &mut ::Queryer) -> Result { let url = &format!("{}&id={}", ::TWEET_LOOKUP_URL, id); - queryer.do_api_get(url) + match self.profile { + Some(ref user_creds) => queryer.do_api_get(url, &self.app_key, &user_creds), + None => Err("No authorized user to conduct lookup".to_owned()) + } } pub fn get_settings(&self, queryer: &mut ::Queryer) -> Result { - queryer.do_api_get(::ACCOUNT_SETTINGS_URL) + match self.profile { + Some(ref user_creds) => queryer.do_api_get(::ACCOUNT_SETTINGS_URL, &self.app_key, &user_creds), + None => Err("No authorized user to request settings".to_owned()) + } } pub fn get_followers(&self, queryer: &mut ::Queryer) -> Result { - queryer.do_api_get(::GET_FOLLOWER_IDS_URL) + match self.profile { + Some(ref user_creds) => queryer.do_api_get(::GET_FOLLOWER_IDS_URL, &self.app_key, &user_creds), + None => Err("No authorized user to request followers".to_owned()) + } } pub fn set_thread(&mut self, name: String, last_id: u64) -> bool { diff --git a/src/tw/user.rs b/src/tw/user.rs index 0af4eb8..86fbe3f 100644 --- a/src/tw/user.rs +++ b/src/tw/user.rs @@ -1,6 +1,6 @@ extern crate serde_json; -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] pub struct User { pub id: String, pub name: String, -- cgit v1.1