2021-05-15 23:45:26 +00:00
|
|
|
use crate::db::sqlite::errors::DataError;
|
|
|
|
use crate::db::sqlite::Rooms;
|
2021-05-16 21:39:19 +00:00
|
|
|
use crate::error::BotError;
|
2020-11-30 19:53:26 +00:00
|
|
|
use crate::matrix;
|
|
|
|
use crate::models::RoomInfo;
|
2021-05-15 23:45:26 +00:00
|
|
|
use futures::stream::{self, StreamExt, TryStreamExt};
|
2021-01-29 22:35:07 +00:00
|
|
|
use matrix_sdk::{self, identifiers::RoomId, Client};
|
2020-11-30 19:53:26 +00:00
|
|
|
|
|
|
|
/// Record the information about a room, including users in it.
|
|
|
|
pub async fn record_room_information(
|
|
|
|
client: &Client,
|
2021-05-15 23:45:26 +00:00
|
|
|
db: &crate::db::sqlite::Database,
|
2021-01-29 22:35:07 +00:00
|
|
|
room_id: &RoomId,
|
|
|
|
room_display_name: &str,
|
2020-11-30 19:53:26 +00:00
|
|
|
our_username: &str,
|
2021-05-16 21:39:19 +00:00
|
|
|
) -> Result<(), BotError> {
|
|
|
|
//Clear out any old room info first.
|
|
|
|
db.clear_info(room_id.as_str()).await?;
|
|
|
|
|
2021-01-29 22:35:07 +00:00
|
|
|
let room_id_str = room_id.as_str();
|
2021-05-16 21:39:19 +00:00
|
|
|
let usernames = matrix::get_users_in_room(&client, &room_id).await?;
|
2020-11-30 19:53:26 +00:00
|
|
|
|
|
|
|
let info = RoomInfo {
|
|
|
|
room_id: room_id_str.to_owned(),
|
2021-01-29 22:35:07 +00:00
|
|
|
room_name: room_display_name.to_owned(),
|
2020-11-30 19:53:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO this and the username adding should be one whole
|
|
|
|
// transaction in the db.
|
2021-05-15 23:45:26 +00:00
|
|
|
db.insert_room_info(&info).await?;
|
2020-11-30 19:53:26 +00:00
|
|
|
|
2021-05-15 23:45:26 +00:00
|
|
|
let filtered_usernames = usernames
|
2020-11-30 19:53:26 +00:00
|
|
|
.into_iter()
|
2021-05-15 23:45:26 +00:00
|
|
|
.filter(|username| username != our_username);
|
|
|
|
|
2021-05-16 21:39:19 +00:00
|
|
|
println!("Users to add to room: {:?}", filtered_usernames);
|
|
|
|
|
2021-05-15 23:45:26 +00:00
|
|
|
// Async collect into vec of results, then use into_iter of result
|
|
|
|
// to go to from Result<Vec<()>> to just Result<()>. Easier than
|
|
|
|
// attempting to async-collect our way to a single Result<()>.
|
|
|
|
stream::iter(filtered_usernames)
|
2021-05-16 21:39:19 +00:00
|
|
|
.then(|username| async move {
|
|
|
|
db.add_user_to_room(&username, &room_id_str)
|
|
|
|
.await
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
})
|
|
|
|
.collect::<Vec<Result<(), BotError>>>()
|
2021-05-15 23:45:26 +00:00
|
|
|
.await
|
|
|
|
.into_iter()
|
|
|
|
.collect()
|
2020-11-30 19:53:26 +00:00
|
|
|
}
|