diff --git a/src/bot/event_handlers.rs b/src/bot/event_handlers.rs index 7d735cd..afb725f 100644 --- a/src/bot/event_handlers.rs +++ b/src/bot/event_handlers.rs @@ -1,7 +1,7 @@ use super::DiceBot; use crate::db::Database; use crate::error::BotError; -use crate::matrix; +use crate::logic::record_room_information; use async_trait::async_trait; use log::{debug, error, info, warn}; use matrix_sdk::{ @@ -92,33 +92,6 @@ fn should_process_event(db: &Database, room_id: &str, event_id: &str) -> bool { }) } -//TODO this needs to be moved to a common API layer. -/// Record the information about a room, including users in it. -pub async fn record_room_information( - client: &matrix_sdk::Client, - db: &crate::db::Database, - room: &matrix_sdk::Room, - our_username: &str, -) -> Result<(), crate::db::errors::DataError> { - let room_id_str = room.room_id.as_str(); - let usernames = matrix::get_users_in_room(&client, &room.room_id).await; - - let info = crate::models::RoomInfo { - room_id: room_id_str.to_owned(), - room_name: room.display_name(), - }; - - // TODO this and the username adding should be one whole - // transaction in the db. - db.rooms.insert_room_info(&info)?; - - usernames - .into_iter() - .filter(|username| username != our_username) - .map(|username| db.rooms.add_user_to_room(&username, room_id_str)) - .collect() //Make use of collect impl on Result. -} - /// This event emitter listens for messages with dice rolling commands. /// Originally adapted from the matrix-rust-sdk examples. #[async_trait] diff --git a/src/commands/management.rs b/src/commands/management.rs index 2243936..e71630b 100644 --- a/src/commands/management.rs +++ b/src/commands/management.rs @@ -1,6 +1,7 @@ use super::{Command, Execution}; use crate::context::Context; use crate::db::errors::DataError; +use crate::logic::record_room_information; use async_trait::async_trait; use matrix_sdk::identifiers::UserId; @@ -18,13 +19,8 @@ impl Command for ResyncCommand { let our_username: Option = ctx.matrix_client.user_id().await; let our_username: &str = our_username.as_ref().map_or("", UserId::as_str); - let result: ResyncResult = crate::bot::event_handlers::record_room_information( - ctx.matrix_client, - &ctx.db, - &ctx.room, - our_username, - ) - .await; + let result: ResyncResult = + record_room_information(ctx.matrix_client, &ctx.db, &ctx.room, our_username).await; let (plain, html) = match result { Ok(()) => { diff --git a/src/lib.rs b/src/lib.rs index 8340421..d67ade8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ pub mod db; pub mod dice; pub mod error; mod help; +pub mod logic; pub mod matrix; pub mod models; mod parser; diff --git a/src/logic.rs b/src/logic.rs new file mode 100644 index 0000000..8649956 --- /dev/null +++ b/src/logic.rs @@ -0,0 +1,30 @@ +use crate::db::errors::DataError; +use crate::matrix; +use crate::models::RoomInfo; +use matrix_sdk::{self, Client, Room}; + +/// Record the information about a room, including users in it. +pub async fn record_room_information( + client: &Client, + db: &crate::db::Database, + room: &Room, + our_username: &str, +) -> Result<(), DataError> { + let room_id_str = room.room_id.as_str(); + let usernames = matrix::get_users_in_room(&client, &room.room_id).await; + + let info = RoomInfo { + room_id: room_id_str.to_owned(), + room_name: room.display_name(), + }; + + // TODO this and the username adding should be one whole + // transaction in the db. + db.rooms.insert_room_info(&info)?; + + usernames + .into_iter() + .filter(|username| username != our_username) + .map(|username| db.rooms.add_user_to_room(&username, room_id_str)) + .collect() //Make use of collect impl on Result. +}