Functions for storing RoomInfo in db. Refactor bot joins room event.
continuous-integration/drone/push Build is passing Details

Add get/insert functions for RoomInfo in the rooms db.

Move 'bot joins room' code to single method, so we can also record a
RoomInfo struct into the database.
This commit is contained in:
projectmoon 2020-11-29 13:59:59 +00:00
parent 68db038336
commit 224f8cd0f1
3 changed files with 40 additions and 7 deletions

View File

@ -92,6 +92,21 @@ fn should_process_event(db: &Database, room_id: &str, event_id: &str) -> bool {
})
}
async fn record_users_in_room(
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;
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]
@ -124,12 +139,7 @@ impl EventEmitter for DiceBot {
self.db.rooms.clear_info(room_id)
} else if event_affects_us && adding_user {
info!("Joined room {}; recording user information", room_id);
let usernames = matrix::get_users_in_room(&self.client, &room.room_id).await;
usernames
.into_iter()
.filter(|username| username != &event.state_key)
.map(|username| self.db.rooms.add_user_to_room(&username, room_id))
.collect() //Make use of collect impl on Result.
record_users_in_room(&self.client, &self.db, &room, &event.state_key).await
} else if !event_affects_us && adding_user {
info!("Adding user {} to room ID {}", username, room_id);
self.db.rooms.add_user_to_room(username, room_id)

View File

@ -1,5 +1,6 @@
use crate::db::errors::DataError;
use crate::db::schema::convert_u64;
use crate::models::RoomInfo;
use byteorder::BigEndian;
use log::{debug, error, log_enabled};
use sled::transaction::TransactionalTree;
@ -14,7 +15,8 @@ use zerocopy::AsBytes;
#[derive(Clone)]
pub struct Rooms {
/// Room ID -> RoomInfo struct (single entries)
/// Room ID -> RoomInfo struct (single entries).
/// Key is just room ID as bytes.
pub(in crate::db) roomid_roominfo: Tree,
/// Room ID -> list of usernames in room.
@ -225,6 +227,24 @@ impl Rooms {
}
}
pub fn insert_room_info(&self, info: &RoomInfo) -> Result<(), DataError> {
let key = info.room_id.as_bytes();
let serialized = bincode::serialize(&info)?;
self.roomid_roominfo.insert(key, serialized)?;
Ok(())
}
pub fn get_room_info(&self, info: &RoomInfo) -> Result<Option<RoomInfo>, DataError> {
let key = info.room_id.as_bytes();
//swap/flatten Result<Option<Result>> down into the return type.
self.roomid_roominfo
.get(key)
.map(|bytes| bytes.map(|b| bincode::deserialize::<RoomInfo>(&b)))?
.transpose()
.map_err(|e| e.into())
}
pub fn get_rooms_for_user(&self, username: &str) -> Result<HashSet<String>, DataError> {
hashset_tree::get_set(&self.username_roomids, username.as_bytes())
}

View File

@ -1,4 +1,7 @@
use serde::{Deserialize, Serialize};
/// RoomInfo has basic metadata about a room: its name, ID, etc.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct RoomInfo {
pub room_id: String,
pub room_name: String,