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-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-05-06 14:36:44 +01:00
|
|
|
pub fn try_remove(server_name: &str) {
|
2020-04-11 19:03:22 +01:00
|
|
|
let mut path = ProjectDirs::from("xyz", "koesters", "conduit")
|
2020-04-10 12:36:57 +01:00
|
|
|
.unwrap()
|
|
|
|
.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-03-30 12:46:18 +01:00
|
|
|
/// Load an existing database or create a new one.
|
2020-05-06 14:36:44 +01:00
|
|
|
pub fn load_or_create(config: &Config) -> Self {
|
|
|
|
let server_name = config.get_str("server_name").unwrap_or("localhost");
|
|
|
|
|
|
|
|
let path = config
|
|
|
|
.get_str("database_path")
|
|
|
|
.map(|x| x.to_owned())
|
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
let path = ProjectDirs::from("xyz", "koesters", "conduit")
|
|
|
|
.unwrap()
|
|
|
|
.data_dir()
|
|
|
|
.join(server_name);
|
|
|
|
path.to_str().unwrap().to_owned()
|
|
|
|
});
|
|
|
|
|
2020-03-30 12:46:18 +01:00
|
|
|
let db = sled::open(&path).unwrap();
|
2020-06-06 18:02:31 +01:00
|
|
|
info!("Opened sled database at {}", path);
|
2020-03-30 12:46:18 +01:00
|
|
|
|
|
|
|
Self {
|
2020-06-06 18:02:31 +01:00
|
|
|
globals: globals::Globals::load(db.open_tree("global").unwrap(), config),
|
2020-05-03 16:25:31 +01:00
|
|
|
users: users::Users {
|
|
|
|
userid_password: db.open_tree("userid_password").unwrap(),
|
|
|
|
userid_displayname: db.open_tree("userid_displayname").unwrap(),
|
|
|
|
userid_avatarurl: db.open_tree("userid_avatarurl").unwrap(),
|
|
|
|
userdeviceid_token: db.open_tree("userdeviceid_token").unwrap(),
|
2020-06-03 12:41:30 +01:00
|
|
|
userdeviceid_metadata: db.open_tree("userdeviceid_metadata").unwrap(),
|
2020-05-10 17:30:12 +01:00
|
|
|
token_userdeviceid: db.open_tree("token_userdeviceid").unwrap(),
|
2020-05-17 18:56:40 +01:00
|
|
|
onetimekeyid_onetimekeys: db.open_tree("onetimekeyid_onetimekeys").unwrap(),
|
|
|
|
userdeviceid_devicekeys: db.open_tree("userdeviceid_devicekeys").unwrap(),
|
2020-05-18 10:36:32 +01:00
|
|
|
devicekeychangeid_userid: db.open_tree("devicekeychangeid_userid").unwrap(),
|
2020-05-17 18:56:40 +01:00
|
|
|
todeviceid_events: db.open_tree("todeviceid_events").unwrap(),
|
2020-05-03 16:25:31 +01:00
|
|
|
},
|
2020-06-06 17:44:50 +01:00
|
|
|
uiaa: uiaa::Uiaa {
|
|
|
|
userdeviceid_uiaainfo: db.open_tree("userdeviceid_uiaainfo").unwrap(),
|
|
|
|
},
|
2020-05-03 16:25:31 +01:00
|
|
|
rooms: rooms::Rooms {
|
|
|
|
edus: rooms::RoomEdus {
|
2020-05-10 17:30:12 +01:00
|
|
|
roomuserid_lastread: db.open_tree("roomuserid_lastread").unwrap(), // "Private" read receipt
|
2020-05-08 20:13:52 +01:00
|
|
|
roomlatestid_roomlatest: db.open_tree("roomlatestid_roomlatest").unwrap(), // Read receipts
|
2020-06-04 10:17:36 +01:00
|
|
|
roomactiveid_userid: db.open_tree("roomactiveid_userid").unwrap(), // Typing notifs
|
|
|
|
roomid_lastroomactiveupdate: db
|
|
|
|
.open_tree("roomid_lastroomactiveupdate")
|
|
|
|
.unwrap(),
|
2020-05-03 16:25:31 +01:00
|
|
|
},
|
|
|
|
pduid_pdu: db.open_tree("pduid_pdu").unwrap(),
|
|
|
|
eventid_pduid: db.open_tree("eventid_pduid").unwrap(),
|
|
|
|
roomid_pduleaves: db.open_tree("roomid_pduleaves").unwrap(),
|
|
|
|
roomstateid_pdu: db.open_tree("roomstateid_pdu").unwrap(),
|
|
|
|
|
2020-05-25 22:24:13 +01:00
|
|
|
alias_roomid: db.open_tree("alias_roomid").unwrap(),
|
|
|
|
aliasid_alias: db.open_tree("alias_roomid").unwrap(),
|
|
|
|
publicroomids: db.open_tree("publicroomids").unwrap(),
|
|
|
|
|
2020-05-03 16:25:31 +01:00
|
|
|
userroomid_joined: db.open_tree("userroomid_joined").unwrap(),
|
|
|
|
roomuserid_joined: db.open_tree("roomuserid_joined").unwrap(),
|
|
|
|
userroomid_invited: db.open_tree("userroomid_invited").unwrap(),
|
|
|
|
roomuserid_invited: db.open_tree("roomuserid_invited").unwrap(),
|
|
|
|
userroomid_left: db.open_tree("userroomid_left").unwrap(),
|
|
|
|
},
|
|
|
|
account_data: account_data::AccountData {
|
|
|
|
roomuserdataid_accountdata: db.open_tree("roomuserdataid_accountdata").unwrap(),
|
|
|
|
},
|
2020-05-09 20:47:09 +01:00
|
|
|
global_edus: global_edus::GlobalEdus {
|
2020-06-04 21:36:48 +01:00
|
|
|
presenceid_presence: db.open_tree("presenceid_presence").unwrap(), // Presence
|
2020-05-09 20:47:09 +01:00
|
|
|
},
|
2020-05-18 16:53:34 +01:00
|
|
|
media: media::Media {
|
|
|
|
mediaid_file: db.open_tree("mediaid_file").unwrap(),
|
|
|
|
},
|
2020-05-03 16:25:31 +01:00
|
|
|
_db: db,
|
2020-04-11 19:03:22 +01:00
|
|
|
}
|
2020-03-30 12:46:18 +01:00
|
|
|
}
|
|
|
|
}
|