diff --git a/Cargo.lock b/Cargo.lock index bf64425..23c5078 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -259,6 +259,15 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" +[[package]] +name = "clokwerk" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd108d365fcb6d7eddf17a6718eb6a33db18ba4178f8cc6b667f480710f10d76" +dependencies = [ + "chrono", +] + [[package]] name = "color-eyre" version = "0.6.3" @@ -937,6 +946,7 @@ dependencies = [ "base64", "chrono", "clap", + "clokwerk", "color-eyre", "copy_dir", "futures", diff --git a/Cargo.toml b/Cargo.toml index 603d925..51f8228 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ askama = "0.12.1" base64 = "0.22.1" chrono = "0.4.38" clap = { version = "4.5.17", features = ["derive"] } +clokwerk = "0.4.0" color-eyre = "0.6.3" copy_dir = "0.1.3" futures = "0.3.30" diff --git a/src/main.rs b/src/main.rs index dcb088d..020bb0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,15 @@ -use copy_dir::copy_dir; +use std::borrow::BorrowMut; use std::fs::{create_dir_all, read_to_string, remove_file, write, File, OpenOptions}; use std::io::{BufRead, BufReader, Write}; use std::path::{Path, PathBuf}; use std::process::Stdio; +use std::sync::Arc; +use std::time::Duration; + +mod jsonrpc; +mod recsend; +use crate::jsonrpc::RpcClient; +use crate::recsend::{Receiver, Sender}; use askama::Template; use base64::prelude::BASE64_STANDARD; @@ -10,19 +17,17 @@ use base64::Engine; use chrono::offset::FixedOffset; use chrono::{DateTime, Days, Local, Utc}; use clap::{command, Parser}; +use clokwerk::{AsyncScheduler, Job, TimeUnits}; use color_eyre::eyre::{eyre, OptionExt, Result}; +use copy_dir::copy_dir; use futures::future::join_all; use jsonrpsee::async_client::{Client, ClientBuilder}; use serde_json::Value; use tempfile::tempdir; use tokio::process::Command; +use tokio::sync::Mutex; use tokio_util::codec::{FramedRead, LinesCodec}; -use crate::jsonrpc::RpcClient; -mod jsonrpc; -use crate::recsend::{Receiver, Sender}; -mod recsend; - const COMMAND_PATH: &str = "signal-cli"; #[derive(Parser, Debug)] @@ -152,10 +157,36 @@ async fn main() -> Result<()> { inner: FramedRead::new(cmd.stdout.take().unwrap(), LinesCodec::new()), }; - let mut client = ClientBuilder::default().build_with_tokio(sender, receiver); - let mut stream = client.subscribe_receive(None).await?; + let client = Arc::new(Mutex::new( + ClientBuilder::default().build_with_tokio(sender, receiver), + )); + + let mut scheduler = AsyncScheduler::new(); + let c = client.clone(); + let sender = config.allowed_sender.clone(); + scheduler.every(1_u32.day()).at("9:00").run(move || { + let c = c.clone(); + let sender = sender.clone(); + async move { + let c = c.lock().await; + let _ = send( + &c, + &sender, + "This is the signal-to-blog bot reminding you to post today.".to_string(), + ) + .await; + } + }); + tokio::spawn(async move { + loop { + scheduler.run_pending().await; + tokio::time::sleep(Duration::from_secs(1)).await; + } + }); + let mut stream = client.lock().await.subscribe_receive(None).await?; loop { let v = stream.next().await.unwrap()?; + let mut client = client.lock().await; // which message doesn't have a sender? let sender = get_msg_sender(&v).expect("which message doesn't have a sender?"); let text = get_msg_text(&v); @@ -194,7 +225,7 @@ async fn main() -> Result<()> { ) .await?; } else { - let res = generate_post(&config, &mut client).await; + let res = generate_post(&config, &mut client.borrow_mut()).await; if let Ok(res) = res { send( &client,