mirror of
https://framagit.org/veretcle/oolatoocs.git
synced 2025-07-21 13:24:18 +02:00
feat: add the ability to rewrite an edited toot
This commit is contained in:
70
src/lib.rs
70
src/lib.rs
@@ -5,20 +5,20 @@ mod config;
|
||||
pub use config::{parse_toml, Config};
|
||||
|
||||
mod state;
|
||||
pub use state::init_db;
|
||||
#[allow(unused_imports)]
|
||||
use state::{read_state, write_state, TweetToToot};
|
||||
use state::{delete_state, read_all_tweet_state, read_state, write_state, TweetToToot};
|
||||
pub use state::{init_db, migrate_db};
|
||||
|
||||
mod mastodon;
|
||||
use mastodon::get_mastodon_timeline_since;
|
||||
pub use mastodon::register;
|
||||
use mastodon::{get_mastodon_instance, get_mastodon_timeline_since, get_status_edited_at};
|
||||
|
||||
mod utils;
|
||||
use utils::{generate_multi_tweets, strip_everything};
|
||||
|
||||
mod twitter;
|
||||
#[allow(unused_imports)]
|
||||
use twitter::{generate_media_ids, post_tweet, transform_poll};
|
||||
use twitter::{delete_tweet, generate_media_ids, post_tweet, transform_poll};
|
||||
|
||||
use rusqlite::Connection;
|
||||
|
||||
@@ -27,11 +27,51 @@ pub async fn run(config: &Config) {
|
||||
let conn = Connection::open(&config.oolatoocs.db_path)
|
||||
.unwrap_or_else(|e| panic!("Cannot open DB: {}", e));
|
||||
|
||||
let last_toot_id = read_state(&conn, None)
|
||||
.unwrap_or_else(|e| panic!("Cannot get last toot id: {}", e))
|
||||
.map(|r| r.toot_id);
|
||||
let mastodon = get_mastodon_instance(&config.mastodon);
|
||||
|
||||
let timeline = get_mastodon_timeline_since(&config.mastodon, last_toot_id)
|
||||
let last_entry =
|
||||
read_state(&conn, None).unwrap_or_else(|e| panic!("Cannot get last toot id: {}", e));
|
||||
|
||||
let last_toot_id: Option<u64> = match last_entry {
|
||||
None => None, // Does not exist, this is the same as previously
|
||||
Some(t) => {
|
||||
match get_status_edited_at(&mastodon, t.toot_id).await {
|
||||
None => Some(t.toot_id),
|
||||
Some(d) => {
|
||||
// a date has been found
|
||||
if d > t.datetime.unwrap() {
|
||||
// said date is posterior to the previously
|
||||
// written tweet, we need to delete/rewrite
|
||||
for local_tweet_id in read_all_tweet_state(&conn, t.toot_id)
|
||||
.unwrap_or_else(|e| {
|
||||
panic!(
|
||||
"Cannot fetch all tweets associated with Toot ID {}: {}",
|
||||
t.toot_id, e
|
||||
)
|
||||
})
|
||||
.into_iter()
|
||||
{
|
||||
delete_tweet(&config.twitter, local_tweet_id)
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
panic!("Cannot delete Tweet ID ({}): {}", t.tweet_id, e)
|
||||
});
|
||||
}
|
||||
delete_state(&conn, t.toot_id).unwrap_or_else(|e| {
|
||||
panic!("Cannot delete Toot ID ({}): {}", t.toot_id, e)
|
||||
});
|
||||
read_state(&conn, None)
|
||||
.unwrap_or_else(|e| panic!("Cannot get last toot id: {}", e))
|
||||
.map(|a| a.toot_id)
|
||||
} else {
|
||||
Some(t.toot_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let timeline = get_mastodon_timeline_since(&mastodon, last_toot_id)
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("Cannot get instance: {}", e));
|
||||
|
||||
@@ -57,9 +97,22 @@ pub async fn run(config: &Config) {
|
||||
// if the toot is too long, we cut it in half here
|
||||
if let Some((first_half, second_half)) = generate_multi_tweets(&tweet_content) {
|
||||
tweet_content = second_half;
|
||||
// post the first half
|
||||
let reply_id = post_tweet(&config.twitter, first_half, vec![], reply_to, None)
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("Cannot post the first half of {}: {}", &toot.id, e));
|
||||
// write it to db
|
||||
write_state(
|
||||
&conn,
|
||||
TweetToToot {
|
||||
tweet_id: reply_id,
|
||||
toot_id: toot.id.parse::<u64>().unwrap(),
|
||||
datetime: None,
|
||||
},
|
||||
)
|
||||
.unwrap_or_else(|e| {
|
||||
panic!("Cannot store Toot/Tweet ({}/{}): {}", &toot.id, reply_id, e)
|
||||
});
|
||||
reply_to = Some(reply_id);
|
||||
};
|
||||
|
||||
@@ -80,6 +133,7 @@ pub async fn run(config: &Config) {
|
||||
TweetToToot {
|
||||
tweet_id,
|
||||
toot_id: toot.id.parse::<u64>().unwrap(),
|
||||
datetime: None,
|
||||
},
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("Cannot store Toot/Tweet ({}/{}): {}", &toot.id, tweet_id, e));
|
||||
|
Reference in New Issue
Block a user