2022-11-04 18:40:14 +01:00
|
|
|
use channels::accounts::rpc::Accounts;
|
|
|
|
use channels::accounts::{me, register};
|
2022-11-04 10:13:44 +01:00
|
|
|
use channels::AsyncClient;
|
|
|
|
use config::SharedAppConfig;
|
|
|
|
use tarpc::context;
|
|
|
|
|
|
|
|
use crate::actions;
|
2022-11-04 18:40:14 +01:00
|
|
|
use crate::db::Database;
|
2022-11-04 10:13:44 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, serde::Serialize, thiserror::Error)]
|
|
|
|
#[serde(rename_all = "kebab-case", tag = "account")]
|
|
|
|
pub enum Error {
|
|
|
|
#[error("Unable to send or receive msg from database")]
|
|
|
|
DbCritical,
|
|
|
|
#[error("Failed to load account data")]
|
|
|
|
Account,
|
|
|
|
#[error("Failed to load account addresses")]
|
|
|
|
Addresses,
|
|
|
|
#[error("Unable to save record")]
|
|
|
|
Saving,
|
|
|
|
#[error("Unable to hash password")]
|
|
|
|
Hashing,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct AccountsServer {
|
|
|
|
db: Database,
|
|
|
|
config: SharedAppConfig,
|
|
|
|
mqtt_client: AsyncClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tarpc::server]
|
2022-11-04 18:40:14 +01:00
|
|
|
impl Accounts for AccountsServer {
|
|
|
|
async fn me(self, _: context::Context, input: me::Input) -> me::Output {
|
|
|
|
let res = actions::me(input.account_id, self.db).await;
|
2022-11-04 10:13:44 +01:00
|
|
|
tracing::info!("ME result: {:?}", res);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2022-11-04 18:40:14 +01:00
|
|
|
async fn register_account(
|
|
|
|
self,
|
|
|
|
_: context::Context,
|
|
|
|
input: register::Input,
|
|
|
|
) -> register::Output {
|
2022-11-05 01:08:45 +01:00
|
|
|
use channels::accounts::Error;
|
2022-11-04 18:40:14 +01:00
|
|
|
|
|
|
|
let res = actions::create_account(input, &self.db, self.config).await;
|
2022-11-04 10:13:44 +01:00
|
|
|
tracing::info!("REGISTER result: {:?}", res);
|
|
|
|
match res {
|
|
|
|
Ok(account) => {
|
2022-11-05 01:08:45 +01:00
|
|
|
self.mqtt_client.emit_account_created(&account).await;
|
2022-11-04 18:40:14 +01:00
|
|
|
register::Output {
|
2022-11-04 10:13:44 +01:00
|
|
|
account: Some(account),
|
|
|
|
error: None,
|
|
|
|
}
|
|
|
|
}
|
2022-11-04 18:40:14 +01:00
|
|
|
Err(_e) => register::Output {
|
2022-11-04 10:13:44 +01:00
|
|
|
account: None,
|
2022-11-04 18:40:14 +01:00
|
|
|
error: Some(Error::Account),
|
2022-11-04 10:13:44 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn start(config: SharedAppConfig, db: Database, mqtt_client: AsyncClient) {
|
2022-11-05 01:08:45 +01:00
|
|
|
let port = { config.lock().account_manager().rpc_port };
|
2022-11-04 10:13:44 +01:00
|
|
|
|
2022-11-05 01:08:45 +01:00
|
|
|
channels::rpc::start("accounts", port, || {
|
|
|
|
AccountsServer {
|
|
|
|
db: db.clone(),
|
|
|
|
config: config.clone(),
|
|
|
|
mqtt_client: mqtt_client.clone(),
|
|
|
|
}
|
|
|
|
.serve()
|
|
|
|
})
|
|
|
|
.await;
|
2022-11-04 10:13:44 +01:00
|
|
|
}
|