Skip to main content

tuwunel_admin/query/sending/
mod.rs

1mod active_requests;
2mod active_requests_for;
3mod get_latest_edu_count;
4mod queued_requests;
5
6use clap::Subcommand;
7use ruma::{OwnedServerName, OwnedUserId};
8use tuwunel_core::{Err, Result};
9use tuwunel_service::sending::Destination;
10
11use crate::admin_command_dispatch;
12
13#[admin_command_dispatch(handler_prefix = "sending")]
14#[derive(Debug, Subcommand)]
15/// All the getters and iterators from src/service/sending/
16pub(crate) enum SendingCommand {
17	/// - Queries database for all `servercurrentevent_data`
18	ActiveRequests,
19
20	/// - Queries database for `servercurrentevent_data` but for a specific
21	///   destination
22	///
23	/// This command takes only *one* format of these arguments:
24	///
25	/// appservice_id
26	/// server_name
27	/// user_id AND push_key
28	///
29	/// See src/service/sending/dest.rs for the definition of the `Destination`
30	/// enum
31	ActiveRequestsFor {
32		#[arg(short, long)]
33		appservice_id: Option<String>,
34		#[arg(short, long)]
35		server_name: Option<OwnedServerName>,
36		#[arg(short, long)]
37		user_id: Option<OwnedUserId>,
38		#[arg(short, long)]
39		push_key: Option<String>,
40	},
41
42	/// - Queries database for `servernameevent_data` which are the queued up
43	///   requests that will eventually be sent
44	///
45	/// This command takes only *one* format of these arguments:
46	///
47	/// appservice_id
48	/// server_name
49	/// user_id AND push_key
50	///
51	/// See src/service/sending/dest.rs for the definition of the `Destination`
52	/// enum
53	QueuedRequests {
54		#[arg(short, long)]
55		appservice_id: Option<String>,
56		#[arg(short, long)]
57		server_name: Option<OwnedServerName>,
58		#[arg(short, long)]
59		user_id: Option<OwnedUserId>,
60		#[arg(short, long)]
61		push_key: Option<String>,
62	},
63
64	GetLatestEduCount {
65		server_name: OwnedServerName,
66	},
67}
68
69fn parse_destination(
70	appservice_id: Option<String>,
71	server_name: Option<OwnedServerName>,
72	user_id: Option<OwnedUserId>,
73	push_key: Option<String>,
74) -> Result<Destination> {
75	if appservice_id.is_none() && server_name.is_none() && user_id.is_none() && push_key.is_none()
76	{
77		return Err!(
78			"An appservice ID, server name, or a user ID with push key must be specified via \
79			 arguments. See --help for more details.",
80		);
81	}
82
83	match (appservice_id, server_name, user_id, push_key) {
84		| (Some(appservice_id), None, None, None) => {
85			if appservice_id.is_empty() {
86				return Err!(
87					"An appservice ID, server name, or a user ID with push key must be \
88					 specified via arguments. See --help for more details.",
89				);
90			}
91
92			Ok(Destination::Appservice(appservice_id))
93		},
94		| (None, Some(server_name), None, None) => Ok(Destination::Federation(server_name)),
95		| (None, None, Some(user_id), Some(push_key)) => {
96			if push_key.is_empty() {
97				return Err!(
98					"An appservice ID, server name, or a user ID with push key must be \
99					 specified via arguments. See --help for more details.",
100				);
101			}
102
103			Ok(Destination::Push(user_id, push_key))
104		},
105		| (Some(_), Some(_), Some(_), Some(_)) => Err!(
106			"An appservice ID, server name, or a user ID with push key must be specified via \
107			 arguments. Not all of them See --help for more details.",
108		),
109		| _ => Err!(
110			"An appservice ID, server name, or a user ID with push key must be specified via \
111			 arguments. See --help for more details.",
112		),
113	}
114}