From 5ecfb0b6204481e9ca4ed1af09ce22ff4591c1e3 Mon Sep 17 00:00:00 2001 From: iximeow Date: Mon, 15 Jan 2018 14:18:23 -0800 Subject: add flag to translate emoji to/from a description right now only handles :thinking: and :clap:, toggled by translate_emoji in profile.json --- src/commands/twete.rs | 53 ++++++++++++++++++++------------------------------- src/display/mod.rs | 13 ++++++++++++- src/tw/mod.rs | 3 +++ 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/commands/twete.rs b/src/commands/twete.rs index 0f3b6a5..8bcfdbd 100644 --- a/src/commands/twete.rs +++ b/src/commands/twete.rs @@ -1,4 +1,5 @@ use display::DisplayInfo; +use serde_json; use tw; use ::Queryer; @@ -61,15 +62,7 @@ fn twete(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, di } pub fn send_twete(text: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, display_info: &mut DisplayInfo) { - let result = match tweeter.current_profile() { - Some(user_profile) => queryer.do_api_post( - CREATE_TWEET_URL, - &vec![("status", &text)], - &tweeter.app_key, - &user_profile.creds - ), - None => Err("No logged in user to tweet as".to_owned()) - }; + let result = make_tweet(&text, vec![("status", &text)], queryer, tweeter); match result { Ok(_) => (), Err(e) => display_info.status(e) @@ -201,19 +194,26 @@ fn rep(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, disp } } +pub fn make_tweet(text: &str, params: Vec<(&str, &str)>, queryer: &mut Queryer, tweeter: &tw::TwitterCache) -> Result { + let updated_text = if tweeter.translate_emoji { + text + .replace(":thinking:", "🤔") + .replace(":clap:", "👏") + } else { + text.to_owned() + }; + let mut all_params = params; + all_params.push(("status", &updated_text)); + + match tweeter.current_profile() { + Some(user_profile) => queryer.do_api_post(CREATE_TWEET_URL, &all_params, &tweeter.app_key, &user_profile.creds), + None => Err("No logged in user to tweet as".to_owned()) + } +} + 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) { - let result = match tweeter.current_profile() { - Some(user_profile) => { - queryer.do_api_post( - CREATE_TWEET_URL, - &vec![("status", &text), ("in_reply_to_status_id", &twete.id)], - &tweeter.app_key, - &user_creds - ) - }, - None => Err("No logged in user to tweet as".to_owned()) - }; + let result = make_tweet(&text, vec![("in_reply_to_status_id", &twete.id)], queryer, tweeter); match result { Ok(_) => (), Err(e) => display_info.status(e) @@ -248,18 +248,7 @@ fn quote(line: String, tweeter: &mut tw::TwitterCache, queryer: &mut Queryer, di 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 ); - let result = match tweeter.current_profile() { - Some(user_profile) => { - queryer.do_api_post( - CREATE_TWEET_URL, - &vec![("status", reply), ("attachment_url", attachment_url)], - - &tweeter.app_key, - &user_profile.creds - ) - }, - None => Err("No logged in user to tweet as".to_owned()) - }; + let result = make_tweet(reply, vec![("attachment_url", attachment_url)], queryer, tweeter); match result { Ok(_) => (), Err(e) => display_info.status(e) diff --git a/src/display/mod.rs b/src/display/mod.rs index 098bb1c..f2f59da 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -830,8 +830,19 @@ pub fn render_twete_no_recurse(twete_id: &TweetId, tweeter: &tw::TwitterCache, d colorized_lines.push(new_line); } + let emoji_replaced: Vec = if tweeter.translate_emoji { + colorized_lines.iter().map(|line| { + line + .replace("🤔", ":thinking:") + .replace("👏",":clap:") + }).collect() + } else { + colorized_lines + }; + + let mut urls_to_include: Vec<&str> = vec![]; - let urls_replaced = colorized_lines + let urls_replaced = emoji_replaced .into_iter() .map(|line| { let mut result: String = line.to_owned(); diff --git a/src/tw/mod.rs b/src/tw/mod.rs index 63b8f07..4aa0365 100644 --- a/src/tw/mod.rs +++ b/src/tw/mod.rs @@ -136,6 +136,8 @@ pub struct TwitterCache { pub profiles: HashMap, mutes: MuteInfo, threads: HashMap, // thread : latest_tweet_in_thread + #[serde(default = "bool::default")] + pub translate_emoji: bool, #[serde(skip)] pub needs_save: bool, #[serde(skip)] @@ -668,6 +670,7 @@ impl TwitterCache { // have one channel up per twitter stream... curr_profile: None, profiles: HashMap::new(), + translate_emoji: false, needs_save: false, caching_permitted: true, threads: HashMap::new(), -- cgit v1.1