Skip to main content

tuwunel_admin/appservice/
commands.rs

1use futures::StreamExt;
2use ruma::api::appservice::Registration;
3use tuwunel_core::{Err, Result, checked, err};
4
5use crate::admin_command;
6
7#[admin_command]
8pub(super) async fn appservice_register(&self) -> Result {
9	let body = &self.body;
10	let body_len = self.body.len();
11	if body_len < 2
12		|| !body[0].trim().starts_with("```")
13		|| body.last().unwrap_or(&"").trim() != "```"
14	{
15		return Err!("Expected code block in command body. Add --help for details.");
16	}
17
18	let range = 1..checked!(body_len - 1)?;
19	let appservice_config_body = body[range].join("\n");
20
21	let registration: Registration = serde_yaml::from_str(&appservice_config_body)
22		.map_err(|e| err!("Could not parse appservice config as YAML: {e}"))?;
23
24	let id = registration.id.clone();
25
26	self.services
27		.appservice
28		.register_appservice(registration)
29		.await
30		.map_err(|e| err!("Failed to register appservice: {e}"))?;
31
32	self.write_string(format!("Appservice registered with ID: {id}"))
33		.await
34}
35
36#[admin_command]
37pub(super) async fn appservice_unregister(&self, appservice_identifier: String) -> Result {
38	self.services
39		.appservice
40		.unregister_appservice(&appservice_identifier)
41		.await
42		.map_err(|e| err!("Failed to unregister appservice: {e}"))?;
43
44	self.write_str("Appservice unregistered.").await
45}
46
47#[admin_command]
48pub(super) async fn appservice_show_config(&self, appservice_identifier: String) -> Result {
49	let config = self
50		.services
51		.appservice
52		.get_registration(&appservice_identifier)
53		.await
54		.ok_or(err!("Appservice does not exist."))?;
55
56	let config_str = serde_yaml::to_string(&config)?;
57
58	self.write_str(&format!("Config for {appservice_identifier}:\n\n```yaml\n{config_str}\n```"))
59		.await
60}
61
62#[admin_command]
63pub(super) async fn appservice_list(&self) -> Result {
64	let appservices: Vec<_> = self
65		.services
66		.appservice
67		.iter_ids()
68		.collect()
69		.await;
70
71	let len = appservices.len();
72	let list = appservices.join(", ");
73	self.write_str(&format!("Appservices ({len}): {list}"))
74		.await
75}