Better database structure
This commit is contained in:
parent
6d27f15516
commit
34a53ce20a
7 changed files with 206 additions and 68 deletions
39
src/data.rs
Normal file
39
src/data.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use directories::ProjectDirs;
|
||||
use ruma_identifiers::UserId;
|
||||
|
||||
pub struct Data(sled::Db);
|
||||
|
||||
impl Data {
|
||||
pub fn set_hostname(&self, hostname: &str) {
|
||||
self.0.insert("hostname", hostname).unwrap();
|
||||
}
|
||||
pub fn hostname(&self) -> String {
|
||||
String::from_utf8(self.0.get("hostname").unwrap().unwrap().to_vec()).unwrap()
|
||||
}
|
||||
pub fn load_or_create() -> Self {
|
||||
Data(
|
||||
sled::open(
|
||||
ProjectDirs::from("xyz", "koesters", "matrixserver")
|
||||
.unwrap()
|
||||
.data_dir(),
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn user_exists(&self, user_id: &UserId) -> bool {
|
||||
self.0
|
||||
.open_tree("username_password")
|
||||
.unwrap()
|
||||
.contains_key(user_id.to_string())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn user_add(&self, user_id: UserId, password: Option<String>) {
|
||||
self.0
|
||||
.open_tree("username_password")
|
||||
.unwrap()
|
||||
.insert(user_id.to_string(), &*password.unwrap_or_default())
|
||||
.unwrap();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue