From 53339282e061d8a3e4ba8db27d3231dece665f26 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sat, 29 May 2021 20:26:20 +0000 Subject: [PATCH] Actually set room when running SetRoomCommand (#79) Also sort rooms in get_rooms_for_user for consistency. Co-Authored-By: projectmoon Co-Committed-By: projectmoon --- src/commands/rooms.rs | 21 ++++++++++++++++++++- src/logic.rs | 2 +- src/models.rs | 4 ++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/commands/rooms.rs b/src/commands/rooms.rs index e53d62b..48b90f6 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; @@ -63,7 +64,7 @@ async fn get_rooms_for_user( ) -> Result, BotError> { let user_id = UserId::try_from(user_id)?; let rooms_for_user = matrix::get_rooms_for_user(client, &user_id).await?; - let rooms_for_user: Vec = stream::iter(rooms_for_user) + let mut rooms_for_user: Vec = stream::iter(rooms_for_user) .filter_map(|room| async move { Some(room.display_name().await.map(|room_name| RoomNameAndId { id: room.room_id().to_string(), @@ -73,6 +74,12 @@ async fn get_rooms_for_user( .try_collect() .await?; + //Alphabetically descending, symbols first, ignore case. + let sort = |r1: &RoomNameAndId, r2: &RoomNameAndId| { + r1.name.to_lowercase().cmp(&r2.name.to_lowercase()) + }; + + rooms_for_user.sort_by(sort); Ok(rooms_for_user) } @@ -144,10 +151,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/logic.rs b/src/logic.rs index c9e5c5d..58dfd06 100644 --- a/src/logic.rs +++ b/src/logic.rs @@ -118,6 +118,6 @@ mod tests { assert!(matches!(account, Account::Registered(_))); let user_again = account.registered_user().unwrap(); - assert_eq!(user, user_again); + assert_eq!(user, *user_again); } } 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, } }