2024-05-09 15:59:08 -07:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
use conduit::{implement, utils::stream::TryIgnore, Result};
|
|
|
|
|
use database::Map;
|
|
|
|
|
use futures::{Stream, StreamExt};
|
|
|
|
|
use ruma::RoomId;
|
2020-05-03 17:25:31 +02:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
use crate::{rooms, Dep};
|
2024-07-18 06:37:47 +00:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
pub struct Service {
|
2024-06-28 22:51:39 +00:00
|
|
|
db: Data,
|
2024-08-08 17:18:30 +00:00
|
|
|
services: Services,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Data {
|
|
|
|
|
disabledroomids: Arc<Map>,
|
|
|
|
|
bannedroomids: Arc<Map>,
|
|
|
|
|
roomid_shortroomid: Arc<Map>,
|
|
|
|
|
pduid_pdu: Arc<Map>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Services {
|
|
|
|
|
short: Dep<rooms::short::Service>,
|
2022-06-25 16:12:23 +02:00
|
|
|
}
|
2021-08-12 23:04:00 +02:00
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
impl crate::Service for Service {
|
|
|
|
|
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
|
|
|
|
Ok(Arc::new(Self {
|
2024-08-08 17:18:30 +00:00
|
|
|
db: Data {
|
|
|
|
|
disabledroomids: args.db["disabledroomids"].clone(),
|
|
|
|
|
bannedroomids: args.db["bannedroomids"].clone(),
|
|
|
|
|
roomid_shortroomid: args.db["roomid_shortroomid"].clone(),
|
|
|
|
|
pduid_pdu: args.db["pduid_pdu"].clone(),
|
|
|
|
|
},
|
|
|
|
|
services: Services {
|
|
|
|
|
short: args.depend::<rooms::short::Service>("rooms::short"),
|
|
|
|
|
},
|
2024-07-04 03:26:19 +00:00
|
|
|
}))
|
2024-05-27 03:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
|
pub async fn exists(&self, room_id: &RoomId) -> bool {
|
|
|
|
|
let Ok(prefix) = self.services.short.get_shortroomid(room_id).await else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Look for PDUs in that room.
|
|
|
|
|
self.db
|
|
|
|
|
.pduid_pdu
|
|
|
|
|
.keys_raw_prefix(&prefix)
|
|
|
|
|
.ignore_err()
|
|
|
|
|
.next()
|
|
|
|
|
.await
|
|
|
|
|
.is_some()
|
|
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
|
pub fn iter_ids(&self) -> impl Stream<Item = &RoomId> + Send + '_ { self.db.roomid_shortroomid.keys().ignore_err() }
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn disable_room(&self, room_id: &RoomId, disabled: bool) {
|
|
|
|
|
if disabled {
|
|
|
|
|
self.db.disabledroomids.insert(room_id.as_bytes(), &[]);
|
|
|
|
|
} else {
|
|
|
|
|
self.db.disabledroomids.remove(room_id.as_bytes());
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn ban_room(&self, room_id: &RoomId, banned: bool) {
|
|
|
|
|
if banned {
|
|
|
|
|
self.db.bannedroomids.insert(room_id.as_bytes(), &[]);
|
|
|
|
|
} else {
|
|
|
|
|
self.db.bannedroomids.remove(room_id.as_bytes());
|
2022-10-05 18:36:12 +02:00
|
|
|
}
|
2024-08-08 17:18:30 +00:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
|
pub fn list_banned_rooms(&self) -> impl Stream<Item = &RoomId> + Send + '_ { self.db.bannedroomids.keys().ignore_err() }
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
|
#[inline]
|
2024-10-09 03:37:13 +00:00
|
|
|
pub async fn is_disabled(&self, room_id: &RoomId) -> bool { self.db.disabledroomids.get(room_id).await.is_ok() }
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
|
#[inline]
|
2024-10-09 03:37:13 +00:00
|
|
|
pub async fn is_banned(&self, room_id: &RoomId) -> bool { self.db.bannedroomids.get(room_id).await.is_ok() }
|