Admin room improvements

This commit is contained in:
Timo Kösters 2020-11-09 12:21:04 +01:00
parent 16b22bb432
commit 9f8cffcd22
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
18 changed files with 152 additions and 29 deletions

View file

@ -8,6 +8,7 @@ use serde::{de::DeserializeOwned, Serialize};
use sled::IVec;
use std::{collections::HashMap, convert::TryFrom};
#[derive(Clone)]
pub struct AccountData {
pub(super) roomuserdataid_accountdata: sled::Tree, // RoomUserDataId = Room + User + Count + Type
}

74
src/database/admin.rs Normal file
View file

@ -0,0 +1,74 @@
use std::convert::{TryFrom, TryInto};
use crate::{pdu::PduBuilder, Error};
use rocket::futures::{channel::mpsc, stream::StreamExt};
use ruma::{events::room::message, events::EventType, UserId};
use tokio::select;
pub enum AdminCommand {
SendTextMessage(message::TextMessageEventContent),
}
#[derive(Clone)]
pub struct Admin {
pub sender: mpsc::UnboundedSender<AdminCommand>,
}
impl Admin {
pub fn start_handler(
&self,
db: super::Database,
mut receiver: mpsc::UnboundedReceiver<AdminCommand>,
) {
tokio::spawn(async move {
// TODO: Use futures when we have long admin commands
//let mut futures = FuturesUnordered::new();
let conduit_user = UserId::try_from(format!("@conduit:{}", db.globals.server_name()))
.expect("@conduit:server_name is valid");
let conduit_room = db
.rooms
.id_from_alias(
&format!("#admins:{}", db.globals.server_name())
.try_into()
.expect("#admins:server_name is a valid room alias"),
)
.unwrap()
.ok_or_else(|| Error::BadConfig("Conduit instance does not have an #admins room."))
.unwrap();
loop {
select! {
Some(event) = receiver.next() => {
match event {
AdminCommand::SendTextMessage(message) => {
println!("{:?}", message);
db.rooms.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMessage,
content: serde_json::to_value(message).expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
redacts: None,
},
&conduit_user,
&conduit_room,
&db.globals,
&db.sending,
&db.admin,
&db.account_data,
).unwrap();
}
}
}
}
}
});
}
pub fn send(&self, command: AdminCommand) {
self.sender.unbounded_send(command).unwrap()
}
}

View file

@ -8,6 +8,7 @@ use ruma::{
};
use std::{collections::BTreeMap, convert::TryFrom};
#[derive(Clone)]
pub struct KeyBackups {
pub(super) backupid_algorithm: sled::Tree, // BackupId = UserId + Version(Count)
pub(super) backupid_etag: sled::Tree, // BackupId = UserId + Version(Count)

View file

@ -9,6 +9,7 @@ pub struct FileMeta {
pub file: Vec<u8>,
}
#[derive(Clone)]
pub struct Media {
pub(super) mediaid_file: sled::Tree, // MediaId = MXC + WidthHeight + Filename + ContentType
}

View file

@ -27,6 +27,8 @@ use std::{
sync::Arc,
};
use super::admin::AdminCommand;
/// The unique identifier of each state group.
///
/// This is created when a state group is added to the database by
@ -443,7 +445,7 @@ impl Rooms {
pdu_id: IVec,
globals: &super::globals::Globals,
account_data: &super::account_data::AccountData,
sending: &super::sending::Sending,
admin: &super::admin::Admin,
) -> Result<()> {
self.replace_pdu_leaves(&pdu.room_id, &pdu.event_id)?;
@ -514,28 +516,13 @@ impl Rooms {
if let Some(command) = parts.next() {
let args = parts.collect::<Vec<_>>();
self.build_and_append_pdu(
PduBuilder {
event_type: EventType::RoomMessage,
content: serde_json::to_value(
message::TextMessageEventContent {
body: format!("Command: {}, Args: {:?}", command, args),
formatted: None,
relates_to: None,
},
)
.expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
redacts: None,
admin.send(AdminCommand::SendTextMessage(
message::TextMessageEventContent {
body: format!("Command: {}, Args: {:?}", command, args),
formatted: None,
relates_to: None,
},
&UserId::try_from(format!("@conduit:{}", globals.server_name()))
.expect("@conduit:server_name is valid"),
&pdu.room_id,
&globals,
&sending,
&account_data,
)?;
));
}
}
}
@ -612,6 +599,7 @@ impl Rooms {
room_id: &RoomId,
globals: &super::globals::Globals,
sending: &super::sending::Sending,
admin: &super::admin::Admin,
account_data: &super::account_data::AccountData,
) -> Result<EventId> {
let PduBuilder {
@ -849,7 +837,7 @@ impl Rooms {
pdu_id.clone().into(),
globals,
account_data,
sending,
admin,
)?;
for server in self

View file

@ -8,6 +8,7 @@ use ruma::{api::federation, ServerName};
use sled::IVec;
use tokio::select;
#[derive(Clone)]
pub struct Sending {
/// The state for a given state hash.
pub(super) servernamepduids: sled::Tree, // ServernamePduId = ServerName + PduId
@ -54,7 +55,7 @@ impl Sending {
))
})
.filter_map(|r| r.ok())
.filter(|pdu| !pdu.is_empty()) // Skip reservation key
.filter(|(_, pdu)| !pdu.is_empty()) // Skip reservation key
.take(50)
// This should not contain more than 50 anyway
{

View file

@ -2,6 +2,7 @@ use crate::Result;
use ruma::{DeviceId, UserId};
use sled::IVec;
#[derive(Clone)]
pub struct TransactionIds {
pub(super) userdevicetxnid_response: sled::Tree, // Response can be empty (/sendToDevice) or the event id (/send)
}

View file

@ -7,6 +7,7 @@ use ruma::{
DeviceId, UserId,
};
#[derive(Clone)]
pub struct Uiaa {
pub(super) userdeviceid_uiaainfo: sled::Tree, // User-interactive authentication
}

View file

@ -14,6 +14,7 @@ use ruma::{
};
use std::{collections::BTreeMap, convert::TryFrom, mem, time::SystemTime};
#[derive(Clone)]
pub struct Users {
pub(super) userid_password: sled::Tree,
pub(super) userid_displayname: sled::Tree,