From faa5864206ad8ab315ae12fb925386c6b76b1a90 Mon Sep 17 00:00:00 2001 From: Andy Wortman Date: Sat, 21 Oct 2017 14:01:56 -0700 Subject: begin conversion to typed TwitterID --- src/commands/fav.rs | 29 +++++++++++++++++++++++------ src/commands/look_up.rs | 1 + src/commands/thread.rs | 24 +++++++++++++++++------- src/commands/twete.rs | 16 +++++++++------- src/commands/view.rs | 6 ++++-- 5 files changed, 54 insertions(+), 22 deletions(-) (limited to 'src/commands') diff --git a/src/commands/fav.rs b/src/commands/fav.rs index 3e2b00d..7b9cce6 100644 --- a/src/commands/fav.rs +++ b/src/commands/fav.rs @@ -1,6 +1,8 @@ use tw; use ::Queryer; +use tw::TweetId; + use commands::Command; use std::str::FromStr; @@ -16,9 +18,17 @@ 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 twete = tweeter.tweet_by_innerid(inner_twid).unwrap(); - queryer.do_api_post(&format!("{}?id={}", UNFAV_TWEET_URL, twete.id)); +// let inner_twid = u64::from_str(&line).unwrap(); + let maybe_id = TweetId::parse(line.to_owned()); + match maybe_id { + Some(twid) => { + let twete = tweeter.retrieve_tweet(&twid).unwrap(); + queryer.do_api_post(&format!("{}?id={}", UNFAV_TWEET_URL, twete.id)); + } + None => { + println!("Invalid id: {}", line); + } + } } pub static FAV: Command = Command { @@ -29,7 +39,14 @@ pub static FAV: Command = Command { fn fav(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { // TODO handle this unwrap - let inner_twid = u64::from_str(&line).unwrap(); - let twete = tweeter.tweet_by_innerid(inner_twid).unwrap(); - queryer.do_api_post(&format!("{}?id={}", FAV_TWEET_URL, twete.id)); + let maybe_id = TweetId::parse(line.to_owned()); + match maybe_id { + Some(twid) => { + let twete = tweeter.retrieve_tweet(&twid).unwrap(); + queryer.do_api_post(&format!("{}?id={}", FAV_TWEET_URL, twete.id)); + } + None => { + println!("Invalid id: {}", line); + } + } } diff --git a/src/commands/look_up.rs b/src/commands/look_up.rs index d04f984..386fade 100644 --- a/src/commands/look_up.rs +++ b/src/commands/look_up.rs @@ -23,6 +23,7 @@ pub static LOOK_UP_TWEET: Command = Command { exec: look_up_tweet }; +// 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); diff --git a/src/commands/thread.rs b/src/commands/thread.rs index 92566fc..57b410c 100644 --- a/src/commands/thread.rs +++ b/src/commands/thread.rs @@ -2,6 +2,8 @@ use tw; use ::Queryer; use ::display; +use tw::TweetId; + use commands::Command; use std::str::FromStr; @@ -30,10 +32,15 @@ fn remember(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer 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); + let maybe_id = TweetId::parse(line.to_owned()); + match maybe_id { + Some(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); + } + None => { + println!("Invalid id: {}", line); } } } @@ -51,9 +58,12 @@ fn ls_threads(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Querye 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(); + if let Some(twete) = tweeter.retrieve_tweet(&TweetId::Bare(*latest_inner_id)) { // gross.. - display::render_twete(&twete.id, tweeter); - println!(""); + display::render_twete(&twete.id, tweeter); + println!(""); + } else { + println!("ERROR no tweet for remembered thread."); + } } } diff --git a/src/commands/twete.rs b/src/commands/twete.rs index f1f19cf..a66d5eb 100644 --- a/src/commands/twete.rs +++ b/src/commands/twete.rs @@ -1,6 +1,8 @@ use tw; use ::Queryer; +use tw::TweetId; + use commands::Command; use std::str::FromStr; @@ -17,7 +19,7 @@ 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.tweet_by_innerid(inner_twid).unwrap(); + let twete = tweeter.retrieve_tweet(&TweetId::Bare(inner_twid)).unwrap(); queryer.do_api_post(&format!("{}/{}.json", DEL_TWEET_URL, twete.id)); } @@ -54,7 +56,7 @@ fn thread(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { 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.tweet_by_innerid(inner_twid) { + if let Some(twete) = tweeter.retrieve_tweet(&TweetId::Bare(inner_twid)) { 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 { @@ -90,7 +92,7 @@ fn rep(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { 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.tweet_by_innerid(inner_twid) { + if let Some(twete) = tweeter.retrieve_tweet(&TweetId::Bare(inner_twid)) { // get handles to reply to... let author_handle = tweeter.retrieve_user(&twete.author_id).unwrap().handle.to_owned(); let mut ats: Vec = twete.get_mentions().into_iter().map(|x| x.to_owned()).collect(); //std::collections::HashSet::new(); @@ -102,12 +104,12 @@ fn rep(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { ats.remove_item(&author_handle); ats.insert(0, author_handle); // no idea why i have to .to_owned() here --v-- what about twete.rt_tweet is a move? - if let Some(rt_tweet) = twete.rt_tweet.to_owned().and_then(|id| tweeter.retrieve_tweet(&id)) { + if let Some(rt_tweet) = twete.rt_tweet.to_owned().and_then(|id| tweeter.retrieve_tweet(&TweetId::Twitter(id))) { 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.to_owned().and_then(|id| tweeter.retrieve_tweet(&id)) { + if let Some(qt_tweet) = twete.quoted_tweet_id.to_owned().and_then(|id| tweeter.retrieve_tweet(&TweetId::Twitter(id))) { // 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); @@ -143,7 +145,7 @@ fn quote(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { 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.tweet_by_innerid(inner_twid) { + if let Some(twete) = tweeter.retrieve_tweet(&TweetId::Bare(inner_twid)) { let substituted = ::url_encode(reply); let attachment_url = ::url_encode( &format!( @@ -178,7 +180,7 @@ 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.tweet_by_innerid(inner_twid).unwrap(); + let twete = tweeter.retrieve_tweet(&TweetId::Bare(inner_twid)).unwrap(); queryer.do_api_post(&format!("{}/{}.json", RT_TWEET_URL, twete.id)); } diff --git a/src/commands/view.rs b/src/commands/view.rs index b9cbf96..c14446a 100644 --- a/src/commands/view.rs +++ b/src/commands/view.rs @@ -1,6 +1,8 @@ use tw; use ::Queryer; +use tw::TweetId; + use commands::Command; use std::str::FromStr; @@ -16,7 +18,7 @@ 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.tweet_by_innerid(inner_twid).unwrap(); + let twete = tweeter.retrieve_tweet(&TweetId::Bare(inner_twid)).unwrap(); display::render_twete(&twete.id, tweeter); println!(" link: https://twitter.com/i/web/status/{}", twete.id); } @@ -34,7 +36,7 @@ fn view_tr(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) } fn view_tr_inner(id: u64, mut tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { - let twete: tw::tweet::Tweet = tweeter.tweet_by_innerid(id).unwrap().to_owned(); + let twete: tw::tweet::Tweet = tweeter.retrieve_tweet(&TweetId::Bare(id)).unwrap().to_owned(); if let Some(reply_id) = twete.reply_to_tweet.clone() { if let Some(reply_internal_id) = tweeter.fetch_tweet(&reply_id, queryer).map(|x| x.internal_id).map(|x| x.to_owned()) { view_tr_inner(reply_internal_id, tweeter, queryer); -- cgit v1.1