refactor: build status text more progressively

This commit is contained in:
VC
2022-11-24 15:38:23 +01:00
parent 1132f41b9e
commit f42aa8cbb6
3 changed files with 222 additions and 203 deletions

View File

@@ -7,7 +7,9 @@ use config::Config;
mod mastodon;
pub use mastodon::register;
use mastodon::{build_basic_status, get_mastodon_token};
use mastodon::{
associate_urls, decode_urls, get_mastodon_token, replace_alt_services, twitter_mentions,
};
mod twitter;
use twitter::*;
@@ -21,10 +23,11 @@ use state::{read_state, write_state, TweetToToot};
use elefren::{prelude::*, status_builder::StatusBuilder, Language};
use futures::StreamExt;
use html_escape::decode_html_entities;
use log::info;
use regex::Regex;
use rusqlite::Connection;
use std::{collections::HashMap, sync::Arc};
use std::sync::Arc;
use tokio::{spawn, sync::Mutex};
const DEFAULT_RATE_LIMIT: usize = 4;
@@ -43,21 +46,7 @@ pub async fn run(config: Config) {
}),
));
let scootaloo_mentions: HashMap<String, String> = config
.mastodon
.values()
.filter(|s| s.mastodon_screen_name.is_some())
.map(|s| {
(
format!("@{}", s.twitter_screen_name),
format!(
"@{}@{}",
s.mastodon_screen_name.as_ref().unwrap(),
s.base.split('/').last().unwrap()
),
)
})
.collect();
let global_mastodon_config = Arc::new(Mutex::new(config.mastodon.clone()));
let display_url_re = config
.scootaloo
@@ -76,11 +65,11 @@ pub async fn run(config: Config) {
// create temporary value for each task
let scootaloo_cache_path = config.scootaloo.cache_path.clone();
let scootaloo_mentions = scootaloo_mentions.clone();
let scootaloo_alt_services = config.scootaloo.alternative_services_for.clone();
let display_url_re = display_url_re.clone();
let token = get_oauth2_token(&config.twitter);
let task_conn = conn.clone();
let global_mastodon_config = global_mastodon_config.clone();
spawn(async move {
info!("Starting treating {}", &mastodon_config.twitter_screen_name);
@@ -118,13 +107,32 @@ pub async fn run(config: Config) {
});
drop(lconn);
// build basic status by just yielding text and dereferencing contained urls
let mut status_text = build_basic_status(
tweet,
&scootaloo_mentions,
&display_url_re,
&scootaloo_alt_services,
);
// basic toot text
let mut status_text = tweet.text.clone();
// add mentions and smart mentions
if !&tweet.entities.user_mentions.is_empty() {
info!("Tweet contains mentions, add them!");
let global_mastodon_config = global_mastodon_config.lock().await;
twitter_mentions(
&mut status_text,
&tweet.entities.user_mentions,
&global_mastodon_config,
);
drop(global_mastodon_config);
}
if !&tweet.entities.urls.is_empty() {
info!("Tweet contains links, add them!");
let mut associated_urls =
associate_urls(&tweet.entities.urls, &display_url_re);
if let Some(a) = &scootaloo_alt_services {
replace_alt_services(&mut associated_urls, a);
}
decode_urls(&mut status_text, &associated_urls);
}
// building associative media list
let (media_url, status_medias) =
@@ -132,11 +140,15 @@ pub async fn run(config: Config) {
status_text = status_text.replace(&media_url, "");
// now that the text wont be altered anymore, we can safely remove HTML
// entities
status_text = decode_html_entities(&status_text).to_string();
info!("Building corresponding Mastodon status");
let mut status_builder = StatusBuilder::new();
status_builder.status(&status_text).media_ids(status_medias);
status_builder.status(status_text).media_ids(status_medias);
// theard if necessary
if let Some(i) = toot_reply_id {