Record room information (ID, name) in the database #48

Manually merged
projectmoon merged 10 commits from record-room-info into master 2020-11-30 20:16:01 +00:00
3 changed files with 40 additions and 7 deletions
Showing only changes of commit 224f8cd0f1 - Show all commits

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. /// This event emitter listens for messages with dice rolling commands.
/// Originally adapted from the matrix-rust-sdk examples. /// Originally adapted from the matrix-rust-sdk examples.
#[async_trait] #[async_trait]
@ -124,12 +139,7 @@ impl EventEmitter for DiceBot {
self.db.rooms.clear_info(room_id) self.db.rooms.clear_info(room_id)
} else if event_affects_us && adding_user { } else if event_affects_us && adding_user {
info!("Joined room {}; recording user information", room_id); info!("Joined room {}; recording user information", room_id);
let usernames = matrix::get_users_in_room(&self.client, &room.room_id).await; record_users_in_room(&self.client, &self.db, &room, &event.state_key).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.
} else if !event_affects_us && adding_user { } else if !event_affects_us && adding_user {
info!("Adding user {} to room ID {}", username, room_id); info!("Adding user {} to room ID {}", username, room_id);
self.db.rooms.add_user_to_room(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::errors::DataError;
use crate::db::schema::convert_u64; use crate::db::schema::convert_u64;
use crate::models::RoomInfo;
use byteorder::BigEndian; use byteorder::BigEndian;
use log::{debug, error, log_enabled}; use log::{debug, error, log_enabled};
use sled::transaction::TransactionalTree; use sled::transaction::TransactionalTree;
@ -14,7 +15,8 @@ use zerocopy::AsBytes;
#[derive(Clone)] #[derive(Clone)]
pub struct Rooms { 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, pub(in crate::db) roomid_roominfo: Tree,
/// Room ID -> list of usernames in room. /// 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> { pub fn get_rooms_for_user(&self, username: &str) -> Result<HashSet<String>, DataError> {
hashset_tree::get_set(&self.username_roomids, username.as_bytes()) 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. /// RoomInfo has basic metadata about a room: its name, ID, etc.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct RoomInfo { pub struct RoomInfo {
pub room_id: String, pub room_id: String,
pub room_name: String, pub room_name: String,