From 87797c7ab09444b16dead365bf42bea26a6abf3c Mon Sep 17 00:00:00 2001 From: VC Date: Mon, 12 Dec 2022 13:38:52 +0100 Subject: [PATCH 1/3] feat: note inside copyprofile --- src/lib.rs | 11 ++++-- src/mastodon.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 101 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6992caf..459b901 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -263,6 +263,13 @@ pub async fn profile(config: Config, bot: Option) { .user .ok_or_else(|| ScootalooError::new("No user in Tweet!"))?; + let note = get_note_from_description( + &twitter_user.description, + &twitter_user.entities.description.urls, + ); + + let fields_attributes = get_attribute_from_url(&twitter_user.entities.url); + let display_name = Some(String::from_utf16_lossy( &twitter_user .name @@ -280,7 +287,7 @@ pub async fn profile(config: Config, bot: Option) { discoverable: None, bot, display_name, - note: twitter_user.description, + note, avatar: Some( base64_media(&twitter_user.profile_image_url_https.replace("_normal", "")) .await?, @@ -288,7 +295,7 @@ pub async fn profile(config: Config, bot: Option) { header, locked: None, source: None, - fields_attributes: None, + fields_attributes, }; let mastodon = get_mastodon_token(&mastodon_config); diff --git a/src/mastodon.rs b/src/mastodon.rs index 684e266..140be04 100644 --- a/src/mastodon.rs +++ b/src/mastodon.rs @@ -1,7 +1,14 @@ use crate::config::MastodonConfig; -use egg_mode::entities::{MentionEntity, UrlEntity}; -use megalodon::{generator, mastodon::Mastodon, megalodon::AppInputOptions}; +use egg_mode::{ + entities::{MentionEntity, UrlEntity}, + user::UserEntityDetail, +}; +use megalodon::{ + generator, + mastodon::Mastodon, + megalodon::{AppInputOptions, CredentialsFieldAttribute}, +}; use regex::Regex; use std::{collections::HashMap, io::stdin}; @@ -123,6 +130,32 @@ pub fn get_mastodon_token(masto: &MastodonConfig) -> Mastodon { Mastodon::new(masto.base.to_string(), Some(masto.token.to_string()), None) } +/// Gets note from twitter_user description +pub fn get_note_from_description(t: &Option, urls: &[UrlEntity]) -> Option { + t.as_ref().map(|d| { + let mut n = d.to_owned(); + let a_urls = associate_urls(urls, &None); + decode_urls(&mut n, &a_urls); + n + }) +} + +/// Gets fields_attribute from UserEntityDetail +pub fn get_attribute_from_url( + user_entity_detail: &Option, +) -> Option> { + user_entity_detail.as_ref().and_then(|u| { + u.urls.first().and_then(|v| { + v.expanded_url.as_ref().map(|e| { + vec![CredentialsFieldAttribute { + name: v.display_url.to_string(), + value: e.to_string(), + }] + }) + }) + }) +} + /// Generic register function /// As this function is supposed to be run only once, it will panic for every error it encounters /// Most of this function is a direct copy/paste of the official `elefren` crate @@ -207,6 +240,63 @@ token = "{}""#, mod tests { use super::*; + #[test] + fn test_get_attribute_from_url() { + let expected_credentials_field_attribute = CredentialsFieldAttribute { + name: "Nintendojo.fr".to_string(), + value: "https://www.nintendojo.fr".to_string(), + }; + + let true_urls = vec![UrlEntity { + display_url: "Nintendojo.fr".to_string(), + expanded_url: Some("https://www.nintendojo.fr".to_string()), + range: (1, 3), + url: "https://t.me/balek".to_string(), + }]; + + let false_urls = vec![UrlEntity { + display_url: "Nintendojo.fr".to_string(), + expanded_url: None, + range: (1, 3), + url: "https://t.me/balek".to_string(), + }]; + + assert!(get_attribute_from_url(&None).is_none()); + assert!(get_attribute_from_url(&Some(UserEntityDetail { urls: false_urls })).is_none()); + + let binding = get_attribute_from_url(&Some(UserEntityDetail { urls: true_urls })).unwrap(); + let result_credentials_field_attribute = binding.first().unwrap(); + + assert_eq!( + result_credentials_field_attribute.name, + expected_credentials_field_attribute.name + ); + assert_eq!( + result_credentials_field_attribute.value, + expected_credentials_field_attribute.value + ); + } + + #[test] + fn test_get_note_from_description() { + let urls = vec![UrlEntity { + display_url: "tamerelol".to_string(), + expanded_url: Some("https://www.nintendojo.fr/dojobar".to_string()), + range: (1, 3), + url: "https://t.me/tamerelol".to_string(), + }]; + + let some_description = Some("Youpi | https://t.me/tamerelol".to_string()); + let none_description = None; + + assert_eq!( + get_note_from_description(&some_description, &urls), + Some("Youpi | https://www.nintendojo.fr/dojobar".to_string()) + ); + + assert_eq!(get_note_from_description(&none_description, &urls), None); + } + #[test] fn test_replace_tweet_by_toot() { let mut associated_urls = HashMap::from([ From 88b73f4bc5f3f878382e970a0a6f1508eb2c3b81 Mon Sep 17 00:00:00 2001 From: VC Date: Mon, 12 Dec 2022 14:15:53 +0100 Subject: [PATCH 2/3] chore: bump version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 168ca83..6b27b40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1342,7 +1342,7 @@ dependencies = [ [[package]] name = "scootaloo" -version = "1.1.1" +version = "1.1.2" dependencies = [ "base64", "clap", diff --git a/Cargo.toml b/Cargo.toml index e6c7da1..6fe2f98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "scootaloo" -version = "1.1.1" +version = "1.1.2" authors = ["VC "] edition = "2021" From 90f47079d9a92fe1a01c21ae9e0a3bd8143b03af Mon Sep 17 00:00:00 2001 From: VC Date: Mon, 12 Dec 2022 15:15:12 +0100 Subject: [PATCH 3/3] fix: fields_attribute is busted so :shrug: --- src/lib.rs | 4 ++-- src/mastodon.rs | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 459b901..9f10a5b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -268,7 +268,7 @@ pub async fn profile(config: Config, bot: Option) { &twitter_user.entities.description.urls, ); - let fields_attributes = get_attribute_from_url(&twitter_user.entities.url); + // let fields_attributes = get_attribute_from_url(&twitter_user.entities.url); let display_name = Some(String::from_utf16_lossy( &twitter_user @@ -295,7 +295,7 @@ pub async fn profile(config: Config, bot: Option) { header, locked: None, source: None, - fields_attributes, + fields_attributes: None, }; let mastodon = get_mastodon_token(&mastodon_config); diff --git a/src/mastodon.rs b/src/mastodon.rs index 140be04..2f93c32 100644 --- a/src/mastodon.rs +++ b/src/mastodon.rs @@ -141,6 +141,7 @@ pub fn get_note_from_description(t: &Option, urls: &[UrlEntity]) -> Opti } /// Gets fields_attribute from UserEntityDetail +#[allow(dead_code)] pub fn get_attribute_from_url( user_entity_detail: &Option, ) -> Option> {