Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split admin module #229

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/database/abstraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub(crate) trait KeyValueDatabaseEngine: Send + Sync {
fn backup(&self) -> Result<(), Box<dyn Error>> { unimplemented!() }

fn backup_list(&self) -> Result<String> { Ok(String::new()) }

fn file_list(&self) -> Result<String> { Ok(String::new()) }
}

pub(crate) trait KvTree: Send + Sync {
Expand Down
24 changes: 24 additions & 0 deletions src/database/abstraction/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,30 @@ impl KeyValueDatabaseEngine for Arc<Engine> {
Ok(res)
}

fn file_list(&self) -> Result<String> {
match self.rocks.live_files() {
Err(e) => Ok(String::from(e)),
Ok(files) => {
let mut res = String::new();
for file in files {
let _ = std::fmt::write(
&mut res,
format_args!(
"<code>L{} {:<13} {:7}+ {:4}- {:9}</code> {}<br>",
file.level,
file.name,
file.num_entries,
file.num_deletions,
file.size,
file.column_family_name,
),
);
}
Ok(res)
},
}
}

// TODO: figure out if this is needed for rocksdb
#[allow(dead_code)]
fn clear_caches(&self) {}
Expand Down
2 changes: 2 additions & 0 deletions src/database/key_value/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,6 @@ lasttimelinecount_cache: {lasttimelinecount_cache}\n"
fn backup(&self) -> Result<(), Box<dyn std::error::Error>> { self.db.backup() }

fn backup_list(&self) -> Result<String> { self.db.backup_list() }

fn file_list(&self) -> Result<String> { self.db.file_list() }
}
92 changes: 92 additions & 0 deletions src/service/admin/appservice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use clap::Subcommand;
use ruma::{api::appservice::Registration, events::room::message::RoomMessageEventContent};

use crate::{service::admin::escape_html, services, Result};

#[cfg_attr(test, derive(Debug))]
#[derive(Subcommand)]
pub(crate) enum AppserviceCommand {
/// - Register an appservice using its registration YAML
///
/// This command needs a YAML generated by an appservice (such as a bridge),
/// which must be provided in a Markdown code block below the command.
///
/// Registering a new bridge using the ID of an existing bridge will replace
/// the old one.
Register,

/// - Unregister an appservice using its ID
///
/// You can find the ID using the `list-appservices` command.
Unregister {
/// The appservice to unregister
appservice_identifier: String,
},

/// - Show an appservice's config using its ID
///
/// You can find the ID using the `list-appservices` command.
Show {
/// The appservice to show
appservice_identifier: String,
},

/// - List all the currently registered appservices
List,
}

pub(crate) async fn process(command: AppserviceCommand, body: Vec<&str>) -> Result<RoomMessageEventContent> {
match command {
AppserviceCommand::Register => {
if body.len() > 2 && body[0].trim().starts_with("```") && body.last().unwrap().trim() == "```" {
let appservice_config = body[1..body.len() - 1].join("\n");
let parsed_config = serde_yaml::from_str::<Registration>(&appservice_config);
match parsed_config {
Ok(yaml) => match services().appservice.register_appservice(yaml).await {
Ok(id) => Ok(RoomMessageEventContent::text_plain(format!(
"Appservice registered with ID: {id}."
))),
Err(e) => Ok(RoomMessageEventContent::text_plain(format!(
"Failed to register appservice: {e}"
))),
},
Err(e) => Ok(RoomMessageEventContent::text_plain(format!(
"Could not parse appservice config: {e}"
))),
}
} else {
Ok(RoomMessageEventContent::text_plain(
"Expected code block in command body. Add --help for details.",
))
}
},
AppserviceCommand::Unregister {
appservice_identifier,
} => match services().appservice.unregister_appservice(&appservice_identifier).await {
Ok(()) => Ok(RoomMessageEventContent::text_plain("Appservice unregistered.")),
Err(e) => Ok(RoomMessageEventContent::text_plain(format!(
"Failed to unregister appservice: {e}"
))),
},
AppserviceCommand::Show {
appservice_identifier,
} => match services().appservice.get_registration(&appservice_identifier).await {
Some(config) => {
let config_str = serde_yaml::to_string(&config).expect("config should've been validated on register");
let output = format!("Config for {}:\n\n```yaml\n{}\n```", appservice_identifier, config_str,);
let output_html = format!(
"Config for {}:\n\n<pre><code class=\"language-yaml\">{}</code></pre>",
escape_html(&appservice_identifier),
escape_html(&config_str),
);
Ok(RoomMessageEventContent::text_html(output, output_html))
},
None => Ok(RoomMessageEventContent::text_plain("Appservice does not exist.")),
},
AppserviceCommand::List => {
let appservices = services().appservice.iter_ids().await;
let output = format!("Appservices ({}): {}", appservices.len(), appservices.join(", "));
Ok(RoomMessageEventContent::text_plain(output))
},
}
}
Loading
Loading