2021-02-07 21:39:21 +00:00
|
|
|
use super::{Command, Execution, ExecutionResult};
|
2020-11-22 21:30:24 +00:00
|
|
|
use crate::context::Context;
|
2021-05-22 14:52:32 +00:00
|
|
|
use crate::db::Users;
|
|
|
|
use crate::error::BotError::PasswordCreationError;
|
|
|
|
use crate::logic::{hash_password, record_room_information};
|
|
|
|
use crate::models::User;
|
2020-11-22 21:30:24 +00:00
|
|
|
use async_trait::async_trait;
|
2020-11-29 21:03:45 +00:00
|
|
|
use matrix_sdk::identifiers::UserId;
|
2020-11-22 21:30:24 +00:00
|
|
|
|
|
|
|
pub struct ResyncCommand;
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl Command for ResyncCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"resync room information"
|
|
|
|
}
|
|
|
|
|
2021-05-21 15:32:08 +00:00
|
|
|
fn is_secure(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:39:21 +00:00
|
|
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
2020-11-23 19:54:16 +00:00
|
|
|
let our_username: Option<UserId> = ctx.matrix_client.user_id().await;
|
|
|
|
let our_username: &str = our_username.as_ref().map_or("", UserId::as_str);
|
2020-11-22 22:13:11 +00:00
|
|
|
|
2021-01-30 14:17:34 +00:00
|
|
|
record_room_information(
|
2021-01-29 22:35:07 +00:00
|
|
|
ctx.matrix_client,
|
|
|
|
&ctx.db,
|
2021-03-15 20:10:42 +00:00
|
|
|
ctx.room_id(),
|
2021-01-29 22:35:07 +00:00
|
|
|
&ctx.room.display_name,
|
|
|
|
our_username,
|
|
|
|
)
|
2021-01-30 14:17:34 +00:00
|
|
|
.await?;
|
2020-11-22 21:30:24 +00:00
|
|
|
|
2021-01-31 14:06:25 +00:00
|
|
|
let message = "Room information resynced.".to_string();
|
2021-02-07 21:39:21 +00:00
|
|
|
Execution::success(message)
|
2020-11-22 21:30:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-21 22:33:49 +00:00
|
|
|
|
|
|
|
pub struct RegisterCommand(pub String);
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl Command for RegisterCommand {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"register user account"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_secure(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
2021-05-22 14:52:32 +00:00
|
|
|
let pw_hash = hash_password(&self.0).map_err(|e| PasswordCreationError(e))?;
|
|
|
|
let user = User {
|
|
|
|
username: ctx.username.to_owned(),
|
|
|
|
password: pw_hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.db.upsert_user(&user).await?;
|
|
|
|
Execution::success("User account registered/updated".to_string())
|
2021-05-21 22:33:49 +00:00
|
|
|
}
|
|
|
|
}
|