From 6d46258d2dbe82609e479d0cfc2d77258457785e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?VERET=20Cl=C3=A9ment?= Date: Thu, 5 Mar 2020 15:02:43 +0100 Subject: [PATCH] Does proper mentions through the MentionEntity struct --- CHANGELOG | 4 ++++ Cargo.lock | 2 +- Cargo.toml | 2 +- src/lib.rs | 18 +++++++++++++++++- 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 CHANGELOG diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..ddd1a49 --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,4 @@ +# v0.1.8 + +* fix #1: mentions are treated like decoded urls (this is not really needed to push it this far but it would be easier in case you want to modify it) + diff --git a/Cargo.lock b/Cargo.lock index 743619e..b94e1ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1498,7 +1498,7 @@ dependencies = [ [[package]] name = "scootaloo" -version = "0.1.7" +version = "0.1.8" dependencies = [ "clap", "egg-mode", diff --git a/Cargo.toml b/Cargo.toml index 1a2f747..fe4b2e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "scootaloo" -version = "0.1.7" +version = "0.1.8" authors = ["VC "] edition = "2018" diff --git a/src/lib.rs b/src/lib.rs index c1c7874..f95b34d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ use serde::Deserialize; use egg_mode::{ Token, KeyPair, - entities::{UrlEntity, MediaEntity, MediaType}, + entities::{UrlEntity, MediaEntity, MentionEntity, MediaType}, tweet::{ Tweet, user_timeline, @@ -97,6 +97,16 @@ fn decode_urls(urls: &Vec) -> HashMap { decoded_urls } +fn twitter_mentions(ums: &Vec) -> HashMap { + let mut decoded_mentions = HashMap::new(); + + for um in ums { + decoded_mentions.insert(format!("@{}", um.screen_name), format!("@{}@twitter.com", um.screen_name)); + } + + decoded_mentions +} + /// Retrieve a single media from a tweet and store it in a temporary file fn get_tweet_media(m: &MediaEntity, t: &str) -> Result> { match m.media_type { @@ -147,6 +157,12 @@ fn build_basic_status(tweet: &Tweet) -> Result> { toot = toot.replace(&decoded_url.0, &decoded_url.1); } + let decoded_mentions = twitter_mentions(&tweet.entities.user_mentions); + + for decoded_mention in decoded_mentions { + toot = toot.replace(&decoded_mention.0, &decoded_mention.1); + } + if let Ok(t) = decode_html(&toot) { toot = t; }