Initial code dump: matrix feedbot, aka bender
This is a rewrite of our old feedbot in rust, heavily inspired from rek2's INN matrix bot and making use of some bits from matrix-rust-sdk This is an asynchronous tokio-based matrix client using a stateless feed fetcher implementation based on reqwest, it uses feed_rs for parsing RSS and Atom feeds. State persistence is achieved using a simple file-backed datastore with serde_yaml as a serialization format. Published under the GNU General Public License version 3 or later.
This commit is contained in:
88
src/state.rs
Normal file
88
src/state.rs
Normal file
@ -0,0 +1,88 @@
|
||||
/**
|
||||
* 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 };
|
||||
|
||||
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>
|
||||
) -> () {
|
||||
|
||||
{
|
||||
info!("Updating feed reader state");
|
||||
self.state.lock().unwrap().insert(uri.into(), dt.timestamp().into());
|
||||
}
|
||||
|
||||
info!("Persisting feed reader state");
|
||||
self.persist().await.unwrap();
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level="debug")]
|
||||
pub fn get(&self, uri: &str) -> Option<DateTime<Utc>> {
|
||||
info!("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)
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user