diff options
author | iximeow <me@iximeow.net> | 2017-10-17 02:33:59 -0700 |
---|---|---|
committer | iximeow <me@iximeow.net> | 2017-10-17 02:33:59 -0700 |
commit | 3e488ad535a8490d7c039dbaa48993143e6ae467 (patch) | |
tree | 1c6312dd3f1f8cc30ec9dcfff944bb1274bf15e3 | |
parent | 9a50ccba1c77cba504b7458e144e6fc5d10b55d1 (diff) |
add rudimentary thread support
-rw-r--r-- | src/commands/follow.rs | 2 | ||||
-rw-r--r-- | src/commands/mod.rs | 6 | ||||
-rw-r--r-- | src/commands/thread.rs | 59 | ||||
-rw-r--r-- | src/display/mod.rs | 7 | ||||
-rw-r--r-- | src/tw/mod.rs | 32 |
5 files changed, 101 insertions, 5 deletions
diff --git a/src/commands/follow.rs b/src/commands/follow.rs index 0da07a8..b2f0aa6 100644 --- a/src/commands/follow.rs +++ b/src/commands/follow.rs @@ -3,8 +3,6 @@ use ::Queryer; use commands::Command; -use std::str::FromStr; - static FOLLOW_URL: &str = "https://api.twitter.com/1.1/friendships/create.json"; static UNFOLLOW_URL: &str = "https://api.twitter.com/1.1/friendships/destroy.json"; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 4052326..49b2cba 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -14,6 +14,7 @@ mod view; mod quit; mod fav; mod follow; +mod thread; pub static COMMANDS: &[&Command] = &[ &show_cache::SHOW_CACHE, @@ -31,7 +32,10 @@ pub static COMMANDS: &[&Command] = &[ &twete::QUOTE, &twete::RETWETE, &twete::REP, - &twete::THREAD + &twete::THREAD, + &thread::FORGET_THREAD, + &thread::REMEMBER_THREAD, + &thread::LIST_THREADS /* &QUIT, &LOOK_UP_USER, diff --git a/src/commands/thread.rs b/src/commands/thread.rs new file mode 100644 index 0000000..92566fc --- /dev/null +++ b/src/commands/thread.rs @@ -0,0 +1,59 @@ +use tw; +use ::Queryer; +use ::display; + +use commands::Command; + +use std::str::FromStr; + +pub static FORGET_THREAD: Command = Command { + keyword: "forget", + params: 1, + exec: forget +}; + +fn forget(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) { + tweeter.forget_thread(line.trim().to_string()); + println!("Ok! Forgot thread {}", line.trim().to_string()); +} + +pub static REMEMBER_THREAD: Command = Command { + keyword: "remember", + params: 2, + exec: remember +}; + +fn remember(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) { + let mut text: String = line.trim().to_string(); + if let Some(id_end_idx) = text.find(" ") { + let name_bare = text.split_off(id_end_idx + 1); + let name = name_bare.trim(); + let id_str = text.trim(); + if name.len() > 0 { + if let Some(inner_twid) = u64::from_str(&id_str).ok() { + if tweeter.tweet_by_innerid(inner_twid).is_some() { + tweeter.set_thread(name.to_string(), inner_twid); + println!("Ok! Recorded {} as thread {}", inner_twid, name); + } + } + } + } +} + +pub static LIST_THREADS: Command = Command { + keyword: "ls_threads", + params: 0, + exec: ls_threads +}; + +fn ls_threads(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { + println!("Threads: "); + for k in tweeter.threads() { + println!("Thread: {}", k); + let latest_inner_id = tweeter.latest_in_thread(k.to_owned()).unwrap(); + let twete = tweeter.tweet_by_innerid(*latest_inner_id).unwrap(); + // gross.. + display::render_twete(&twete.id, tweeter); + println!(""); + } +} diff --git a/src/display/mod.rs b/src/display/mod.rs index 3a48454..aaca4b2 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -99,7 +99,12 @@ impl Render for tw::events::Event { pub fn render_twete(twete_id: &String, tweeter: &tw::TwitterCache) { let id_color = color::Fg(color::Rgb(180, 80, 40)); - let twete = tweeter.retrieve_tweet(twete_id).unwrap(); + let maybe_twete = tweeter.retrieve_tweet(twete_id); + if maybe_twete.is_none() { + println!("No such tweet: {}", twete_id); + return; + } + let twete = maybe_twete.unwrap(); // if we got the tweet, the API gave us the user too let user = tweeter.retrieve_user(&twete.author_id).unwrap(); match twete.rt_tweet { diff --git a/src/tw/mod.rs b/src/tw/mod.rs index 49b7add..b703960 100644 --- a/src/tw/mod.rs +++ b/src/tw/mod.rs @@ -93,6 +93,7 @@ pub struct TwitterCache { pub followers: HashSet<String>, lost_followers: HashSet<String>, follower_history: HashMap<String, (String, i64)>, // userid:date?? + threads: HashMap<String, u64>, // thread : latest_tweet_in_thread #[serde(skip)] id_to_tweet_id: HashMap<u64, String>, #[serde(skip)] @@ -121,7 +122,8 @@ impl TwitterCache { id_to_tweet_id: HashMap::new(), needs_save: false, caching_permitted: true, - current_user: User::default() + current_user: User::default(), + threads: HashMap::new() } } fn new_without_caching() -> TwitterCache { @@ -387,6 +389,34 @@ impl TwitterCache { pub fn get_followers(&self, queryer: &mut ::Queryer) -> Option<serde_json::Value> { queryer.do_api_get(::GET_FOLLOWER_IDS_URL) } + + pub fn set_thread(&mut self, name: String, last_id: u64) -> bool { + self.threads.insert(name, last_id); + true + } + + pub fn update_thread(&mut self, name: String, last_id: u64) -> bool { + // ensure that last_id is threaded tweet from the last one stored by name. + // if no thread is stored by name, just use last_id. + // who'm am'st i kid'ing, just store it for now lol + self.threads.insert(name, last_id); + true + } + + pub fn latest_in_thread(&self, name: String) -> Option<&u64> { + self.threads.get(&name) + } + + pub fn forget_thread(&mut self, name: String) { + self.threads.remove(&name); + } + + /* + * can the return type here be Iterator< + */ + pub fn threads<'a>(&'a self) -> Box<Iterator<Item=&String> + 'a> { + Box::new(self.threads.keys()) + } } fn handle_twitter_event( |