diff --git a/src/commands/rooms.rs b/src/commands/rooms.rs index e53d62b..ac951e7 100644 --- a/src/commands/rooms.rs +++ b/src/commands/rooms.rs @@ -1,5 +1,6 @@ use super::{Command, Execution, ExecutionResult}; use crate::context::Context; +use crate::db::Users; use crate::error::BotError; use crate::matrix; use async_trait::async_trait; @@ -144,10 +145,22 @@ impl Command for SetRoomCommand { } 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 room = search_for_room(&rooms_for_user, &self.0); 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)) } else { Err(BotError::RoomDoesNotExist) diff --git a/src/models.rs b/src/models.rs index c185424..336a053 100644 --- a/src/models.rs +++ b/src/models.rs @@ -60,9 +60,9 @@ impl Account { /// Consume self into an Option instance, which will be Some /// if this account has a registered user, and None otherwise. - pub fn registered_user(self) -> Option { + pub fn registered_user(&self) -> Option<&User> { match self { - Self::Registered(user) => Some(user), + Self::Registered(ref user) => Some(user), _ => None, } }