From f46b9142394c07f6c6026747118fb355f6db14d2 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sun, 22 Nov 2020 20:52:44 +0000 Subject: [PATCH] Add matrix client to context. --- src/bin/dicebot-cmd.rs | 10 +++++++++- src/bot.rs | 9 ++++++++- src/bot/event_handlers.rs | 3 +-- src/cofd/dice.rs | 24 +++++++++++++++++++++--- src/commands.rs | 8 +++++++- src/context.rs | 18 ++---------------- src/cthulhu/dice.rs | 9 ++++++++- 7 files changed, 56 insertions(+), 25 deletions(-) diff --git a/src/bin/dicebot-cmd.rs b/src/bin/dicebot-cmd.rs index 6add6ab..8af1121 100644 --- a/src/bin/dicebot-cmd.rs +++ b/src/bin/dicebot-cmd.rs @@ -12,7 +12,15 @@ async fn main() -> Result<(), BotError> { Err(e) => return Err(e), }; - let context = Context::new(&db, "roomid", "localuser", &input); + let context = Context { + db: db, + matrix_client: &matrix_sdk::Client::new("http://example.com") + .expect("Could not create matrix client"), + room_id: "roomid", + username: "@localuser:example.com", + message_body: &input, + }; + println!("{}", command.execute(&context).await.plain()); Ok(()) } diff --git a/src/bot.rs b/src/bot.rs index 5eb9b42..0886381 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -127,7 +127,14 @@ impl DiceBot { let commands = msg_body.trim().lines().filter(|line| line.starts_with("!")); for command in commands { - let ctx = Context::new(&self.db, &room_id.as_str(), &sender_username, &command); + let ctx = Context { + db: self.db.clone(), + matrix_client: &self.client, + room_id: room_id.as_str(), + username: &sender_username, + message_body: &command, + }; + let cmd_result = execute_command(&ctx).await; results.push(cmd_result); } diff --git a/src/bot/event_handlers.rs b/src/bot/event_handlers.rs index 6b1b4c5..11e1673 100644 --- a/src/bot/event_handlers.rs +++ b/src/bot/event_handlers.rs @@ -1,3 +1,4 @@ +use super::DiceBot; use crate::db::Database; use crate::error::BotError; use async_trait::async_trait; @@ -12,8 +13,6 @@ use matrix_sdk::{ identifiers::RoomId, Client, EventEmitter, SyncRoom, }; -//use matrix_sdk_common_macros::async_trait; -use super::DiceBot; use std::clone::Clone; use std::ops::Sub; use std::time::{Duration, SystemTime}; diff --git a/src/cofd/dice.rs b/src/cofd/dice.rs index f97ad73..df18dfa 100644 --- a/src/cofd/dice.rs +++ b/src/cofd/dice.rs @@ -461,7 +461,13 @@ mod tests { #[tokio::test] async fn rejects_large_expression_test() { let db = Database::new_temp().unwrap(); - let ctx = Context::new(&db, "roomid", "username", "message"); + let ctx = Context { + db: db, + matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(), + room_id: "roomid", + username: "username", + message_body: "message", + }; let mut amounts = vec![]; @@ -486,7 +492,13 @@ mod tests { #[tokio::test] async fn converts_to_chance_die_test() { let db = Database::new_temp().unwrap(); - let ctx = Context::new(&db, "roomid", "username", "message"); + let ctx = Context { + db: db, + matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(), + room_id: "roomid", + username: "username", + message_body: "message", + }; let mut amounts = vec![]; @@ -508,7 +520,13 @@ mod tests { #[tokio::test] async fn can_resolve_variables_test() { let db = Database::new_temp().unwrap(); - let ctx = Context::new(&db, "roomid", "username", "message"); + let ctx = Context { + db: db.clone(), + matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(), + room_id: "roomid", + username: "username", + message_body: "message", + }; let user_and_room = crate::db::variables::UserAndRoom(&ctx.username, &ctx.room_id); db.variables diff --git a/src/commands.rs b/src/commands.rs index 0a30fbf..8a67178 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -80,7 +80,13 @@ mod tests { #[tokio::test] async fn unrecognized_command() { let db = crate::db::Database::new_temp().unwrap(); - let ctx = Context::new(&db, "myroomid", "@testuser:example.com", "!notacommand"); + let ctx = Context { + db: db, + matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(), + room_id: "myroomid", + username: "myusername", + message_body: "!notacommand", + }; let result = execute_command(&ctx).await; assert!(result.plain.contains("Error")); } diff --git a/src/context.rs b/src/context.rs index 630659f..42a854a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,27 +1,13 @@ use crate::db::Database; +use matrix_sdk::Client; /// A context carried through the system providing access to things /// like the database. #[derive(Clone)] pub struct Context<'a> { pub db: Database, + pub matrix_client: &'a Client, pub room_id: &'a str, pub username: &'a str, pub message_body: &'a str, } - -impl<'a> Context<'a> { - pub fn new( - db: &Database, - room_id: &'a str, - username: &'a str, - message_body: &'a str, - ) -> Context<'a> { - Context { - db: db.clone(), - room_id: room_id, - username: username, - message_body: message_body, - } - } -} diff --git a/src/cthulhu/dice.rs b/src/cthulhu/dice.rs index 6f2a35a..4851034 100644 --- a/src/cthulhu/dice.rs +++ b/src/cthulhu/dice.rs @@ -380,7 +380,14 @@ mod tests { }; let db = Database::new_temp().unwrap(); - let ctx = Context::new(&db, "roomid", "username", "message"); + let ctx = Context { + db: db, + matrix_client: &matrix_sdk::Client::new("https://example.com").unwrap(), + room_id: "roomid", + username: "username", + message_body: "message", + }; + let roll_with_ctx = DiceRollWithContext(&roll, &ctx); let result = regular_roll(&roll_with_ctx).await; assert!(result.is_err());