Compare commits
No commits in common. "569ba4f2e0d230b9c580da9b8179f3ad28550a31" and "c8c38ac1d42085d2070ef643b219ac667738d94f" have entirely different histories.
569ba4f2e0
...
c8c38ac1d4
|
@ -2,20 +2,10 @@ use chronicle_dicebot::commands;
|
||||||
use chronicle_dicebot::context::Context;
|
use chronicle_dicebot::context::Context;
|
||||||
use chronicle_dicebot::db::Database;
|
use chronicle_dicebot::db::Database;
|
||||||
use chronicle_dicebot::error::BotError;
|
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]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), BotError> {
|
async fn main() -> Result<(), BotError> {
|
||||||
|
let db = Database::new_temp()?;
|
||||||
let input = std::env::args().skip(1).collect::<Vec<String>>().join(" ");
|
let input = std::env::args().skip(1).collect::<Vec<String>>().join(" ");
|
||||||
let command = match commands::parser::parse_command(&input) {
|
let command = match commands::parser::parse_command(&input) {
|
||||||
Ok(command) => command,
|
Ok(command) => command,
|
||||||
|
@ -23,10 +13,10 @@ async fn main() -> Result<(), BotError> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let context = Context {
|
let context = Context {
|
||||||
db: Database::new_temp()?,
|
db: db,
|
||||||
matrix_client: &matrix_sdk::Client::new("http://example.com")
|
matrix_client: &matrix_sdk::Client::new("http://example.com")
|
||||||
.expect("Could not create matrix client"),
|
.expect("Could not create matrix client"),
|
||||||
room: &dummy_room(),
|
room_id: "roomid",
|
||||||
username: "@localuser:example.com",
|
username: "@localuser:example.com",
|
||||||
message_body: &input,
|
message_body: &input,
|
||||||
};
|
};
|
||||||
|
|
|
@ -130,7 +130,7 @@ impl DiceBot {
|
||||||
let ctx = Context {
|
let ctx = Context {
|
||||||
db: self.db.clone(),
|
db: self.db.clone(),
|
||||||
matrix_client: &self.client,
|
matrix_client: &self.client,
|
||||||
room: room,
|
room_id: room_id.as_str(),
|
||||||
username: &sender_username,
|
username: &sender_username,
|
||||||
message_body: &command,
|
message_body: &command,
|
||||||
};
|
};
|
||||||
|
|
|
@ -100,16 +100,6 @@ async fn record_room_information(
|
||||||
) -> Result<(), crate::db::errors::DataError> {
|
) -> Result<(), crate::db::errors::DataError> {
|
||||||
let room_id_str = room.room_id.as_str();
|
let room_id_str = room.room_id.as_str();
|
||||||
let usernames = matrix::get_users_in_room(&client, &room.room_id).await;
|
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
|
usernames
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|username| username != our_username)
|
.filter(|username| username != our_username)
|
||||||
|
|
|
@ -323,14 +323,6 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::db::Database;
|
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
|
///Instead of being random, generate a series of numbers we have complete
|
||||||
///control over.
|
///control over.
|
||||||
struct SequentialDieRoller {
|
struct SequentialDieRoller {
|
||||||
|
@ -472,7 +464,7 @@ mod tests {
|
||||||
let ctx = Context {
|
let ctx = Context {
|
||||||
db: db,
|
db: db,
|
||||||
matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(),
|
matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(),
|
||||||
room: &dummy_room(),
|
room_id: "roomid",
|
||||||
username: "username",
|
username: "username",
|
||||||
message_body: "message",
|
message_body: "message",
|
||||||
};
|
};
|
||||||
|
@ -503,7 +495,7 @@ mod tests {
|
||||||
let ctx = Context {
|
let ctx = Context {
|
||||||
db: db,
|
db: db,
|
||||||
matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(),
|
matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(),
|
||||||
room: &dummy_room(),
|
room_id: "roomid",
|
||||||
username: "username",
|
username: "username",
|
||||||
message_body: "message",
|
message_body: "message",
|
||||||
};
|
};
|
||||||
|
@ -527,18 +519,15 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn can_resolve_variables_test() {
|
async fn can_resolve_variables_test() {
|
||||||
use crate::db::variables::UserAndRoom;
|
|
||||||
|
|
||||||
let db = Database::new_temp().unwrap();
|
let db = Database::new_temp().unwrap();
|
||||||
let ctx = Context {
|
let ctx = Context {
|
||||||
db: db.clone(),
|
db: db.clone(),
|
||||||
matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(),
|
matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(),
|
||||||
room: &dummy_room(),
|
room_id: "roomid",
|
||||||
username: "username",
|
username: "username",
|
||||||
message_body: "message",
|
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
|
db.variables
|
||||||
.set_user_variable(&user_and_room, "myvariable", 10)
|
.set_user_variable(&user_and_room, "myvariable", 10)
|
||||||
|
|
|
@ -78,21 +78,13 @@ pub async fn execute_command(ctx: &Context<'_>) -> CommandResult {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
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]
|
#[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 {
|
let ctx = Context {
|
||||||
db: db,
|
db: db,
|
||||||
matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(),
|
matrix_client: &matrix_sdk::Client::new("http://example.com").unwrap(),
|
||||||
room: &dummy_room(),
|
room_id: "myroomid",
|
||||||
username: "myusername",
|
username: "myusername",
|
||||||
message_body: "!notacommand",
|
message_body: "!notacommand",
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,7 +3,8 @@ use crate::context::Context;
|
||||||
use crate::db::errors::DataError;
|
use crate::db::errors::DataError;
|
||||||
use crate::matrix;
|
use crate::matrix;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use matrix_sdk::identifiers::UserId;
|
use matrix_sdk::identifiers::{RoomId, UserId};
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
pub struct ResyncCommand;
|
pub struct ResyncCommand;
|
||||||
|
|
||||||
|
@ -16,7 +17,7 @@ impl Command for ResyncCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
||||||
let room_id = &ctx.room.room_id;
|
let room_id = RoomId::try_from(ctx.room_id).expect("failed to decode room ID");
|
||||||
let our_username: Option<UserId> = ctx.matrix_client.user_id().await;
|
let our_username: Option<UserId> = ctx.matrix_client.user_id().await;
|
||||||
let our_username: &str = our_username.as_ref().map_or("", UserId::as_str);
|
let our_username: &str = our_username.as_ref().map_or("", UserId::as_str);
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ impl Command for GetAllVariablesCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
||||||
let key = UserAndRoom(&ctx.username, &ctx.room.room_id.as_str());
|
let key = UserAndRoom(&ctx.username, &ctx.room_id);
|
||||||
let result = ctx.db.variables.get_user_variables(&key);
|
let result = ctx.db.variables.get_user_variables(&key);
|
||||||
|
|
||||||
let value = match result {
|
let value = match result {
|
||||||
|
@ -48,7 +48,7 @@ impl Command for GetVariableCommand {
|
||||||
|
|
||||||
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
||||||
let name = &self.0;
|
let name = &self.0;
|
||||||
let key = UserAndRoom(&ctx.username, &ctx.room.room_id.as_str());
|
let key = UserAndRoom(&ctx.username, &ctx.room_id);
|
||||||
let result = ctx.db.variables.get_user_variable(&key, name);
|
let result = ctx.db.variables.get_user_variable(&key, name);
|
||||||
|
|
||||||
let value = match result {
|
let value = match result {
|
||||||
|
@ -74,7 +74,7 @@ impl Command for SetVariableCommand {
|
||||||
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
||||||
let name = &self.0;
|
let name = &self.0;
|
||||||
let value = self.1;
|
let value = self.1;
|
||||||
let key = UserAndRoom(&ctx.username, ctx.room.room_id.as_str());
|
let key = UserAndRoom(&ctx.username, ctx.room_id);
|
||||||
let result = ctx.db.variables.set_user_variable(&key, name, value);
|
let result = ctx.db.variables.set_user_variable(&key, name, value);
|
||||||
|
|
||||||
let content = match result {
|
let content = match result {
|
||||||
|
@ -98,7 +98,7 @@ impl Command for DeleteVariableCommand {
|
||||||
|
|
||||||
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
async fn execute(&self, ctx: &Context<'_>) -> Execution {
|
||||||
let name = &self.0;
|
let name = &self.0;
|
||||||
let key = UserAndRoom(&ctx.username, ctx.room.room_id.as_str());
|
let key = UserAndRoom(&ctx.username, ctx.room_id);
|
||||||
let result = ctx.db.variables.delete_user_variable(&key, name);
|
let result = ctx.db.variables.delete_user_variable(&key, name);
|
||||||
|
|
||||||
let value = match result {
|
let value = match result {
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use crate::db::Database;
|
use crate::db::Database;
|
||||||
use matrix_sdk::Client;
|
use matrix_sdk::Client;
|
||||||
use matrix_sdk::Room;
|
|
||||||
|
|
||||||
/// 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.
|
||||||
|
@ -8,7 +7,7 @@ use matrix_sdk::Room;
|
||||||
pub struct Context<'a> {
|
pub struct Context<'a> {
|
||||||
pub db: Database,
|
pub db: Database,
|
||||||
pub matrix_client: &'a Client,
|
pub matrix_client: &'a Client,
|
||||||
pub room: &'a Room,
|
pub room_id: &'a str,
|
||||||
pub username: &'a str,
|
pub username: &'a str,
|
||||||
pub message_body: &'a str,
|
pub message_body: &'a str,
|
||||||
}
|
}
|
||||||
|
|
|
@ -343,14 +343,6 @@ mod tests {
|
||||||
use crate::db::Database;
|
use crate::db::Database;
|
||||||
use crate::parser::{Amount, Element, Operator};
|
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
|
/// Generate a series of numbers manually for testing. For this
|
||||||
/// die system, the first roll in the Vec should be the unit roll,
|
/// die system, the first roll in the Vec should be the unit roll,
|
||||||
/// and any subsequent rolls should be the tens place roll. The
|
/// and any subsequent rolls should be the tens place roll. The
|
||||||
|
@ -391,7 +383,7 @@ mod tests {
|
||||||
let ctx = Context {
|
let ctx = Context {
|
||||||
db: db,
|
db: db,
|
||||||
matrix_client: &matrix_sdk::Client::new("https://example.com").unwrap(),
|
matrix_client: &matrix_sdk::Client::new("https://example.com").unwrap(),
|
||||||
room: &dummy_room(),
|
room_id: "roomid",
|
||||||
username: "username",
|
username: "username",
|
||||||
message_body: "message",
|
message_body: "message",
|
||||||
};
|
};
|
||||||
|
|
|
@ -9,7 +9,7 @@ use futures::stream::{self, StreamExt, TryStreamExt};
|
||||||
//New hotness
|
//New hotness
|
||||||
pub async fn calculate_dice_amount(amounts: &[Amount], ctx: &Context<'_>) -> Result<i32, BotError> {
|
pub async fn calculate_dice_amount(amounts: &[Amount], ctx: &Context<'_>) -> Result<i32, BotError> {
|
||||||
let stream = stream::iter(amounts);
|
let stream = stream::iter(amounts);
|
||||||
let key = UserAndRoom(&ctx.username, ctx.room.room_id.as_str());
|
let key = UserAndRoom(&ctx.username, &ctx.room_id);
|
||||||
let variables = &ctx.db.variables.get_user_variables(&key)?;
|
let variables = &ctx.db.variables.get_user_variables(&key)?;
|
||||||
|
|
||||||
use DiceRollingError::VariableNotFound;
|
use DiceRollingError::VariableNotFound;
|
||||||
|
|
Loading…
Reference in New Issue