diff options
author | Andy Wortman <ixineeringeverywhere@gmail.com> | 2017-10-25 04:07:32 -0700 |
---|---|---|
committer | Andy Wortman <ixineeringeverywhere@gmail.com> | 2017-10-25 04:07:32 -0700 |
commit | ccccc6e1b2ebd62a35715f71c746f3a3b9c0b97e (patch) | |
tree | 8bb44e53dab2a9b5f94284ca0f390061032eb4f1 | |
parent | 580f44092358cc260db00ec9871caecac62de361 (diff) |
begin removing unwrap()
-rw-r--r-- | src/commands/fav.rs | 18 | ||||
-rw-r--r-- | src/commands/follow.rs | 6 | ||||
-rw-r--r-- | src/commands/look_up.rs | 17 | ||||
-rw-r--r-- | src/commands/thread.rs | 11 | ||||
-rw-r--r-- | src/commands/twete.rs | 169 | ||||
-rw-r--r-- | src/commands/view.rs | 41 | ||||
-rw-r--r-- | src/display/mod.rs | 153 | ||||
-rw-r--r-- | src/tw/mod.rs | 32 |
8 files changed, 267 insertions, 180 deletions
diff --git a/src/commands/fav.rs b/src/commands/fav.rs index 1cb41e4..89e1987 100644 --- a/src/commands/fav.rs +++ b/src/commands/fav.rs @@ -15,13 +15,14 @@ pub static UNFAV: Command = Command { }; fn unfav(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { - // TODO handle this unwrap -// let inner_twid = u64::from_str(&line).unwrap(); let maybe_id = TweetId::parse(line.to_owned()); match maybe_id { Ok(twid) => { - let twete = tweeter.retrieve_tweet(&twid).unwrap(); - queryer.do_api_post(&format!("{}?id={}", UNFAV_TWEET_URL, twete.id)); + if let Some(twete) = tweeter.retrieve_tweet(&twid).map(|x| x.clone()) { // TODO: no clone when this stops taking &mut self + queryer.do_api_post(&format!("{}?id={}", UNFAV_TWEET_URL, twete.id)); + } else { + tweeter.display_info.status(format!("No tweet for id: {:?}", twid)); + } } Err(e) => { println!("Invalid id: {}", e); @@ -36,12 +37,15 @@ pub static FAV: Command = Command { }; fn fav(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { - // TODO handle this unwrap let maybe_id = TweetId::parse(line.to_owned()); match maybe_id { Ok(twid) => { - let twete = tweeter.retrieve_tweet(&twid).unwrap(); - queryer.do_api_post(&format!("{}?id={}", FAV_TWEET_URL, twete.id)); + // tweeter.to_twitter_tweet_id(twid)... + if let Some(twete) = tweeter.retrieve_tweet(&twid).map(|x| x.clone()) { // TODO: no clone when this stops taking &mut self + queryer.do_api_post(&format!("{}?id={}", FAV_TWEET_URL, twete.id)); + } else { + tweeter.display_info.status(format!("No tweet for id: {:?}", twid)); + } } Err(e) => { println!("Invalid id: {}", e); diff --git a/src/commands/follow.rs b/src/commands/follow.rs index b2f0aa6..ad121e5 100644 --- a/src/commands/follow.rs +++ b/src/commands/follow.rs @@ -13,8 +13,7 @@ pub static UNFOLLOW: Command = Command { }; fn unfl(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { - // TODO handle this unwrap - let screen_name = line.trim(); //u64::from_str(&line).unwrap(); + let screen_name = line.trim(); queryer.do_api_post(&format!("{}?screen_name={}", FOLLOW_URL, screen_name)); } @@ -25,7 +24,6 @@ pub static FOLLOW: Command = Command { }; fn fl(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { - // TODO handle this unwrap - let screen_name = line.trim(); //u64::from_str(&line).unwrap(); + let screen_name = line.trim(); println!("fl resp: {:?}", queryer.do_api_post(&format!("{}?screen_name={}", UNFOLLOW_URL, screen_name))); } diff --git a/src/commands/look_up.rs b/src/commands/look_up.rs index 386fade..701ce2d 100644 --- a/src/commands/look_up.rs +++ b/src/commands/look_up.rs @@ -1,4 +1,6 @@ use tw; +use tw::TweetId; +use display; use ::Queryer; use commands::Command; @@ -25,9 +27,16 @@ pub static LOOK_UP_TWEET: Command = Command { // TODO: make this parse a proper tweet id fn look_up_tweet(line: String, tweeter: &mut tw::TwitterCache, mut queryer: &mut Queryer) { - if let Some(tweet) = tweeter.fetch_tweet(&line, &mut queryer) { - println!("{:?}", tweet); - } else { -// println!("Couldn't retrieve {}", tweetid); + match TweetId::parse(line) { + Ok(twid) => { + if let Some(tweet) = tweeter.fetch_tweet(&twid, &mut queryer).map(|x| x.clone()) { + tweeter.display_info.recv(display::Infos::Tweet(twid)); + } else { + tweeter.display_info.status(format!("Couldn't retrieve {:?}", twid)); + } + }, + Err(e) => { + tweeter.display_info.status(format!("Invalid id {:?}", e)); + } } } diff --git a/src/commands/thread.rs b/src/commands/thread.rs index fd491ba..8880af7 100644 --- a/src/commands/thread.rs +++ b/src/commands/thread.rs @@ -33,12 +33,15 @@ fn remember(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer let maybe_id = TweetId::parse(line.to_owned()); match maybe_id { Ok(twid) => { - let twete = tweeter.retrieve_tweet(&twid).unwrap().clone(); - tweeter.set_thread(name.to_string(), twete.internal_id); - println!("Ok! Recorded {:?} as thread {}", twid, name); + if let Some(twete) = tweeter.retrieve_tweet(&twid).map(|x| x.clone()) { + tweeter.set_thread(name.to_string(), twete.internal_id); + tweeter.display_info.status(format!("Ok! Recorded {:?} as thread {}", twid, name)); + } else { + tweeter.display_info.status(format!("No tweet for id: {:?}", twid)); + } } Err(e) => { - println!("Invalid id: {}", e); + tweeter.display_info.status(format!("Invalid id: {}", e)); } } } diff --git a/src/commands/twete.rs b/src/commands/twete.rs index 9f5cb0d..b0530c8 100644 --- a/src/commands/twete.rs +++ b/src/commands/twete.rs @@ -18,9 +18,19 @@ pub static DEL: Command = Command { }; fn del(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { - let inner_twid = u64::from_str(&line).unwrap(); - let twete = tweeter.retrieve_tweet(&TweetId::Bare(inner_twid)).unwrap(); - queryer.do_api_post(&format!("{}/{}.json", DEL_TWEET_URL, twete.id)); + match TweetId::parse(line.clone()) { + Ok(twid) => { + // TODO this really converts twid to a TweetId::Twitter + if let Some(twitter_id) = tweeter.retrieve_tweet(&twid).map(|x| x.id.to_owned()) { + queryer.do_api_post(&format!("{}/{}.json", DEL_TWEET_URL, twitter_id)); + } else { + tweeter.display_info.status(format!("No tweet for id {:?}", twid)); + } + }, + Err(e) => { + tweeter.display_info.status(format!("Invalid id: {:?}", line)); + } + } } pub static TWETE: Command = Command { @@ -55,19 +65,23 @@ fn thread(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { let reply = reply_bare.trim(); let id_str = text.trim(); if reply.len() > 0 { - if let Some(inner_twid) = u64::from_str(&id_str).ok() { - if let Some(twete) = tweeter.retrieve_tweet(&TweetId::Bare(inner_twid)).map(|x| x.clone()) { - let handle = &tweeter.retrieve_user(&twete.author_id).unwrap().handle; - // TODO: definitely breaks if you change your handle right now - if handle == &tweeter.current_user.handle { - let substituted = ::url_encode(reply); - queryer.do_api_post(&format!("{}?status={}&in_reply_to_status_id={}", CREATE_TWEET_URL, substituted, twete.id)); - } else { - println!("you can only thread your own tweets"); - // ask if it should .@ instead? + let maybe_id = TweetId::parse(id_str.to_owned()); + match maybe_id { + Ok(twid) => { + if let Some(twete) = tweeter.retrieve_tweet(&twid).map(|x| x.clone()) { // TODO: no clone when this stops taking &mut self + let handle = &tweeter.retrieve_user(&twete.author_id).unwrap().handle; + // TODO: definitely breaks if you change your handle right now + if handle == &tweeter.current_user.handle { + let substituted = ::url_encode(reply); + queryer.do_api_post(&format!("{}?status={}&in_reply_to_status_id={}", CREATE_TWEET_URL, substituted, twete.id)); + } else { + println!("you can only thread your own tweets"); + // ask if it should .@ instead? + } } - let substituted = ::url_encode(reply); - queryer.do_api_post(&format!("{}?status={}&in_reply_to_status_id={}", CREATE_TWEET_URL, substituted, twete.id)); + } + Err(e) => { + tweeter.display_info.status(format!("Invalid id: {}", e)); } } } else { @@ -91,36 +105,43 @@ fn rep(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { let reply = reply_bare.trim(); let id_str = text.trim(); if reply.len() > 0 { - if let Some(inner_twid) = u64::from_str(&id_str).ok() { - // TODO: probably should just have Tweet impl Copy or something - if let Some(twete) = tweeter.retrieve_tweet(&TweetId::Bare(inner_twid)).map(|x| x.clone()) { - // get handles to reply to... - let author_handle = tweeter.retrieve_user(&twete.author_id).unwrap().handle.to_owned(); - let mut ats: Vec<String> = twete.get_mentions(); //std::collections::HashSet::new(); - /* - for handle in twete.get_mentions() { - ats.insert(handle); - } - */ - ats.remove_item(&author_handle); - ats.insert(0, author_handle); - if let Some(rt_tweet) = twete.rt_tweet.and_then(|id| tweeter.retrieve_tweet(&TweetId::Twitter(id))).map(|x| x.clone()) { - let rt_author_handle = tweeter.retrieve_user(&rt_tweet.author_id).unwrap().handle.to_owned(); - ats.remove_item(&rt_author_handle); - ats.insert(1, rt_author_handle); - } - if let Some(qt_tweet) = twete.quoted_tweet_id.and_then(|id| tweeter.retrieve_tweet(&TweetId::Twitter(id))).map(|x| x.clone()) { - // let qt_author_handle = tweeter.retrieve_user(&qt_tweet.author_id).unwrap().handle.to_owned(); - // ats.remove_item(&qt_author_handle); - // ats.insert(1, qt_author_handle); + let maybe_id = TweetId::parse(id_str.to_owned()); + match maybe_id { + Ok(twid) => { + if let Some(twete) = tweeter.retrieve_tweet(&twid).map(|x| x.clone()) { // TODO: no clone when this stops taking &mut self + // get handles to reply to... + let author_handle = tweeter.retrieve_user(&twete.author_id).unwrap().handle.to_owned(); + let mut ats: Vec<String> = twete.get_mentions(); //std::collections::HashSet::new(); + /* + for handle in twete.get_mentions() { + ats.insert(handle); + } + */ + ats.remove_item(&author_handle); + ats.insert(0, author_handle); + if let Some(rt_tweet) = twete.rt_tweet.and_then(|id| tweeter.retrieve_tweet(&TweetId::Twitter(id))).map(|x| x.clone()) { + let rt_author_handle = tweeter.retrieve_user(&rt_tweet.author_id).unwrap().handle.to_owned(); + ats.remove_item(&rt_author_handle); + ats.insert(1, rt_author_handle); + } + if let Some(qt_tweet) = twete.quoted_tweet_id.and_then(|id| tweeter.retrieve_tweet(&TweetId::Twitter(id))).map(|x| x.clone()) { + // let qt_author_handle = tweeter.retrieve_user(&qt_tweet.author_id).unwrap().handle.to_owned(); + // ats.remove_item(&qt_author_handle); + // ats.insert(1, qt_author_handle); + } + //let ats_vec: Vec<&str> = ats.into_iter().collect(); + //let full_reply = format!("{} {}", ats_vec.join(" "), reply); + let decorated_ats: Vec<String> = ats.into_iter().map(|x| format!("@{}", x)).collect(); + let full_reply = format!("{} {}", decorated_ats.join(" "), reply); + let substituted = ::url_encode(&full_reply); + // println!("{}", (&format!("{}?status={}&in_reply_to_status_id={}", CREATE_TWEET_URL, substituted, twete.id))); + queryer.do_api_post(&format!("{}?status={}&in_reply_to_status_id={}", CREATE_TWEET_URL, substituted, twete.id)); + } else { + tweeter.display_info.status(format!("No tweet for id: {:?}", twid)); } - //let ats_vec: Vec<&str> = ats.into_iter().collect(); - //let full_reply = format!("{} {}", ats_vec.join(" "), reply); - let decorated_ats: Vec<String> = ats.into_iter().map(|x| format!("@{}", x)).collect(); - let full_reply = format!("{} {}", decorated_ats.join(" "), reply); - let substituted = ::url_encode(&full_reply); -// println!("{}", (&format!("{}?status={}&in_reply_to_status_id={}", CREATE_TWEET_URL, substituted, twete.id))); - queryer.do_api_post(&format!("{}?status={}&in_reply_to_status_id={}", CREATE_TWEET_URL, substituted, twete.id)); + }, + Err(e) => { + tweeter.display_info.status(format!("Cannot parse input: {:?}", id_str)); } } } else { @@ -144,24 +165,32 @@ fn quote(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { let reply = reply_bare.trim(); let id_str = text.trim(); if reply.len() > 0 { - if let Some(inner_twid) = u64::from_str(&id_str).ok() { - if let Some(twete) = tweeter.retrieve_tweet(&TweetId::Bare(inner_twid)).map(|x| x.clone()) { - let substituted = ::url_encode(reply); - let attachment_url = ::url_encode( - &format!( - "https://www.twitter.com/{}/status/{}", - tweeter.retrieve_user(&twete.author_id).unwrap().handle, - twete.id - ) - ); - println!("{}", substituted); - queryer.do_api_post( - &format!("{}?status={}&attachment_url={}", - CREATE_TWEET_URL, - substituted, - attachment_url - ) - ); + let maybe_id = TweetId::parse(id_str.to_owned()); + match maybe_id { + Ok(twid) => { + if let Some(twete) = tweeter.retrieve_tweet(&twid).map(|x| x.clone()) { // TODO: no clone when this stops taking &mut self + let substituted = ::url_encode(reply); + let attachment_url = ::url_encode( + &format!( + "https://www.twitter.com/{}/status/{}", + tweeter.retrieve_user(&twete.author_id).unwrap().handle, // TODO: for now this is ok ish, if we got the tweet we have the author + twete.id + ) + ); + println!("{}", substituted); + queryer.do_api_post( + &format!("{}?status={}&attachment_url={}", + CREATE_TWEET_URL, + substituted, + attachment_url + ) + ); + } else { + tweeter.display_info.status(format!("No tweet found for id {:?}", twid)); + } + }, + Err(e) => { + tweeter.display_info.status(format!("Invalid id: {:?}", id_str)); } } } else { @@ -179,8 +208,18 @@ pub static RETWETE: Command = Command { }; fn retwete(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { - let inner_twid = u64::from_str(&line).unwrap(); - let twete = tweeter.retrieve_tweet(&TweetId::Bare(inner_twid)).unwrap(); - queryer.do_api_post(&format!("{}/{}.json", RT_TWEET_URL, twete.id)); + match TweetId::parse(line.clone()) { + Ok(twid) => { + // TODO this really converts twid to a TweetId::Twitter + if let Some(twitter_id) = tweeter.retrieve_tweet(&twid).map(|x| x.id.to_owned()) { + queryer.do_api_post(&format!("{}/{}.json", RT_TWEET_URL, twitter_id)); + } else { + tweeter.display_info.status(format!("No tweet for id {:?}", twid)); + } + }, + Err(e) => { + tweeter.display_info.status(format!("Invalid id: {:?}", line)); + } + } } diff --git a/src/commands/view.rs b/src/commands/view.rs index e9b38ee..a6ce647 100644 --- a/src/commands/view.rs +++ b/src/commands/view.rs @@ -16,10 +16,18 @@ pub static VIEW: Command = Command { }; fn view(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) { - // TODO handle this unwrap - let inner_twid = u64::from_str(&line).unwrap(); - let twete = tweeter.retrieve_tweet(&TweetId::Bare(inner_twid)).unwrap().clone(); - tweeter.display_info.recv(display::Infos::Tweet(TweetId::Twitter(twete.id.to_owned()))); + match TweetId::parse(line) { + Ok(twid) => { + if let Some(twete) = tweeter.retrieve_tweet(&twid).map(|x| x.clone()) { + tweeter.display_info.recv(display::Infos::Tweet(TweetId::Twitter(twete.id.to_owned()))); + } else { + tweeter.display_info.status(format!("No tweet for id {:?}", twid)); + } + }, + Err(e) => { + tweeter.display_info.status(format!("Invalid id {:?}", e)); + } + } // display::render_twete(&twete.id, tweeter); // println!(" link: https://twitter.com/i/web/status/{}", twete.id); } @@ -32,14 +40,23 @@ pub static VIEW_THREAD: Command = Command { fn view_tr(line: String, mut tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { let mut thread: Vec<TweetId> = Vec::new(); - let inner_twid = u64::from_str(&line).unwrap(); - let curr_id = TweetId::Bare(inner_twid); - let mut maybe_next_id = tweeter.retrieve_tweet(&curr_id).and_then(|x| x.reply_to_tweet.to_owned()); - thread.push(curr_id); - while let Some(next_id) = maybe_next_id { - let curr_id = TweetId::Twitter(next_id); - maybe_next_id = tweeter.retrieve_tweet(&curr_id).and_then(|x| x.reply_to_tweet.to_owned()); - thread.push(curr_id); + let maybe_curr_id = TweetId::parse(line); + match maybe_curr_id { + Ok(curr_id) => { + let first_twete = tweeter.fetch_tweet(&curr_id, queryer).map(|x| x.to_owned()); + if first_twete.is_some() { + thread.push(curr_id); + } + let mut maybe_next_id = first_twete.and_then(|x| x.reply_to_tweet.to_owned()); + while let Some(next_id) = maybe_next_id { + let curr_id = TweetId::Twitter(next_id); + maybe_next_id = tweeter.fetch_tweet(&curr_id, queryer).and_then(|x| x.reply_to_tweet.to_owned()); + thread.push(curr_id); + } + }, + Err(e) => { + tweeter.display_info.status(format!("Invalid id {:?}", e)); + } } tweeter.display_info.recv(display::Infos::Thread(thread)); diff --git a/src/display/mod.rs b/src/display/mod.rs index c011294..4faaf5b 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -197,84 +197,85 @@ impl Render for tw::events::Event { pub fn render_twete(twete_id: &TweetId, tweeter: &mut tw::TwitterCache) -> Vec<String> { let mut result = Vec::new(); let id_color = color::Fg(color::Rgb(180, 80, 40)); - let maybe_twete = tweeter.retrieve_tweet(twete_id).map(|x| x.clone()); - if maybe_twete.is_none() { - result.push(format!("No such tweet: {:?}", twete_id)); - return result; - } - 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).map(|x| x.clone()).unwrap(); - match twete.rt_tweet { - Some(ref rt_id) => { - // same for a retweet - let rt = tweeter.retrieve_tweet(&TweetId::Twitter(rt_id.to_owned())).unwrap().clone(); - // and its author - let rt_author = tweeter.retrieve_user(&rt.author_id).unwrap().clone(); - result.push(format!("{} id:{} (rt_id:{}){}{}", - id_color, rt.internal_id, twete.internal_id, - rt.reply_to_tweet.clone() - .map(|id| tweeter.retrieve_tweet(&TweetId::Twitter(id.to_owned())) - .and_then(|tw| Some(format!(" reply_to:{}", tw.internal_id))) - .unwrap_or(format!(" reply_to:twitter::{}", id)) - ) - .unwrap_or("".to_string()), - color::Fg(color::Reset) - )); - result.push(format!(" {}{}{} ({}@{}{}) via {}{}{} ({}@{}{}) RT:", - color_for(&rt_author.handle), rt_author.name, color::Fg(color::Reset), - color_for(&rt_author.handle), rt_author.handle, color::Fg(color::Reset), - color_for(&user.handle), user.name, color::Fg(color::Reset), - color_for(&user.handle), user.handle, color::Fg(color::Reset) - )); - } - None => { - result.push(format!("{} id:{}{}{}", - id_color, twete.internal_id, - twete.reply_to_tweet.clone() - .map(|id| tweeter.retrieve_tweet(&TweetId::Twitter(id.to_owned())) - .and_then(|tw| Some(format!(" reply_to:{}", tw.internal_id))) - .unwrap_or(format!(" reply_to:twitter::{}", id)) - ) - .unwrap_or("".to_string()), - color::Fg(color::Reset) - )); - result.push(format!(" {}{}{} ({}@{}{})", - color_for(&user.handle), user.name, color::Fg(color::Reset), - color_for(&user.handle), user.handle, color::Fg(color::Reset) - )); - } - } + match tweeter.retrieve_tweet(twete_id).map(|x| x.clone()) { + Some(twete) => { + // if we got the tweet, the API gave us the user too + let user = tweeter.retrieve_user(&twete.author_id).map(|x| x.clone()).unwrap(); + match twete.rt_tweet { + Some(ref rt_id) => { + // same for a retweet + let rt = tweeter.retrieve_tweet(&TweetId::Twitter(rt_id.to_owned())).unwrap().clone(); + // and its author + let rt_author = tweeter.retrieve_user(&rt.author_id).unwrap().clone(); + result.push(format!("{} id:{} (rt_id:{}){}{}", + id_color, rt.internal_id, twete.internal_id, + rt.reply_to_tweet.clone() + .map(|id| tweeter.retrieve_tweet(&TweetId::Twitter(id.to_owned())) + .and_then(|tw| Some(format!(" reply_to:{}", tw.internal_id))) + .unwrap_or(format!(" reply_to:twitter::{}", id)) + ) + .unwrap_or("".to_string()), + color::Fg(color::Reset) + )); + result.push(format!(" {}{}{} ({}@{}{}) via {}{}{} ({}@{}{}) RT:", + color_for(&rt_author.handle), rt_author.name, color::Fg(color::Reset), + color_for(&rt_author.handle), rt_author.handle, color::Fg(color::Reset), + color_for(&user.handle), user.name, color::Fg(color::Reset), + color_for(&user.handle), user.handle, color::Fg(color::Reset) + )); + } + None => { + result.push(format!("{} id:{}{}{}", + id_color, twete.internal_id, + twete.reply_to_tweet.clone() + .map(|id| tweeter.retrieve_tweet(&TweetId::Twitter(id.to_owned())) + .and_then(|tw| Some(format!(" reply_to:{}", tw.internal_id))) + .unwrap_or(format!(" reply_to:twitter::{}", id)) + ) + .unwrap_or("".to_string()), + color::Fg(color::Reset) + )); + result.push(format!(" {}{}{} ({}@{}{})", + color_for(&user.handle), user.name, color::Fg(color::Reset), + color_for(&user.handle), user.handle, color::Fg(color::Reset) + )); + } + } - result.extend( - format!(" {}", twete.text.replace("\r", "\\r").split("\n").collect::<Vec<&str>>().join("\n ")).split("\n").map(|x| x.to_owned()) - ); + result.extend( + format!(" {}", twete.text.replace("\r", "\\r").split("\n").collect::<Vec<&str>>().join("\n ")).split("\n").map(|x| x.to_owned()) + ); - if let Some(ref qt_id) = twete.quoted_tweet_id { - let maybe_qt = tweeter.retrieve_tweet(&TweetId::Twitter(qt_id.to_owned())).map(|x| x.to_owned()); - if let Some(qt) = maybe_qt { - let qt_author = tweeter.retrieve_user(&qt.author_id).unwrap().clone(); - result.push(format!("{} id:{}{}{}", - id_color, qt.internal_id, - qt.reply_to_tweet.clone() - .map(|id| tweeter.retrieve_tweet(&TweetId::Twitter(id.to_owned())) - .and_then(|tw| Some(format!(" reply_to:{}", tw.internal_id))) - .unwrap_or(format!(" reply_to:twitter::{}", id)) - ) - .unwrap_or("".to_string()), - color::Fg(color::Reset) - )); - result.push(format!( - " {}{}{} ({}@{}{})", - color_for(&qt_author.handle), qt_author.name, color::Fg(color::Reset), - color_for(&qt_author.handle), qt_author.handle, color::Fg(color::Reset) - )); - result.push(format!( - " {}", - qt.text.replace("\r", "\\r").split("\n").collect::<Vec<&str>>().join("\n ") - )); - } else { - result.push(format!(" << don't have quoted tweet! >>")); + if let Some(ref qt_id) = twete.quoted_tweet_id { + let maybe_qt = tweeter.retrieve_tweet(&TweetId::Twitter(qt_id.to_owned())).map(|x| x.to_owned()); + if let Some(qt) = maybe_qt { + let qt_author = tweeter.retrieve_user(&qt.author_id).unwrap().clone(); + result.push(format!("{} id:{}{}{}", + id_color, qt.internal_id, + qt.reply_to_tweet.clone() + .map(|id| tweeter.retrieve_tweet(&TweetId::Twitter(id.to_owned())) + .and_then(|tw| Some(format!(" reply_to:{}", tw.internal_id))) + .unwrap_or(format!(" reply_to:twitter::{}", id)) + ) + .unwrap_or("".to_string()), + color::Fg(color::Reset) + )); + result.push(format!( + " {}{}{} ({}@{}{})", + color_for(&qt_author.handle), qt_author.name, color::Fg(color::Reset), + color_for(&qt_author.handle), qt_author.handle, color::Fg(color::Reset) + )); + result.push(format!( + " {}", + qt.text.replace("\r", "\\r").split("\n").collect::<Vec<&str>>().join("\n ") + )); + } else { + result.push(format!(" << don't have quoted tweet! >>")); + } + } + }, + None => { + result.push(format!("No such tweet: {:?}", twete_id)); } } diff --git a/src/tw/mod.rs b/src/tw/mod.rs index 0341ec9..5f40c5e 100644 --- a/src/tw/mod.rs +++ b/src/tw/mod.rs @@ -232,7 +232,7 @@ impl Default for DisplayInfo { } impl DisplayInfo { - fn status(&mut self, stat: String) { + pub fn status(&mut self, stat: String) { self.log.push(stat); } @@ -501,14 +501,30 @@ impl TwitterCache { pub fn retrieve_user(&self, user_id: &String) -> Option<&User> { self.users.get(user_id) } - pub fn fetch_tweet(&mut self, tweet_id: &String, mut queryer: &mut ::Queryer) -> Option<&Tweet> { - if !self.tweets.contains_key(tweet_id) { - match self.look_up_tweet(tweet_id, &mut queryer) { - Some(json) => self.cache_api_tweet(json), - None => self.display_info.status(format!("Unable to retrieve tweet {}", tweet_id)) - }; + pub fn fetch_tweet(&mut self, tweet_id: &TweetId, mut queryer: &mut ::Queryer) -> Option<&Tweet> { + match tweet_id { + &TweetId::Bare(ref id) => { + // we can do nothing but just try to get it + self.retrieve_tweet(tweet_id) + } + &TweetId::Today(ref id) => { + // we can do nothing but just try to get it + self.retrieve_tweet(tweet_id) + }, + &TweetId::Dated(ref date, ref id) => { + // we can do nothing but just try to get it + self.retrieve_tweet(tweet_id) + }, + &TweetId::Twitter(ref id) => { + if !self.tweets.contains_key(id) { + match self.look_up_tweet(id, &mut queryer) { + Some(json) => self.cache_api_tweet(json), + None => self.display_info.status(format!("Unable to retrieve tweet {}", id)) + }; + } + self.retrieve_tweet(tweet_id) + } } - self.tweets.get(tweet_id) } pub fn fetch_user(&mut self, user_id: &String, mut queryer: &mut ::Queryer) -> Option<&User> { if !self.users.contains_key(user_id) { |