forked from projectmoon/tenebrous-dicebot
Centralize record_room_information function.
This commit is contained in:
parent
a65084e04a
commit
e177da9c25
|
@ -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]
|
||||
|
|
|
@ -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<UserId> = 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(()) => {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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.
|
||||
}
|
Loading…
Reference in New Issue