Actually set room when running SetRoomCommand #79
|
@ -1,5 +1,6 @@
|
||||||
use super::{Command, Execution, ExecutionResult};
|
use super::{Command, Execution, ExecutionResult};
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
|
use crate::db::Users;
|
||||||
use crate::error::BotError;
|
use crate::error::BotError;
|
||||||
use crate::matrix;
|
use crate::matrix;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
@ -144,10 +145,22 @@ impl Command for SetRoomCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
|
||||||
|
if !ctx.account.is_registered() {
|
||||||
|
return Err(BotError::AccountDoesNotExist);
|
||||||
|
}
|
||||||
|
|
||||||
let rooms_for_user = get_rooms_for_user(ctx.matrix_client, ctx.username).await?;
|
let rooms_for_user = get_rooms_for_user(ctx.matrix_client, ctx.username).await?;
|
||||||
let room = search_for_room(&rooms_for_user, &self.0);
|
let room = search_for_room(&rooms_for_user, &self.0);
|
||||||
|
|
||||||
if let Some(room) = room {
|
if let Some(room) = room {
|
||||||
|
let mut new_user = ctx
|
||||||
|
.account
|
||||||
|
.registered_user()
|
||||||
|
.cloned()
|
||||||
|
.ok_or(BotError::AccountDoesNotExist)?;
|
||||||
|
|
||||||
|
new_user.active_room = Some(room.id.clone());
|
||||||
|
ctx.db.upsert_user(&new_user).await?;
|
||||||
Execution::success(format!(r#"Active room set to "{}""#, room.name))
|
Execution::success(format!(r#"Active room set to "{}""#, room.name))
|
||||||
} else {
|
} else {
|
||||||
Err(BotError::RoomDoesNotExist)
|
Err(BotError::RoomDoesNotExist)
|
||||||
|
|
|
@ -60,9 +60,9 @@ impl Account {
|
||||||
|
|
||||||
/// Consume self into an Option<User> instance, which will be Some
|
/// Consume self into an Option<User> instance, which will be Some
|
||||||
/// if this account has a registered user, and None otherwise.
|
/// if this account has a registered user, and None otherwise.
|
||||||
pub fn registered_user(self) -> Option<User> {
|
pub fn registered_user(&self) -> Option<&User> {
|
||||||
match self {
|
match self {
|
||||||
Self::Registered(user) => Some(user),
|
Self::Registered(ref user) => Some(user),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue