diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/dm.rs | 53 | ||||
-rw-r--r-- | src/commands/mod.rs | 2 | ||||
-rw-r--r-- | src/display/mod.rs | 19 | ||||
-rw-r--r-- | src/tw/mod.rs | 8 |
4 files changed, 75 insertions, 7 deletions
diff --git a/src/commands/dm.rs b/src/commands/dm.rs new file mode 100644 index 0000000..95f65b7 --- /dev/null +++ b/src/commands/dm.rs @@ -0,0 +1,53 @@ +use display::DisplayInfo; +use tw; +use ::Queryer; + +use tw::TweetId; + +use commands::Command; + +static DM_CREATE_URL: &str = "https://api.twitter.com/1.1/direct_messages/new.json"; + +pub static DM: Command = Command { + keyword: "dm", + params: 1, + exec: dm, + param_str: " <user_handle>", + help_str: "Send DM to <user_handle>" +}; + +fn dm(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 => { + display_info.status("To send a DM you must be an authenticated user.".to_owned()); + return; + } + }; + let mut text: String = line.trim().to_string(); + let text_bare = match text.find(" ") { + None => "".to_owned(), + Some(id_end_idx) => { + text.split_off(id_end_idx + 1) + } + }; + let dm_text = text_bare.trim(); + let handle_chars = text.trim().chars().collect::<Vec<char>>(); + let normalized_handle = if handle_chars[0] == '@' { + handle_chars[1..].to_vec() + } else { + handle_chars + }.into_iter().collect::<String>(); + + let encoded = ::url_encode(dm_text); + let result = match tweeter.current_profile() { + Some(user_profile) => { + queryer.do_api_post(&format!("{}?text={}&screen_name={}", DM_CREATE_URL, encoded, normalized_handle), &tweeter.app_key, &user_profile.creds) + }, + None => Err("No logged in user to DM as".to_owned()) + }; + match result { + Ok(_) => (), + Err(e) => display_info.status(e) + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index e8a725b..8108a78 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -10,6 +10,7 @@ pub struct Command { pub help_str: &'static str } +pub mod dm; pub mod help; pub mod auth; pub mod show_cache; @@ -23,6 +24,7 @@ pub mod thread; pub mod profile; pub static COMMANDS: &[&Command] = &[ + &dm::DM, &profile::PROFILE, &profile::PROFILES, &help::HELP, diff --git a/src/display/mod.rs b/src/display/mod.rs index 6115472..098bb1c 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -27,7 +27,7 @@ pub enum Infos { TweetWithContext(TweetId, String), Thread(Vec<TweetId>), Event(tw::events::Event), - DM(String), + DM(String, String, String), User(tw::user::User), Text(Vec<String>) } @@ -549,10 +549,8 @@ pub fn paint(tweeter: &::tw::TwitterCache, display_info: &mut DisplayInfo) -> Re }; wrapped.into_iter().rev().collect() }, - Infos::DM(msg) => { - let mut lines = vec![format!("{}{}{} DM:", cursor::Goto(1, height - h), clear::CurrentLine, "from")]; - lines.push(msg); - lines + Infos::DM(msg, from_id, to_id) => { + into_display_lines(render_dm(msg, from_id, to_id, tweeter, display_info, width), width).into_iter().rev().collect() } Infos::User(user) => { vec![ @@ -703,6 +701,17 @@ fn short_display_summary(u: &tw::user::User) -> String { ) } +pub fn render_dm(text: String, from_id: String, to_id: String, tweeter: &tw::TwitterCache, display_info: &mut DisplayInfo, width: u16) -> Vec<String> { + let from_user = tweeter.retrieve_user(&from_id).unwrap().clone(); + let to_user = tweeter.retrieve_user(&to_id).unwrap().clone(); + + let mut lines = pad_lines(into_display_lines(text.split("\n").map(|x| x.to_owned()).collect(), width - 2), " "); + let envelope = format!("DM: {} -> {}", short_display_summary(&from_user), short_display_summary(&to_user)); + + lines.insert(0, envelope); + lines +} + pub fn render_twete(twete_id: &TweetId, tweeter: &tw::TwitterCache, display_info: &mut DisplayInfo, width: Option<u16>) -> Vec<String> { let mut lines = render_twete_no_recurse(twete_id, tweeter, display_info, width); match tweeter.retrieve_tweet(twete_id).map(|x| x.clone()) { diff --git a/src/tw/mod.rs b/src/tw/mod.rs index 2eb574f..9a14b11 100644 --- a/src/tw/mod.rs +++ b/src/tw/mod.rs @@ -1200,8 +1200,12 @@ fn handle_twitter_dm( display_info: &mut DisplayInfo, _queryer: &mut ::Queryer) { // show DM - display_info.recv(display::Infos::Text(vec![format!("{:?}", structure)])); - display_info.recv(display::Infos::DM(structure["direct_message"]["text"].as_str().unwrap().to_string())); + tweeter.cache_api_user(structure["direct_message"]["recipient"].clone()); + tweeter.cache_api_user(structure["direct_message"]["sender"].clone()); + let dm_text = structure["direct_message"]["text"].as_str().unwrap().to_string(); + let to = structure["direct_message"]["recipient_id_str"].as_str().unwrap().to_string(); + let from = structure["direct_message"]["sender_id_str"].as_str().unwrap().to_string(); + display_info.recv(display::Infos::DM(dm_text, from, to)); } fn handle_twitter_welcome( |