Add matrix client to context.

This commit is contained in:
projectmoon 2020-11-22 20:52:44 +00:00
parent f352c90b6b
commit f46b914239
7 changed files with 56 additions and 25 deletions

View File

@ -12,7 +12,15 @@ async fn main() -> Result<(), BotError> {
Err(e) => return Err(e), 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()); println!("{}", command.execute(&context).await.plain());
Ok(()) Ok(())
} }

View File

@ -127,7 +127,14 @@ impl DiceBot {
let commands = msg_body.trim().lines().filter(|line| line.starts_with("!")); let commands = msg_body.trim().lines().filter(|line| line.starts_with("!"));
for command in commands { 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; let cmd_result = execute_command(&ctx).await;
results.push(cmd_result); results.push(cmd_result);
} }

View File

@ -1,3 +1,4 @@
use super::DiceBot;
use crate::db::Database; use crate::db::Database;
use crate::error::BotError; use crate::error::BotError;
use async_trait::async_trait; use async_trait::async_trait;
@ -12,8 +13,6 @@ use matrix_sdk::{
identifiers::RoomId, identifiers::RoomId,
Client, EventEmitter, SyncRoom, Client, EventEmitter, SyncRoom,
}; };
//use matrix_sdk_common_macros::async_trait;
use super::DiceBot;
use std::clone::Clone; use std::clone::Clone;
use std::ops::Sub; use std::ops::Sub;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};

View File

@ -461,7 +461,13 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn rejects_large_expression_test() { async fn rejects_large_expression_test() {
let db = Database::new_temp().unwrap(); 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![]; let mut amounts = vec![];
@ -486,7 +492,13 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn converts_to_chance_die_test() { async fn converts_to_chance_die_test() {
let db = Database::new_temp().unwrap(); 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![]; let mut amounts = vec![];
@ -508,7 +520,13 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn can_resolve_variables_test() { async fn can_resolve_variables_test() {
let db = Database::new_temp().unwrap(); 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); let user_and_room = crate::db::variables::UserAndRoom(&ctx.username, &ctx.room_id);
db.variables db.variables

View File

@ -80,7 +80,13 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn unrecognized_command() { async fn unrecognized_command() {
let db = crate::db::Database::new_temp().unwrap(); 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; let result = execute_command(&ctx).await;
assert!(result.plain.contains("Error")); assert!(result.plain.contains("Error"));
} }

View File

@ -1,27 +1,13 @@
use crate::db::Database; use crate::db::Database;
use matrix_sdk::Client;
/// A context carried through the system providing access to things /// A context carried through the system providing access to things
/// like the database. /// like the database.
#[derive(Clone)] #[derive(Clone)]
pub struct Context<'a> { pub struct Context<'a> {
pub db: Database, pub db: Database,
pub matrix_client: &'a Client,
pub room_id: &'a str, pub room_id: &'a str,
pub username: &'a str, pub username: &'a str,
pub message_body: &'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,
}
}
}

View File

@ -380,7 +380,14 @@ mod tests {
}; };
let db = Database::new_temp().unwrap(); 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 roll_with_ctx = DiceRollWithContext(&roll, &ctx);
let result = regular_roll(&roll_with_ctx).await; let result = regular_roll(&roll_with_ctx).await;
assert!(result.is_err()); assert!(result.is_err());