2020-05-03 16:25:31 +01:00
|
|
|
pub(self) mod account_data;
|
2020-05-09 20:47:09 +01:00
|
|
|
pub(self) mod global_edus;
|
2020-05-03 16:25:31 +01:00
|
|
|
pub(self) mod globals;
|
2020-05-18 16:53:34 +01:00
|
|
|
pub(self) mod media;
|
2020-05-03 16:25:31 +01:00
|
|
|
pub(self) mod rooms;
|
2020-06-06 17:44:50 +01:00
|
|
|
pub(self) mod uiaa;
|
2020-05-03 16:25:31 +01:00
|
|
|
pub(self) mod users;
|
|
|
|
|
2020-06-09 14:13:17 +01:00
|
|
|
use crate::{Error, Result};
|
2020-03-30 12:46:18 +01:00
|
|
|
use directories::ProjectDirs;
|
2020-06-06 18:02:31 +01:00
|
|
|
use log::info;
|
2020-04-10 12:36:57 +01:00
|
|
|
use std::fs::remove_dir_all;
|
2020-03-30 12:46:18 +01:00
|
|
|
|
2020-05-06 14:36:44 +01:00
|
|
|
use rocket::Config;
|
|
|
|
|
2020-03-30 12:46:18 +01:00
|
|
|
pub struct Database {
|
2020-05-03 16:25:31 +01:00
|
|
|
pub globals: globals::Globals,
|
|
|
|
pub users: users::Users,
|
2020-06-06 17:44:50 +01:00
|
|
|
pub uiaa: uiaa::Uiaa,
|
2020-05-03 16:25:31 +01:00
|
|
|
pub rooms: rooms::Rooms,
|
|
|
|
pub account_data: account_data::AccountData,
|
2020-05-09 20:47:09 +01:00
|
|
|
pub global_edus: global_edus::GlobalEdus,
|
2020-05-18 16:53:34 +01:00
|
|
|
pub media: media::Media,
|
2020-05-03 16:25:31 +01:00
|
|
|
pub _db: sled::Db,
|
2020-03-30 12:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Database {
|
2020-04-10 12:36:57 +01:00
|
|
|
/// Tries to remove the old database but ignores all errors.
|
2020-06-09 14:13:17 +01:00
|
|
|
pub fn try_remove(server_name: &str) -> Result<()> {
|
2020-04-11 19:03:22 +01:00
|
|
|
let mut path = ProjectDirs::from("xyz", "koesters", "conduit")
|
2020-06-09 14:13:17 +01:00
|
|
|
.ok_or(Error::BadConfig(
|
|
|
|
"The OS didn't return a valid home directory path.",
|
|
|
|
))?
|
2020-04-10 12:36:57 +01:00
|
|
|
.data_dir()
|
|
|
|
.to_path_buf();
|
2020-05-06 14:36:44 +01:00
|
|
|
path.push(server_name);
|
2020-04-10 12:36:57 +01:00
|
|
|
let _ = remove_dir_all(path);
|
2020-06-09 14:13:17 +01:00
|
|
|
|
|
|
|
Ok(())
|
2020-04-10 12:36:57 +01:00
|
|
|
}
|
|
|
|
|
2020-03-30 12:46:18 +01:00
|
|
|
/// Load an existing database or create a new one.
|
2020-06-09 14:13:17 +01:00
|
|
|
pub fn load_or_create(config: &Config) -> Result<Self> {
|
2020-05-06 14:36:44 +01:00
|
|
|
let server_name = config.get_str("server_name").unwrap_or("localhost");
|
|
|
|
|
|
|
|
let path = config
|
|
|
|
.get_str("database_path")
|
2020-06-09 14:13:17 +01:00
|
|
|
.map(|x| Ok::<_, Error>(x.to_owned()))
|
2020-05-06 14:36:44 +01:00
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
let path = ProjectDirs::from("xyz", "koesters", "conduit")
|
2020-06-09 14:13:17 +01:00
|
|
|
.ok_or(Error::BadConfig(
|
|
|
|
"The OS didn't return a valid home directory path.",
|
|
|
|
))?
|
2020-05-06 14:36:44 +01:00
|
|
|
.data_dir()
|
|
|
|
.join(server_name);
|
|
|
|
|
2020-06-09 14:13:17 +01:00
|
|
|
Ok(path
|
|
|
|
.to_str()
|
|
|
|
.ok_or(Error::BadConfig("Database path contains invalid unicode."))?
|
|
|
|
.to_owned())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let db = sled::open(&path)?;
|
2020-06-06 18:02:31 +01:00
|
|
|
info!("Opened sled database at {}", path);
|
2020-03-30 12:46:18 +01:00
|
|
|
|
2020-06-09 14:13:17 +01:00
|
|
|
Ok(Self {
|
|
|
|
globals: globals::Globals::load(db.open_tree("global")?, config)?,
|
2020-05-03 16:25:31 +01:00
|
|
|
users: users::Users {
|
2020-06-09 14:13:17 +01:00
|
|
|
userid_password: db.open_tree("userid_password")?,
|
|
|
|
userid_displayname: db.open_tree("userid_displayname")?,
|
|
|
|
userid_avatarurl: db.open_tree("userid_avatarurl")?,
|
|
|
|
userdeviceid_token: db.open_tree("userdeviceid_token")?,
|
|
|
|
userdeviceid_metadata: db.open_tree("userdeviceid_metadata")?,
|
|
|
|
token_userdeviceid: db.open_tree("token_userdeviceid")?,
|
|
|
|
onetimekeyid_onetimekeys: db.open_tree("onetimekeyid_onetimekeys")?,
|
|
|
|
userdeviceid_devicekeys: db.open_tree("userdeviceid_devicekeys")?,
|
|
|
|
devicekeychangeid_userid: db.open_tree("devicekeychangeid_userid")?,
|
|
|
|
todeviceid_events: db.open_tree("todeviceid_events")?,
|
2020-05-03 16:25:31 +01:00
|
|
|
},
|
2020-06-06 17:44:50 +01:00
|
|
|
uiaa: uiaa::Uiaa {
|
2020-06-09 14:13:17 +01:00
|
|
|
userdeviceid_uiaainfo: db.open_tree("userdeviceid_uiaainfo")?,
|
2020-06-06 17:44:50 +01:00
|
|
|
},
|
2020-05-03 16:25:31 +01:00
|
|
|
rooms: rooms::Rooms {
|
|
|
|
edus: rooms::RoomEdus {
|
2020-06-09 14:13:17 +01:00
|
|
|
roomuserid_lastread: db.open_tree("roomuserid_lastread")?, // "Private" read receipt
|
|
|
|
roomlatestid_roomlatest: db.open_tree("roomlatestid_roomlatest")?, // Read receipts
|
|
|
|
roomactiveid_userid: db.open_tree("roomactiveid_userid")?, // Typing notifs
|
|
|
|
roomid_lastroomactiveupdate: db.open_tree("roomid_lastroomactiveupdate")?,
|
2020-05-03 16:25:31 +01:00
|
|
|
},
|
2020-06-09 14:13:17 +01:00
|
|
|
pduid_pdu: db.open_tree("pduid_pdu")?,
|
|
|
|
eventid_pduid: db.open_tree("eventid_pduid")?,
|
|
|
|
roomid_pduleaves: db.open_tree("roomid_pduleaves")?,
|
|
|
|
roomstateid_pdu: db.open_tree("roomstateid_pdu")?,
|
2020-05-03 16:25:31 +01:00
|
|
|
|
2020-06-09 14:13:17 +01:00
|
|
|
alias_roomid: db.open_tree("alias_roomid")?,
|
|
|
|
aliasid_alias: db.open_tree("alias_roomid")?,
|
|
|
|
publicroomids: db.open_tree("publicroomids")?,
|
2020-05-25 22:24:13 +01:00
|
|
|
|
2020-06-09 14:13:17 +01:00
|
|
|
userroomid_joined: db.open_tree("userroomid_joined")?,
|
|
|
|
roomuserid_joined: db.open_tree("roomuserid_joined")?,
|
|
|
|
userroomid_invited: db.open_tree("userroomid_invited")?,
|
|
|
|
roomuserid_invited: db.open_tree("roomuserid_invited")?,
|
|
|
|
userroomid_left: db.open_tree("userroomid_left")?,
|
2020-05-03 16:25:31 +01:00
|
|
|
},
|
|
|
|
account_data: account_data::AccountData {
|
2020-06-09 14:13:17 +01:00
|
|
|
roomuserdataid_accountdata: db.open_tree("roomuserdataid_accountdata")?,
|
2020-05-03 16:25:31 +01:00
|
|
|
},
|
2020-05-09 20:47:09 +01:00
|
|
|
global_edus: global_edus::GlobalEdus {
|
2020-06-09 14:13:17 +01:00
|
|
|
presenceid_presence: db.open_tree("presenceid_presence")?, // Presence
|
2020-05-09 20:47:09 +01:00
|
|
|
},
|
2020-05-18 16:53:34 +01:00
|
|
|
media: media::Media {
|
2020-06-09 14:13:17 +01:00
|
|
|
mediaid_file: db.open_tree("mediaid_file")?,
|
2020-05-18 16:53:34 +01:00
|
|
|
},
|
2020-05-03 16:25:31 +01:00
|
|
|
_db: db,
|
2020-06-09 14:13:17 +01:00
|
|
|
})
|
2020-03-30 12:46:18 +01:00
|
|
|
}
|
|
|
|
}
|