2020-07-17 21:00:39 +01:00
|
|
|
use std::convert::TryInto;
|
2020-05-03 16:25:31 +01:00
|
|
|
|
2020-06-21 20:58:42 +01:00
|
|
|
use crate::{utils, Error, Result};
|
2020-07-17 21:00:39 +01:00
|
|
|
use ruma::identifiers::ServerName;
|
2020-05-03 16:25:31 +01:00
|
|
|
pub const COUNTER: &str = "c";
|
|
|
|
|
|
|
|
pub struct Globals {
|
|
|
|
pub(super) globals: sled::Tree,
|
2020-06-05 17:19:26 +01:00
|
|
|
keypair: ruma::signatures::Ed25519KeyPair,
|
2020-05-03 16:25:31 +01:00
|
|
|
reqwest_client: reqwest::Client,
|
2020-07-17 21:00:39 +01:00
|
|
|
server_name: Box<ServerName>,
|
2020-06-06 18:02:31 +01:00
|
|
|
registration_disabled: bool,
|
2020-05-03 16:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Globals {
|
2020-06-09 14:13:17 +01:00
|
|
|
pub fn load(globals: sled::Tree, config: &rocket::Config) -> Result<Self> {
|
2020-06-05 17:19:26 +01:00
|
|
|
let keypair = ruma::signatures::Ed25519KeyPair::new(
|
2020-05-03 16:25:31 +01:00
|
|
|
&*globals
|
2020-06-09 14:13:17 +01:00
|
|
|
.update_and_fetch("keypair", utils::generate_keypair)?
|
|
|
|
.expect("utils::generate_keypair always returns Some"),
|
2020-05-03 16:25:31 +01:00
|
|
|
"key1".to_owned(),
|
|
|
|
)
|
2020-06-11 09:03:08 +01:00
|
|
|
.map_err(|_| Error::bad_database("Private or public keys are invalid."))?;
|
2020-05-03 16:25:31 +01:00
|
|
|
|
2020-06-09 14:13:17 +01:00
|
|
|
Ok(Self {
|
2020-05-03 16:25:31 +01:00
|
|
|
globals,
|
|
|
|
keypair,
|
|
|
|
reqwest_client: reqwest::Client::new(),
|
2020-07-17 21:00:39 +01:00
|
|
|
server_name: config
|
|
|
|
.get_str("server_name")
|
|
|
|
.unwrap_or("localhost")
|
|
|
|
.to_string()
|
|
|
|
.try_into()
|
2020-07-21 21:26:01 +01:00
|
|
|
.map_err(|_| Error::BadConfig("Invalid server name found."))?,
|
2020-06-06 18:02:31 +01:00
|
|
|
registration_disabled: config.get_bool("registration_disabled").unwrap_or(false),
|
2020-06-09 14:13:17 +01:00
|
|
|
})
|
2020-05-03 16:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns this server's keypair.
|
2020-06-05 17:19:26 +01:00
|
|
|
pub fn keypair(&self) -> &ruma::signatures::Ed25519KeyPair {
|
2020-05-03 16:25:31 +01:00
|
|
|
&self.keypair
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reqwest client which can be used to send requests.
|
|
|
|
pub fn reqwest_client(&self) -> &reqwest::Client {
|
|
|
|
&self.reqwest_client
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next_count(&self) -> Result<u64> {
|
|
|
|
Ok(utils::u64_from_bytes(
|
|
|
|
&self
|
|
|
|
.globals
|
|
|
|
.update_and_fetch(COUNTER, utils::increment)?
|
|
|
|
.expect("utils::increment will always put in a value"),
|
2020-06-09 14:13:17 +01:00
|
|
|
)
|
2020-06-11 09:03:08 +01:00
|
|
|
.map_err(|_| Error::bad_database("Count has invalid bytes."))?)
|
2020-05-03 16:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn current_count(&self) -> Result<u64> {
|
2020-06-09 14:13:17 +01:00
|
|
|
self.globals.get(COUNTER)?.map_or(Ok(0_u64), |bytes| {
|
|
|
|
Ok(utils::u64_from_bytes(&bytes)
|
2020-06-11 09:03:08 +01:00
|
|
|
.map_err(|_| Error::bad_database("Count has invalid bytes."))?)
|
2020-06-09 14:13:17 +01:00
|
|
|
})
|
2020-05-03 16:25:31 +01:00
|
|
|
}
|
2020-06-06 18:02:31 +01:00
|
|
|
|
2020-07-17 21:00:39 +01:00
|
|
|
pub fn server_name(&self) -> &ServerName {
|
2020-06-21 20:58:42 +01:00
|
|
|
self.server_name.as_ref()
|
2020-06-06 18:02:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn registration_disabled(&self) -> bool {
|
|
|
|
self.registration_disabled
|
|
|
|
}
|
2020-05-03 16:25:31 +01:00
|
|
|
}
|