From 569ba4f2e0d230b9c580da9b8179f3ad28550a31 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sun, 29 Nov 2020 21:03:45 +0000 Subject: [PATCH] Carry full room instance in context instead of just room id. --- src/bin/dicebot-cmd.rs | 16 +++++++++++++--- src/bot.rs | 2 +- src/cofd/dice.rs | 19 +++++++++++++++---- src/commands.rs | 10 +++++++++- src/commands/management.rs | 5 ++--- src/commands/variables.rs | 8 ++++---- src/context.rs | 3 ++- src/cthulhu/dice.rs | 10 +++++++++- src/dice.rs | 2 +- 9 files changed, 56 insertions(+), 19 deletions(-) diff --git a/src/bin/dicebot-cmd.rs b/src/bin/dicebot-cmd.rs index 8af1121..c9b93da 100644 --- a/src/bin/dicebot-cmd.rs +++ b/src/bin/dicebot-cmd.rs @@ -2,10 +2,20 @@ use chronicle_dicebot::commands; use chronicle_dicebot::context::Context; use chronicle_dicebot::db::Database; use chronicle_dicebot::error::BotError; +use matrix_sdk::{ + identifiers::{room_id, user_id}, + Room, +}; + +fn dummy_room() -> Room { + Room::new( + &room_id!("!fakeroomid:example.com"), + &user_id!("@fakeuserid:example.com"), + ) +} #[tokio::main] async fn main() -> Result<(), BotError> { - let db = Database::new_temp()?; let input = std::env::args().skip(1).collect::>().join(" "); let command = match commands::parser::parse_command(&input) { Ok(command) => command, @@ -13,10 +23,10 @@ async fn main() -> Result<(), BotError> { }; let context = Context { - db: db, + db: Database::new_temp()?, matrix_client: &matrix_sdk::Client::new("http://example.com") .expect("Could not create matrix client"), - room_id: "roomid", + room: &dummy_room(), username: "@localuser:example.com", message_body: &input, }; diff --git a/src/bot.rs b/src/bot.rs index 0886381..9d2d5c7 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -130,7 +130,7 @@ impl DiceBot { let ctx = Context { db: self.db.clone(), matrix_client: &self.client, - room_id: room_id.as_str(), + room: room, username: &sender_username, message_body: &command, }; diff --git a/src/cofd/dice.rs b/src/cofd/dice.rs index df18dfa..64e798c 100644 --- a/src/cofd/dice.rs +++ b/src/cofd/dice.rs @@ -323,6 +323,14 @@ mod tests { use super::*; use crate::db::Database; + /// Create dummy room instance. + fn dummy_room() -> matrix_sdk::Room { + matrix_sdk::Room::new( + &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"), + &matrix_sdk::identifiers::user_id!("@fakeuserid:example.com"), + ) + } + ///Instead of being random, generate a series of numbers we have complete ///control over. struct SequentialDieRoller { @@ -464,7 +472,7 @@ mod tests { let ctx = Context { db: db, matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(), - room_id: "roomid", + room: &dummy_room(), username: "username", message_body: "message", }; @@ -495,7 +503,7 @@ mod tests { let ctx = Context { db: db, matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(), - room_id: "roomid", + room: &dummy_room(), username: "username", message_body: "message", }; @@ -519,15 +527,18 @@ mod tests { #[tokio::test] async fn can_resolve_variables_test() { + use crate::db::variables::UserAndRoom; + let db = Database::new_temp().unwrap(); let ctx = Context { db: db.clone(), matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(), - room_id: "roomid", + room: &dummy_room(), username: "username", message_body: "message", }; - let user_and_room = crate::db::variables::UserAndRoom(&ctx.username, &ctx.room_id); + + let user_and_room = UserAndRoom(&ctx.username, &ctx.room.room_id.as_str()); db.variables .set_user_variable(&user_and_room, "myvariable", 10) diff --git a/src/commands.rs b/src/commands.rs index dd2c89e..8bb950a 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -78,13 +78,21 @@ pub async fn execute_command(ctx: &Context<'_>) -> CommandResult { mod tests { use super::*; + /// Create a dummy room instance. + fn dummy_room() -> matrix_sdk::Room { + matrix_sdk::Room::new( + &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"), + &matrix_sdk::identifiers::user_id!("@fakeuserid:example.com"), + ) + } + #[tokio::test] async fn unrecognized_command() { let db = crate::db::Database::new_temp().unwrap(); let ctx = Context { db: db, matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(), - room_id: "myroomid", + room: &dummy_room(), username: "myusername", message_body: "!notacommand", }; diff --git a/src/commands/management.rs b/src/commands/management.rs index 564a59f..ac150cf 100644 --- a/src/commands/management.rs +++ b/src/commands/management.rs @@ -3,8 +3,7 @@ use crate::context::Context; use crate::db::errors::DataError; use crate::matrix; use async_trait::async_trait; -use matrix_sdk::identifiers::{RoomId, UserId}; -use std::convert::TryFrom; +use matrix_sdk::identifiers::UserId; pub struct ResyncCommand; @@ -17,7 +16,7 @@ impl Command for ResyncCommand { } async fn execute(&self, ctx: &Context<'_>) -> Execution { - let room_id = RoomId::try_from(ctx.room_id).expect("failed to decode room ID"); + let room_id = &ctx.room.room_id; let our_username: Option = ctx.matrix_client.user_id().await; let our_username: &str = our_username.as_ref().map_or("", UserId::as_str); diff --git a/src/commands/variables.rs b/src/commands/variables.rs index 3f5cb50..8e47dbd 100644 --- a/src/commands/variables.rs +++ b/src/commands/variables.rs @@ -13,7 +13,7 @@ impl Command for GetAllVariablesCommand { } async fn execute(&self, ctx: &Context<'_>) -> Execution { - let key = UserAndRoom(&ctx.username, &ctx.room_id); + let key = UserAndRoom(&ctx.username, &ctx.room.room_id.as_str()); let result = ctx.db.variables.get_user_variables(&key); let value = match result { @@ -48,7 +48,7 @@ impl Command for GetVariableCommand { async fn execute(&self, ctx: &Context<'_>) -> Execution { let name = &self.0; - let key = UserAndRoom(&ctx.username, &ctx.room_id); + let key = UserAndRoom(&ctx.username, &ctx.room.room_id.as_str()); let result = ctx.db.variables.get_user_variable(&key, name); let value = match result { @@ -74,7 +74,7 @@ impl Command for SetVariableCommand { async fn execute(&self, ctx: &Context<'_>) -> Execution { let name = &self.0; let value = self.1; - let key = UserAndRoom(&ctx.username, ctx.room_id); + let key = UserAndRoom(&ctx.username, ctx.room.room_id.as_str()); let result = ctx.db.variables.set_user_variable(&key, name, value); let content = match result { @@ -98,7 +98,7 @@ impl Command for DeleteVariableCommand { async fn execute(&self, ctx: &Context<'_>) -> Execution { let name = &self.0; - let key = UserAndRoom(&ctx.username, ctx.room_id); + let key = UserAndRoom(&ctx.username, ctx.room.room_id.as_str()); let result = ctx.db.variables.delete_user_variable(&key, name); let value = match result { diff --git a/src/context.rs b/src/context.rs index 42a854a..7fead4b 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,5 +1,6 @@ use crate::db::Database; use matrix_sdk::Client; +use matrix_sdk::Room; /// A context carried through the system providing access to things /// like the database. @@ -7,7 +8,7 @@ use matrix_sdk::Client; pub struct Context<'a> { pub db: Database, pub matrix_client: &'a Client, - pub room_id: &'a str, + pub room: &'a Room, pub username: &'a str, pub message_body: &'a str, } diff --git a/src/cthulhu/dice.rs b/src/cthulhu/dice.rs index 4851034..0aeb015 100644 --- a/src/cthulhu/dice.rs +++ b/src/cthulhu/dice.rs @@ -343,6 +343,14 @@ mod tests { use crate::db::Database; use crate::parser::{Amount, Element, Operator}; + /// Create a dummy room instance. + fn dummy_room() -> matrix_sdk::Room { + matrix_sdk::Room::new( + &matrix_sdk::identifiers::room_id!("!fakeroomid:example.com"), + &matrix_sdk::identifiers::user_id!("@fakeuserid:example.com"), + ) + } + /// Generate a series of numbers manually for testing. For this /// die system, the first roll in the Vec should be the unit roll, /// and any subsequent rolls should be the tens place roll. The @@ -383,7 +391,7 @@ mod tests { let ctx = Context { db: db, matrix_client: &matrix_sdk::Client::new("https://example.com").unwrap(), - room_id: "roomid", + room: &dummy_room(), username: "username", message_body: "message", }; diff --git a/src/dice.rs b/src/dice.rs index 1472559..f2e0fba 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -9,7 +9,7 @@ use futures::stream::{self, StreamExt, TryStreamExt}; //New hotness pub async fn calculate_dice_amount(amounts: &[Amount], ctx: &Context<'_>) -> Result { let stream = stream::iter(amounts); - let key = UserAndRoom(&ctx.username, &ctx.room_id); + let key = UserAndRoom(&ctx.username, ctx.room.room_id.as_str()); let variables = &ctx.db.variables.get_user_variables(&key)?; use DiceRollingError::VariableNotFound;