| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- use crate::categories::CatStats;
- use pickledb::{PickleDb, PickleDbDumpPolicy, SerializationMethod};
- use radix_trie::Trie;
- use shellexpand::tilde;
- use std::collections::HashSet;
- use std::path::PathBuf;
- use std::time::Duration;
- /// Configuration for single user
- pub struct User {
- /// Categories statistics for the user
- pub catmap: CatStats,
- /// Available accounts for the user
- pub accounts: HashSet<String>,
- /// database with config
- db: PickleDb,
- }
- #[cfg(not(feature = "docker"))]
- pub const DEFAULT_DB_PATH: &str = "~/.config/receqif/";
- #[cfg(feature = "docker")]
- pub const DEFAULT_DB_PATH: &str = "/etc/receqif/";
- impl Drop for User {
- fn drop(&mut self) {
- self.save_data();
- }
- }
- impl User {
- pub fn new(uid: i64, dbfile: &Option<String>) -> Self {
- let ten_sec = Duration::from_secs(10);
- let path: String = match dbfile {
- Some(path) => path.to_string(),
- None => DEFAULT_DB_PATH.to_owned() + &uid.to_string() + ".db",
- };
- let confpath: &str = &tilde(&path);
- let confpath = PathBuf::from(confpath);
- let dbase = PickleDb::load(
- &confpath,
- PickleDbDumpPolicy::PeriodicDump(ten_sec),
- SerializationMethod::Json,
- );
- let db = match dbase {
- Ok(db) => db,
- Err(_) => PickleDb::new(
- &confpath,
- PickleDbDumpPolicy::PeriodicDump(ten_sec),
- SerializationMethod::Json,
- ),
- };
- let catmap: CatStats = match db.get("catmap") {
- Some(v) => v,
- None => Trie::new(),
- };
- let accounts = match db.get::<Vec<String>>("accounts") {
- Some(a) => HashSet::from_iter(a),
- None => HashSet::new(),
- };
- User {
- catmap,
- accounts,
- db,
- }
- }
- pub fn accounts(&mut self, acc: Vec<String>) {
- self.accounts = HashSet::from_iter(acc);
- }
- pub fn save_data(&mut self) {
- log::debug!("Saving user data");
- self.db.set("catmap", &self.catmap).unwrap();
- self.db.dump().unwrap();
- }
- }
|