feature: state is held into a sqlite db

This commit is contained in:
VC
2022-04-23 10:01:00 +02:00
parent 6363c12460
commit 48b8eaaa5b
4 changed files with 120 additions and 88 deletions

View File

@@ -16,7 +16,7 @@ use twitter::*;
mod util;
mod state;
use state::{read_state, write_state};
use state::{read_state, write_state, TweetToToot};
pub use state::init_db;
// std
@@ -34,11 +34,19 @@ use elefren::{
// log
use log::{info, warn, error, debug};
// rusqlite
use rusqlite::Connection;
/// This is where the magic happens
#[tokio::main]
pub async fn run(config: Config) {
// open the SQLite connection
let conn = Connection::open(&config.scootaloo.db_path).unwrap();
// retrieve the last tweet ID for the username
let last_tweet_id = read_state(&config.scootaloo.db_path);
let last_tweet_id = match read_state(&conn, None).unwrap() {
Some(i) => Some(i.tweet_id),
None => None,
};
// get OAuth2 token
let token = get_oauth2_token(&config.twitter);
@@ -125,12 +133,17 @@ pub async fn run(config: Config) {
// publish status
// again unwrap is safe here as we are in the main thread
mastodon.new_status(status).unwrap();
let published_status = mastodon.new_status(status).unwrap();
// this will panic if it cannot publish the status, which is a good thing, it allows the
// last_tweet gathered not to be written
let ttt_towrite = TweetToToot {
tweet_id: tweet.id,
toot_id: published_status.id,
};
// write the current state (tweet ID) to avoid copying it another time
write_state(&config.scootaloo.db_path, tweet.id).unwrap_or_else(|e|
write_state(&conn, ttt_towrite).unwrap_or_else(|e|
panic!("Cant write the last tweet retrieved: {}", e)
);
}