Merge branch 'feat_links' into 'master'

feat: fields_attributes and note inside copyprofile

See merge request veretcle/scootaloo!50
This commit is contained in:
VC
2022-12-12 14:35:42 +00:00
4 changed files with 103 additions and 5 deletions

2
Cargo.lock generated
View File

@@ -1342,7 +1342,7 @@ dependencies = [
[[package]] [[package]]
name = "scootaloo" name = "scootaloo"
version = "1.1.1" version = "1.1.2"
dependencies = [ dependencies = [
"base64", "base64",
"clap", "clap",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "scootaloo" name = "scootaloo"
version = "1.1.1" version = "1.1.2"
authors = ["VC <veretcle+framagit@mateu.be>"] authors = ["VC <veretcle+framagit@mateu.be>"]
edition = "2021" edition = "2021"

View File

@@ -263,6 +263,13 @@ pub async fn profile(config: Config, bot: Option<bool>) {
.user .user
.ok_or_else(|| ScootalooError::new("No user in Tweet!"))?; .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( let display_name = Some(String::from_utf16_lossy(
&twitter_user &twitter_user
.name .name
@@ -280,7 +287,7 @@ pub async fn profile(config: Config, bot: Option<bool>) {
discoverable: None, discoverable: None,
bot, bot,
display_name, display_name,
note: twitter_user.description, note,
avatar: Some( avatar: Some(
base64_media(&twitter_user.profile_image_url_https.replace("_normal", "")) base64_media(&twitter_user.profile_image_url_https.replace("_normal", ""))
.await?, .await?,

View File

@@ -1,7 +1,14 @@
use crate::config::MastodonConfig; use crate::config::MastodonConfig;
use egg_mode::entities::{MentionEntity, UrlEntity}; use egg_mode::{
use megalodon::{generator, mastodon::Mastodon, megalodon::AppInputOptions}; entities::{MentionEntity, UrlEntity},
user::UserEntityDetail,
};
use megalodon::{
generator,
mastodon::Mastodon,
megalodon::{AppInputOptions, CredentialsFieldAttribute},
};
use regex::Regex; use regex::Regex;
use std::{collections::HashMap, io::stdin}; use std::{collections::HashMap, io::stdin};
@@ -123,6 +130,33 @@ pub fn get_mastodon_token(masto: &MastodonConfig) -> Mastodon {
Mastodon::new(masto.base.to_string(), Some(masto.token.to_string()), None) 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<String>, urls: &[UrlEntity]) -> Option<String> {
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
#[allow(dead_code)]
pub fn get_attribute_from_url(
user_entity_detail: &Option<UserEntityDetail>,
) -> Option<Vec<CredentialsFieldAttribute>> {
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 /// Generic register function
/// As this function is supposed to be run only once, it will panic for every error it encounters /// 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 /// Most of this function is a direct copy/paste of the official `elefren` crate
@@ -207,6 +241,63 @@ token = "{}""#,
mod tests { mod tests {
use super::*; 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] #[test]
fn test_replace_tweet_by_toot() { fn test_replace_tweet_by_toot() {
let mut associated_urls = HashMap::from([ let mut associated_urls = HashMap::from([