From 224f8cd0f15f8ac06548f09cba729514f640885a Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sun, 29 Nov 2020 13:59:59 +0000 Subject: [PATCH] Functions for storing RoomInfo in db. Refactor bot joins room event. 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. --- src/bot/event_handlers.rs | 22 ++++++++++++++++------ src/db/rooms.rs | 22 +++++++++++++++++++++- src/models.rs | 3 +++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/bot/event_handlers.rs b/src/bot/event_handlers.rs index 0e22433..c44f6db 100644 --- a/src/bot/event_handlers.rs +++ b/src/bot/event_handlers.rs @@ -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) diff --git a/src/db/rooms.rs b/src/db/rooms.rs index 02a80ea..af6b1a2 100644 --- a/src/db/rooms.rs +++ b/src/db/rooms.rs @@ -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, DataError> { + let key = info.room_id.as_bytes(); + + //swap/flatten Result> down into the return type. + self.roomid_roominfo + .get(key) + .map(|bytes| bytes.map(|b| bincode::deserialize::(&b)))? + .transpose() + .map_err(|e| e.into()) + } + pub fn get_rooms_for_user(&self, username: &str) -> Result, DataError> { hashset_tree::get_set(&self.username_roomids, username.as_bytes()) } diff --git a/src/models.rs b/src/models.rs index 5d76f96..3040a80 100644 --- a/src/models.rs +++ b/src/models.rs @@ -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,