2022-10-09 17:25:06 +02:00
|
|
|
use ruma::{OwnedRoomId, RoomId};
|
2022-09-06 23:15:09 +02:00
|
|
|
|
2024-05-26 21:29:19 +00:00
|
|
|
use crate::{utils, Error, KeyValueDatabase, Result};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
pub trait Data: Send + Sync {
|
2022-06-25 16:12:23 +02:00
|
|
|
/// Adds the room to the public room directory
|
2022-09-07 13:25:51 +02:00
|
|
|
fn set_public(&self, room_id: &RoomId) -> Result<()>;
|
2022-06-25 16:12:23 +02:00
|
|
|
|
|
|
|
|
/// Removes the room from the public room directory.
|
2022-09-07 13:25:51 +02:00
|
|
|
fn set_not_public(&self, room_id: &RoomId) -> Result<()>;
|
2022-06-25 16:12:23 +02:00
|
|
|
|
|
|
|
|
/// Returns true if the room is in the public room directory.
|
2022-09-07 13:25:51 +02:00
|
|
|
fn is_public_room(&self, room_id: &RoomId) -> Result<bool>;
|
2022-06-25 16:12:23 +02:00
|
|
|
|
|
|
|
|
/// Returns the unsorted public room directory
|
2022-10-09 17:25:06 +02:00
|
|
|
fn public_rooms<'a>(&'a self) -> Box<dyn Iterator<Item = Result<OwnedRoomId>> + 'a>;
|
2022-06-25 16:12:23 +02:00
|
|
|
}
|
2024-05-26 21:29:19 +00:00
|
|
|
|
|
|
|
|
impl Data for KeyValueDatabase {
|
|
|
|
|
fn set_public(&self, room_id: &RoomId) -> Result<()> { self.publicroomids.insert(room_id.as_bytes(), &[]) }
|
|
|
|
|
|
|
|
|
|
fn set_not_public(&self, room_id: &RoomId) -> Result<()> { self.publicroomids.remove(room_id.as_bytes()) }
|
|
|
|
|
|
|
|
|
|
fn is_public_room(&self, room_id: &RoomId) -> Result<bool> {
|
|
|
|
|
Ok(self.publicroomids.get(room_id.as_bytes())?.is_some())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn public_rooms<'a>(&'a self) -> Box<dyn Iterator<Item = Result<OwnedRoomId>> + 'a> {
|
|
|
|
|
Box::new(self.publicroomids.iter().map(|(bytes, _)| {
|
|
|
|
|
RoomId::parse(
|
|
|
|
|
utils::string_from_bytes(&bytes)
|
|
|
|
|
.map_err(|_| Error::bad_database("Room ID in publicroomids is invalid unicode."))?,
|
|
|
|
|
)
|
|
|
|
|
.map_err(|_| Error::bad_database("Room ID in publicroomids is invalid."))
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
}
|