Files
continuwuity/src/service/rooms/metadata/data.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
2.6 KiB
Rust
Raw Normal View History

2024-05-27 03:17:20 +00:00
use std::sync::Arc;
2024-06-28 22:51:39 +00:00
use conduit::{error, utils, Error, Result};
use database::{Database, Map};
2022-10-09 17:25:06 +02:00
use ruma::{OwnedRoomId, RoomId};
2022-09-06 23:15:09 +02:00
2024-06-28 22:51:39 +00:00
use crate::services;
2024-03-05 19:48:54 -05:00
2024-06-28 22:51:39 +00:00
pub(super) struct Data {
disabledroomids: Arc<Map>,
bannedroomids: Arc<Map>,
roomid_shortroomid: Arc<Map>,
pduid_pdu: Arc<Map>,
}
2024-05-26 21:29:19 +00:00
2024-05-27 03:17:20 +00:00
impl Data {
2024-06-28 22:51:39 +00:00
pub(super) fn new(db: &Arc<Database>) -> Self {
2024-05-27 03:17:20 +00:00
Self {
2024-06-28 22:51:39 +00:00
disabledroomids: db["disabledroomids"].clone(),
bannedroomids: db["bannedroomids"].clone(),
roomid_shortroomid: db["roomid_shortroomid"].clone(),
pduid_pdu: db["pduid_pdu"].clone(),
2024-05-27 03:17:20 +00:00
}
}
pub(super) fn exists(&self, room_id: &RoomId) -> Result<bool> {
2024-05-26 21:29:19 +00:00
let prefix = match services().rooms.short.get_shortroomid(room_id)? {
Some(b) => b.to_be_bytes().to_vec(),
None => return Ok(false),
};
// Look for PDUs in that room.
Ok(self
.pduid_pdu
.iter_from(&prefix, false)
.next()
.filter(|(k, _)| k.starts_with(&prefix))
.is_some())
}
2024-05-27 03:17:20 +00:00
pub(super) fn iter_ids<'a>(&'a self) -> Box<dyn Iterator<Item = Result<OwnedRoomId>> + 'a> {
2024-05-26 21:29:19 +00:00
Box::new(self.roomid_shortroomid.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 roomid_shortroomid is invalid."))
}))
}
2024-05-27 03:17:20 +00:00
pub(super) fn is_disabled(&self, room_id: &RoomId) -> Result<bool> {
2024-05-26 21:29:19 +00:00
Ok(self.disabledroomids.get(room_id.as_bytes())?.is_some())
}
2024-05-27 03:17:20 +00:00
pub(super) fn disable_room(&self, room_id: &RoomId, disabled: bool) -> Result<()> {
2024-05-26 21:29:19 +00:00
if disabled {
self.disabledroomids.insert(room_id.as_bytes(), &[])?;
} else {
self.disabledroomids.remove(room_id.as_bytes())?;
}
Ok(())
}
2024-05-27 03:17:20 +00:00
pub(super) fn is_banned(&self, room_id: &RoomId) -> Result<bool> {
Ok(self.bannedroomids.get(room_id.as_bytes())?.is_some())
}
2024-05-26 21:29:19 +00:00
2024-05-27 03:17:20 +00:00
pub(super) fn ban_room(&self, room_id: &RoomId, banned: bool) -> Result<()> {
2024-05-26 21:29:19 +00:00
if banned {
self.bannedroomids.insert(room_id.as_bytes(), &[])?;
} else {
self.bannedroomids.remove(room_id.as_bytes())?;
}
Ok(())
}
2024-05-27 03:17:20 +00:00
pub(super) fn list_banned_rooms<'a>(&'a self) -> Box<dyn Iterator<Item = Result<OwnedRoomId>> + 'a> {
2024-05-26 21:29:19 +00:00
Box::new(self.bannedroomids.iter().map(
|(room_id_bytes, _ /* non-banned rooms should not be in this table */)| {
let room_id = utils::string_from_bytes(&room_id_bytes)
.map_err(|e| {
error!("Invalid room_id bytes in bannedroomids: {e}");
Error::bad_database("Invalid room_id in bannedroomids.")
})?
.try_into()
.map_err(|e| {
error!("Invalid room_id in bannedroomids: {e}");
Error::bad_database("Invalid room_id in bannedroomids")
})?;
Ok(room_id)
},
))
}
}