From 983f073965024770f70ab29611f593c961499653 Mon Sep 17 00:00:00 2001 From: iximeow Date: Sat, 25 Nov 2017 18:39:04 -0800 Subject: extract DisplayInfo from TwitterCache what a mess... threading this through to any point where printing happens is upsetting. probably should be a global mutable behind accessors. --- src/commands/auth.rs | 23 +++++++------ src/commands/fav.rs | 21 ++++++------ src/commands/follow.rs | 11 +++--- src/commands/help.rs | 3 +- src/commands/look_up.rs | 19 ++++++----- src/commands/mod.rs | 3 +- src/commands/profile.rs | 9 ++--- src/commands/quit.rs | 3 +- src/commands/show_cache.rs | 17 +++++----- src/commands/thread.rs | 19 ++++++----- src/commands/twete.rs | 85 +++++++++++++++++++++++----------------------- src/commands/view.rs | 23 +++++++------ 12 files changed, 124 insertions(+), 112 deletions(-) (limited to 'src/commands') diff --git a/src/commands/auth.rs b/src/commands/auth.rs index db3a143..9b83c66 100644 --- a/src/commands/auth.rs +++ b/src/commands/auth.rs @@ -1,3 +1,4 @@ +use display::DisplayInfo; use tw; use std; use std::collections::HashMap; @@ -22,7 +23,7 @@ static OAUTH_REQUEST_TOKEN_URL: &str = "https://api.twitter.com/oauth/request_to static OAUTH_AUTHORIZE_URL: &str = "https://api.twitter.com/oauth/authorize"; static OAUTH_ACCESS_TOKEN_URL: &str = "https://api.twitter.com/oauth/access_token"; -fn auth(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn auth(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { // step 0: get an oauth token. // https://developer.twitter.com/en/docs/basics/authentication/api-reference/request_token with // callback set to oob so the user will later get a PIN. @@ -42,13 +43,13 @@ fn auth(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { key: as_map["oauth_token"].to_owned(), secret: as_map["oauth_token_secret"].to_owned() }); - tweeter.display_info.status(format!("Now enter `pin` with the code at {}?oauth_token={}", OAUTH_AUTHORIZE_URL, as_map["oauth_token"])); + display_info.status(format!("Now enter `pin` with the code at {}?oauth_token={}", OAUTH_AUTHORIZE_URL, as_map["oauth_token"])); } Err(_) => - tweeter.display_info.status("couldn't rebuild url".to_owned()) + display_info.status("couldn't rebuild url".to_owned()) }, Err(e) => - tweeter.display_info.status(format!("request token url error: {}", e)) + display_info.status(format!("request token url error: {}", e)) }; } @@ -60,9 +61,9 @@ pub static PIN: Command = Command { help_str: "Complete account auth. Enter PIN from prior `auth` link to connect an account." }; -fn pin(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn pin(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { if tweeter.WIP_auth.is_none() { - tweeter.display_info.status("Begin authorizing an account with `auth` first.".to_owned()); + display_info.status("Begin authorizing an account with `auth` first.".to_owned()); return; } @@ -102,20 +103,20 @@ fn pin(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { match queryer.do_api_get(::ACCOUNT_SETTINGS_URL, &tweeter.app_key, &user_credential) { Ok(settings) => { let user_handle = settings["screen_name"].as_str().unwrap().to_owned(); - tweeter.add_profile(tw::TwitterProfile::new(user_credential, tw::user::User::default()), Some(user_handle.clone())); + tweeter.add_profile(tw::TwitterProfile::new(user_credential, tw::user::User::default()), Some(user_handle.clone()), display_info); tweeter.WIP_auth = None; tweeter.state = tw::AppState::Reconnect(user_handle); - tweeter.display_info.status("Looks like you authed! Connecting...".to_owned()); + display_info.status("Looks like you authed! Connecting...".to_owned()); }, Err(_) => { - tweeter.display_info.status("Auth failed - couldn't find your handle.".to_owned()); + display_info.status("Auth failed - couldn't find your handle.".to_owned()); } }; }, Err(_) => - tweeter.display_info.status("couldn't rebuild url".to_owned()) + display_info.status("couldn't rebuild url".to_owned()) }, Err(e) => - tweeter.display_info.status(format!("request token url error: {}", e)) + display_info.status(format!("request token url error: {}", e)) }; } diff --git a/src/commands/fav.rs b/src/commands/fav.rs index e823618..0bee8ce 100644 --- a/src/commands/fav.rs +++ b/src/commands/fav.rs @@ -1,3 +1,4 @@ +use display::DisplayInfo; use tw; use ::Queryer; @@ -16,25 +17,25 @@ pub static UNFAV: Command = Command { help_str: "Unfavorite a tweet." }; -fn unfav(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn unfav(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { let maybe_id = TweetId::parse(line.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 + if let Some(twete) = tweeter.retrieve_tweet(&twid, display_info).map(|x| x.clone()) { // TODO: no clone when this stops taking &mut self let result = match tweeter.current_profile() { Some(user_profile) => queryer.do_api_post(&format!("{}?id={}", UNFAV_TWEET_URL, twete.id), &tweeter.app_key, &user_profile.creds), None => Err("No logged in user to unfav from".to_owned()) }; match result { Ok(_) => (), - Err(e) => tweeter.display_info.status(e) + Err(e) => display_info.status(e) } } else { - tweeter.display_info.status(format!("No tweet for id: {:?}", twid)); + display_info.status(format!("No tweet for id: {:?}", twid)); } } Err(e) => { - tweeter.display_info.status(format!("Invalid id: {}", e)); + display_info.status(format!("Invalid id: {}", e)); } } } @@ -47,26 +48,26 @@ pub static FAV: Command = Command { help_str: "Favorite a tweet." }; -fn fav(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn fav(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { let maybe_id = TweetId::parse(line.to_owned()); match maybe_id { Ok(twid) => { // 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 + if let Some(twete) = tweeter.retrieve_tweet(&twid, display_info).map(|x| x.clone()) { // TODO: no clone when this stops taking &mut self let result = match tweeter.current_profile() { Some(user_profile) => queryer.do_api_post(&format!("{}?id={}", FAV_TWEET_URL, twete.id), &tweeter.app_key, &user_profile.creds), None => Err("No logged in user to fav from".to_owned()) }; match result { Ok(_) => (), - Err(e) => tweeter.display_info.status(e) + Err(e) => display_info.status(e) } } else { - tweeter.display_info.status(format!("No tweet for id: {:?}", twid)); + display_info.status(format!("No tweet for id: {:?}", twid)); } } Err(e) => { - tweeter.display_info.status(format!("Invalid id: {}", e)); + display_info.status(format!("Invalid id: {}", e)); } } } diff --git a/src/commands/follow.rs b/src/commands/follow.rs index 6e29788..bc767d5 100644 --- a/src/commands/follow.rs +++ b/src/commands/follow.rs @@ -1,3 +1,4 @@ +use display::DisplayInfo; use tw; use ::Queryer; @@ -14,7 +15,7 @@ pub static UNFOLLOW: Command = Command { help_str: "Unfollow . No @ prefix in !" }; -fn unfl(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn unfl(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { let screen_name = line.trim(); let result = match tweeter.current_profile() { Some(user_profile) => { @@ -24,7 +25,7 @@ fn unfl(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { }; match result { Ok(_resp) => (), - Err(e) => tweeter.display_info.status(format!("unfl request error: {}", e)) + Err(e) => display_info.status(format!("unfl request error: {}", e)) } } @@ -36,11 +37,11 @@ pub static FOLLOW: Command = Command { help_str: "Follow . No @ prefix in !" }; -fn fl(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn fl(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { let screen_name = line.trim(); match tweeter.current_profile().map(|profile| profile.to_owned()) { Some(user_profile) => { - tweeter.display_info.status( + display_info.status( format!( "fl resp: {:?}", queryer.do_api_post( @@ -51,6 +52,6 @@ fn fl(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { ) ) }, - None => tweeter.display_info.status("No logged in user to follow from".to_owned()) + None => display_info.status("No logged in user to follow from".to_owned()) }; } diff --git a/src/commands/help.rs b/src/commands/help.rs index 445684b..550b677 100644 --- a/src/commands/help.rs +++ b/src/commands/help.rs @@ -1,3 +1,4 @@ +use display::DisplayInfo; use tw; use ::Queryer; @@ -11,6 +12,6 @@ pub static HELP: Command = Command { help_str: "This help prompt." }; -fn help(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn help(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { tweeter.state = tw::AppState::ShowHelp; } diff --git a/src/commands/look_up.rs b/src/commands/look_up.rs index 7bd5885..2674b34 100644 --- a/src/commands/look_up.rs +++ b/src/commands/look_up.rs @@ -1,3 +1,4 @@ +use display::DisplayInfo; use tw; use tw::TweetId; use display; @@ -13,12 +14,12 @@ pub static LOOK_UP_USER: Command = Command { help_str: "Look up the user by the specified twitter user ID, display name/handle." }; -fn look_up_user(line: String, tweeter: &mut tw::TwitterCache, mut queryer: &mut Queryer) { +fn look_up_user(line: String, tweeter: &mut tw::TwitterCache, mut queryer: &mut Queryer, display_info: &mut DisplayInfo) { // should probably just pass the id? - if let Some(user) = tweeter.fetch_user(&line, &mut queryer).map(|x| x.clone()) { - tweeter.display_info.recv(display::Infos::User(user)); + if let Some(user) = tweeter.fetch_user(&line, &mut queryer, display_info).map(|x| x.clone()) { + display_info.recv(display::Infos::User(user)); } else { - tweeter.display_info.status(format!("Couldn't retrieve {}", line)); + display_info.status(format!("Couldn't retrieve {}", line)); } } @@ -31,17 +32,17 @@ 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) { +fn look_up_tweet(line: String, tweeter: &mut tw::TwitterCache, mut queryer: &mut Queryer, display_info: &mut DisplayInfo) { 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)); + if let Some(tweet) = tweeter.fetch_tweet(&twid, &mut queryer, display_info).map(|x| x.clone()) { + display_info.recv(display::Infos::Tweet(twid)); } else { - tweeter.display_info.status(format!("Couldn't retrieve {:?}", twid)); + display_info.status(format!("Couldn't retrieve {:?}", twid)); } }, Err(e) => { - tweeter.display_info.status(format!("Invalid id {:?}", e)); + display_info.status(format!("Invalid id {:?}", e)); } } } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 262a748..e8a725b 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,10 +1,11 @@ +use display::DisplayInfo; use tw; use ::Queryer; pub struct Command { pub keyword: &'static str, pub params: u8, - pub exec: fn(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer), + pub exec: fn(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo), pub param_str: &'static str, pub help_str: &'static str } diff --git a/src/commands/profile.rs b/src/commands/profile.rs index c0d5d79..e20859b 100644 --- a/src/commands/profile.rs +++ b/src/commands/profile.rs @@ -1,3 +1,4 @@ +use display::DisplayInfo; use tw; use std; use std::collections::HashMap; @@ -14,12 +15,12 @@ pub static PROFILE: Command = Command { help_str: "Switch to profile " }; -fn switch_profile(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn switch_profile(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { let profile_name = line.trim(); if tweeter.profiles.contains_key(profile_name) { tweeter.curr_profile = Some(profile_name.to_owned()); } else { - tweeter.display_info.status(format!("No profile named {}", profile_name)) + display_info.status(format!("No profile named {}", profile_name)) }; } @@ -31,8 +32,8 @@ pub static PROFILES: Command = Command { help_str: "List all profiles" }; -fn list_profiles(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { - tweeter.display_info.recv(::display::Infos::Text( +fn list_profiles(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { + display_info.recv(::display::Infos::Text( tweeter.profiles.keys().map(|key| key.to_owned()).collect() )); } diff --git a/src/commands/quit.rs b/src/commands/quit.rs index 0f5c582..45390a3 100644 --- a/src/commands/quit.rs +++ b/src/commands/quit.rs @@ -1,3 +1,4 @@ +use display::DisplayInfo; use tw; use ::Queryer; @@ -12,6 +13,6 @@ pub static QUIT: Command = Command { help_str: "Gracefully exit this thing" }; -fn quit(_line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) { +fn quit(_line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer, display_info: &mut DisplayInfo) { tweeter.state = tw::AppState::Shutdown; } diff --git a/src/commands/show_cache.rs b/src/commands/show_cache.rs index 6dda8dc..2b810d7 100644 --- a/src/commands/show_cache.rs +++ b/src/commands/show_cache.rs @@ -1,3 +1,4 @@ +use display::DisplayInfo; use tw; use ::Queryer; @@ -11,24 +12,24 @@ pub static SHOW_CACHE: Command = Command { help_str: "Dump all cached info. Probably a bad idea." }; -fn show_cache(_line: String, tweeter: &mut tw::TwitterCache, mut queryer: &mut Queryer) { +fn show_cache(_line: String, tweeter: &mut tw::TwitterCache, mut queryer: &mut Queryer, display_info: &mut DisplayInfo) { /* - tweeter.display_info.status("----* USERS *----".to_owned()); + display_info.status("----* USERS *----".to_owned()); for (uid, user) in &tweeter.users { - tweeter.display_info.status(format!("User: {} -> {:?}", uid, user)); + display_info.status(format!("User: {} -> {:?}", uid, user)); } - tweeter.display_info.status("----* TWEETS *----".to_owned()); + display_info.status("----* TWEETS *----".to_owned()); for (tid, tweet) in &tweeter.tweets { - tweeter.display_info.status(format!("Tweet: {} -> {:?}", tid, tweet)); + display_info.status(format!("Tweet: {} -> {:?}", tid, tweet)); } - tweeter.display_info.status("----* FOLLOWERS *----".to_owned()); + display_info.status("----* FOLLOWERS *----".to_owned()); for uid in &tweeter.followers.clone() { let user_res = tweeter.fetch_user(uid, &mut queryer).map(|x| x.clone()); match user_res { Some(user) => { - tweeter.display_info.status(format!("Follower: {} - {:?}", uid, user)); + display_info.status(format!("Follower: {} - {:?}", uid, user)); } - None => { tweeter.display_info.status(" ...".to_owned()); } + None => { display_info.status(" ...".to_owned()); } } } */ diff --git a/src/commands/thread.rs b/src/commands/thread.rs index 71d0e52..302e641 100644 --- a/src/commands/thread.rs +++ b/src/commands/thread.rs @@ -1,3 +1,4 @@ +use display::DisplayInfo; use tw; use ::Queryer; use ::display; @@ -14,9 +15,9 @@ pub static FORGET_THREAD: Command = Command { help_str: "Discard thread known by . Entirely local to the client." }; -fn forget(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) { +fn forget(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer, display_info: &mut DisplayInfo) { tweeter.forget_thread(line.trim().to_string()); - tweeter.display_info.status(format!("Ok! Forgot thread {}", line.trim().to_string())); + display_info.status(format!("Ok! Forgot thread {}", line.trim().to_string())); } pub static REMEMBER_THREAD: Command = Command { @@ -27,7 +28,7 @@ pub static REMEMBER_THREAD: Command = Command { help_str: "Remember the thread tipped by as . Entirely local to the client." }; -fn remember(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) { +fn remember(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer, display_info: &mut DisplayInfo) { 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); @@ -37,15 +38,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) => { - if let Some(twete) = tweeter.retrieve_tweet(&twid).map(|x| x.clone()) { + if let Some(twete) = tweeter.retrieve_tweet(&twid, display_info).map(|x| x.clone()) { tweeter.set_thread(name.to_string(), twete.internal_id); - tweeter.display_info.status(format!("Ok! Recorded {:?} as thread {}", twid, name)); + display_info.status(format!("Ok! Recorded {:?} as thread {}", twid, name)); } else { - tweeter.display_info.status(format!("No tweet for id: {:?}", twid)); + display_info.status(format!("No tweet for id: {:?}", twid)); } } Err(e) => { - tweeter.display_info.status(format!("Invalid id: {}", e)); + display_info.status(format!("Invalid id: {}", e)); } } } @@ -60,10 +61,10 @@ pub static LIST_THREADS: Command = Command { help_str: "Show all known (local) threads" }; -fn ls_threads(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn ls_threads(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { let threads: Vec = tweeter.threads().collect::>().into_iter().map(|x| x.to_owned()).collect::>(); for k in threads { let latest_inner_id = tweeter.latest_in_thread(k.to_owned()).unwrap().to_owned(); - tweeter.display_info.recv(display::Infos::TweetWithContext(TweetId::Bare(latest_inner_id), format!("Thread: {}", k))) + display_info.recv(display::Infos::TweetWithContext(TweetId::Bare(latest_inner_id), format!("Thread: {}", k))) } } diff --git a/src/commands/twete.rs b/src/commands/twete.rs index 0d82b76..ac4bbeb 100644 --- a/src/commands/twete.rs +++ b/src/commands/twete.rs @@ -1,3 +1,4 @@ +use display::DisplayInfo; use tw; use ::Queryer; @@ -17,25 +18,25 @@ pub static DEL: Command = Command { help_str: "Delete tweet " }; -fn del(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn del(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { 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()) { + if let Some(twitter_id) = tweeter.retrieve_tweet(&twid, display_info).map(|x| x.id.to_owned()) { let result = match tweeter.current_profile() { Some(user_profile) => queryer.do_api_post(&format!("{}/{}.json", DEL_TWEET_URL, twitter_id), &tweeter.app_key, &user_profile.creds), None => Err("No logged in user to delete as".to_owned()) }; match result { Ok(_) => (), - Err(e) => tweeter.display_info.status(e) + Err(e) => display_info.status(e) } } else { - tweeter.display_info.status(format!("No tweet for id {:?}", twid)); + display_info.status(format!("No tweet for id {:?}", twid)); } }, Err(e) => { - tweeter.display_info.status(format!("Invalid id: {:?} ({})", line, e)); + display_info.status(format!("Invalid id: {:?} ({})", line, e)); } } } @@ -48,18 +49,18 @@ pub static TWETE: Command = Command { help_str: "Enter tweet compose mode." }; -fn twete(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn twete(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { // if there's text, send it. // if it's just "t", enter compose mode. let text = line.trim().to_owned(); if text.len() == 0 { - tweeter.display_info.mode = Some(::display::DisplayMode::Compose(text)); + display_info.mode = Some(::display::DisplayMode::Compose(text)); } else { - send_twete(text, tweeter, queryer); + send_twete(text, tweeter, queryer, display_info); } } -pub fn send_twete(text: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +pub fn send_twete(text: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { let substituted = ::url_encode(&text); let result = match tweeter.current_profile() { Some(user_profile) => queryer.do_api_post(&format!("{}?status={}", CREATE_TWEET_URL, substituted), &tweeter.app_key, &user_profile.creds), @@ -67,7 +68,7 @@ pub fn send_twete(text: String, tweeter: &mut tw::TwitterCache, queryer: &mut Qu }; match result { Ok(_) => (), - Err(e) => tweeter.display_info.status(e) + Err(e) => display_info.status(e) } } @@ -82,11 +83,11 @@ pub static THREAD: Command = Command { // the difference between threading and replying is not including // yourself in th @'s. -fn thread(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn thread(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { let user_profile = match tweeter.current_profile().map(|profile| profile.to_owned()) { Some(profile) => profile, None => { - tweeter.display_info.status("To reply you must be authenticated as a user.".to_owned()); + display_info.status("To reply you must be authenticated as a user.".to_owned()); return; } }; @@ -99,26 +100,26 @@ fn thread(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { 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 + if let Some(twete) = tweeter.retrieve_tweet(&twid, display_info).map(|x| x.clone()) { // TODO: no clone when this stops taking &mut self let handle = &tweeter.retrieve_user(&twete.author_id).unwrap().handle.to_owned(); // TODO: definitely breaks if you change your handle right now if handle == &user_profile.user.handle { - send_reply(reply.to_owned(), twid, tweeter, queryer, user_profile.creds); + send_reply(reply.to_owned(), twid, tweeter, queryer, user_profile.creds, display_info); } else { - tweeter.display_info.status("you can only thread your own tweets".to_owned()); + display_info.status("you can only thread your own tweets".to_owned()); // ask if it should .@ instead? } } } Err(e) => { - tweeter.display_info.status(format!("Invalid id: {}", e)); + display_info.status(format!("Invalid id: {}", e)); } } } else { - tweeter.display_info.status("thread your sik reply".to_owned()); + display_info.status("thread your sik reply".to_owned()); } } else { - tweeter.display_info.status("thread your sik reply".to_owned()); + display_info.status("thread your sik reply".to_owned()); } } @@ -131,11 +132,11 @@ pub static REP: Command = Command { help_str: "Enter compose mode to reply to " }; -fn rep(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn rep(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { let user_profile = match tweeter.current_profile().map(|profile| profile.to_owned()) { Some(profile) => profile, None => { - tweeter.display_info.status("To reply you must be authenticated as a user.".to_owned()); + display_info.status("To reply you must be authenticated as a user.".to_owned()); return; } }; @@ -151,13 +152,13 @@ fn rep(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { 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 + if let Some(twete) = tweeter.retrieve_tweet(&twid, display_info).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 = twete.get_mentions(); //std::collections::HashSet::new(); 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()) { + if let Some(rt_tweet) = twete.rt_tweet.and_then(|id| tweeter.retrieve_tweet(&TweetId::Twitter(id), display_info)).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); @@ -175,22 +176,22 @@ fn rep(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { let full_reply = format!("{} {}", decorated_ats.join(" "), reply); if reply.len() > 0 { - send_reply(full_reply, twid, tweeter, queryer, user_profile.creds); + send_reply(full_reply, twid, tweeter, queryer, user_profile.creds, display_info); } else { - tweeter.display_info.mode = Some(::display::DisplayMode::Reply(twid, full_reply)); + display_info.mode = Some(::display::DisplayMode::Reply(twid, full_reply)); } } else { - tweeter.display_info.status(format!("No tweet for id: {:?}", twid)); + display_info.status(format!("No tweet for id: {:?}", twid)); } }, Err(e) => { - tweeter.display_info.status(format!("Cannot parse input: {:?} ({})", id_str, e)); + display_info.status(format!("Cannot parse input: {:?} ({})", id_str, e)); } } } -pub fn send_reply(text: String, twid: TweetId, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, user_creds: tw::Credential) { - if let Some(twete) = tweeter.retrieve_tweet(&twid).map(|x| x.clone()) { // TODO: no clone when this stops taking &mut self +pub fn send_reply(text: String, twid: TweetId, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, user_creds: tw::Credential, display_info: &mut DisplayInfo) { + if let Some(twete) = tweeter.retrieve_tweet(&twid, display_info).map(|x| x.clone()) { // TODO: no clone when this stops taking &mut self let substituted = ::url_encode(&text); let result = match tweeter.current_profile() { Some(user_profile) => { @@ -200,10 +201,10 @@ pub fn send_reply(text: String, twid: TweetId, tweeter: &mut tw::TwitterCache, q }; match result { Ok(_) => (), - Err(e) => tweeter.display_info.status(e) + Err(e) => display_info.status(e) } } else { - tweeter.display_info.status(format!("Tweet stopped existing: {:?}", twid)); + display_info.status(format!("Tweet stopped existing: {:?}", twid)); } } @@ -215,7 +216,7 @@ pub static QUOTE: Command = Command { help_str: "Quote with context " }; -fn quote(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn quote(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { let mut text: String = line.trim().to_string(); if let Some(id_end_idx) = text.find(" ") { let reply_bare = text.split_off(id_end_idx + 1); @@ -225,7 +226,7 @@ fn quote(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { 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 + if let Some(twete) = tweeter.retrieve_tweet(&twid, display_info).map(|x| x.clone()) { // TODO: no clone when this stops taking &mut self let substituted = ::url_encode(reply); let attachment_url = ::url_encode( &format!( @@ -250,21 +251,21 @@ fn quote(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { }; match result { Ok(_) => (), - Err(e) => tweeter.display_info.status(e) + Err(e) => display_info.status(e) } } else { - tweeter.display_info.status(format!("No tweet found for id {:?}", twid)); + display_info.status(format!("No tweet found for id {:?}", twid)); } }, Err(e) => { - tweeter.display_info.status(format!("Invalid id: {:?}", id_str)); + display_info.status(format!("Invalid id: {:?}", id_str)); } } } else { - tweeter.display_info.status("rep your sik reply".to_owned()); + display_info.status("rep your sik reply".to_owned()); } } else { - tweeter.display_info.status("rep your sik reply".to_owned()); + display_info.status("rep your sik reply".to_owned()); } } @@ -276,11 +277,11 @@ pub static RETWETE: Command = Command { help_str: "Retweet " }; -fn retwete(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn retwete(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { 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()) { + if let Some(twitter_id) = tweeter.retrieve_tweet(&twid, display_info).map(|x| x.id.to_owned()) { let result = match tweeter.current_profile() { Some(user_profile) => { queryer.do_api_post(&format!("{}/{}.json", RT_TWEET_URL, twitter_id), &tweeter.app_key, &user_profile.creds) @@ -289,14 +290,14 @@ fn retwete(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) }; match result { Ok(_) => (), - Err(e) => tweeter.display_info.status(e) + Err(e) => display_info.status(e) } } else { - tweeter.display_info.status(format!("No tweet for id {:?}", twid)); + display_info.status(format!("No tweet for id {:?}", twid)); } }, Err(e) => { - tweeter.display_info.status(format!("Invalid id: {:?}", line)); + display_info.status(format!("Invalid id: {:?}", line)); } } } diff --git a/src/commands/view.rs b/src/commands/view.rs index 15c94b1..41dd9b3 100644 --- a/src/commands/view.rs +++ b/src/commands/view.rs @@ -1,3 +1,4 @@ +use display::DisplayInfo; use tw; use ::Queryer; @@ -15,20 +16,20 @@ pub static VIEW: Command = Command { help_str: "Display tweet with a reference URL" }; -fn view(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) { +fn view(line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer, display_info: &mut DisplayInfo) { match TweetId::parse(line) { Ok(twid) => { - if let Some(twete) = tweeter.retrieve_tweet(&twid).map(|x| x.clone()) { - tweeter.display_info.recv(display::Infos::TweetWithContext( + if let Some(twete) = tweeter.retrieve_tweet(&twid, display_info) { + display_info.recv(display::Infos::TweetWithContext( TweetId::Twitter(twete.id.to_owned()), format!("link: https://twitter.com/i/web/status/{}", twete.id) )); } else { - tweeter.display_info.status(format!("No tweet for id {:?}", twid)); + display_info.status(format!("No tweet for id {:?}", twid)); } }, Err(e) => { - tweeter.display_info.status(format!("Invalid id {:?}", e)); + display_info.status(format!("Invalid id {:?}", e)); } } } @@ -41,28 +42,28 @@ pub static VIEW_THREAD: Command = Command { help_str: "Display whole thread leading to , reference URLs for each" }; -fn view_tr(line: String, mut tweeter: &mut tw::TwitterCache, queryer: &mut Queryer) { +fn view_tr(line: String, mut tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { let mut thread: Vec = Vec::new(); 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()); + let first_twete = tweeter.fetch_tweet(&curr_id, queryer, display_info).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()); + maybe_next_id = tweeter.fetch_tweet(&curr_id, queryer, display_info).and_then(|x| x.reply_to_tweet.to_owned()); thread.push(curr_id); } }, Err(e) => { - tweeter.display_info.status(format!("Invalid id {:?}", e)); + display_info.status(format!("Invalid id {:?}", e)); } } - tweeter.display_info.recv(display::Infos::Thread(thread)); + display_info.recv(display::Infos::Thread(thread)); } pub static VIEW_THREAD_FORWARD: Command = Command { @@ -73,7 +74,7 @@ pub static VIEW_THREAD_FORWARD: Command = Command { help_str: "help me make this work" }; -fn view_tr_forward(_line: String, _tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) { +fn view_tr_forward(_line: String, _tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer, display_info: &mut DisplayInfo) { // first see if we have a thread for the tweet named // if we do not, we'll have to mimic a request like // curl 'https://twitter.com/jojonila/status/914383908090691584' \ -- cgit v1.1