89 lines
2.5 KiB
Rust
89 lines
2.5 KiB
Rust
/**
|
|
* matrix-feedbot v0.1.0
|
|
*
|
|
* Copyright (C) 2024 The 1312 Media Collective
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
use std::{
|
|
error::Error,
|
|
sync::{Arc, Mutex}
|
|
};
|
|
|
|
use tokio::fs;
|
|
|
|
use chrono::{ DateTime, Utc };
|
|
use tracing::{ info, debug };
|
|
|
|
use serde_yaml::mapping::Mapping as FeedReaderState;
|
|
|
|
#[derive(Debug)]
|
|
pub struct FeedReaderStateDb {
|
|
state: Mutex<FeedReaderState>,
|
|
filename: String
|
|
}
|
|
|
|
impl FeedReaderStateDb {
|
|
pub async fn new(filename: &str)
|
|
-> Result<Arc<Self>, Box<dyn Error>> {
|
|
|
|
info!("loading {}", filename);
|
|
let db = Arc::new(FeedReaderStateDb {
|
|
state: Mutex::new(Self::load(filename).await?),
|
|
filename: String::from(filename)
|
|
});
|
|
|
|
Ok(db.clone())
|
|
}
|
|
|
|
pub async fn set(
|
|
&self,
|
|
uri: &str,
|
|
dt: DateTime<Utc>
|
|
) -> () {
|
|
|
|
{
|
|
debug!("Updating feed reader state");
|
|
self.state.lock().unwrap().insert(uri.into(), dt.timestamp().into());
|
|
}
|
|
|
|
self.persist().await.unwrap();
|
|
}
|
|
|
|
#[tracing::instrument(ret, level="debug")]
|
|
pub fn get(&self, uri: &str) -> Option<DateTime<Utc>> {
|
|
debug!("Retrieving state for feed {}", uri);
|
|
match self.state.lock().unwrap().get(uri) {
|
|
Some(t) => DateTime::from_timestamp((*t).as_i64().unwrap(), 0),
|
|
None => None
|
|
}
|
|
}
|
|
|
|
#[tracing::instrument(ret, level="debug")]
|
|
async fn load(state_file: &str) -> Result<FeedReaderState, Box<dyn Error>> {
|
|
let serialized_state = fs::read_to_string(state_file).await?;
|
|
let state: FeedReaderState = serde_yaml::from_str(&serialized_state)?;
|
|
Ok(state)
|
|
}
|
|
|
|
#[tracing::instrument(ret, level="debug")]
|
|
async fn persist(&self) -> Result<(), Box<dyn Error>> {
|
|
let serialized_state = serde_yaml::to_string(&self.state)?;
|
|
fs::write(self.filename.clone(), &serialized_state.as_bytes()).await?;
|
|
Ok(())
|
|
}
|
|
}
|
|
|