Skip to main content

tuwunel_admin/debug/
get_remote_pdu_list.rs

1use ruma::{EventId, OwnedServerName};
2use tuwunel_core::{Err, Result, utils::string::EMPTY, warn};
3
4use crate::admin_command;
5
6#[admin_command]
7pub(super) async fn get_remote_pdu_list(&self, server: OwnedServerName, force: bool) -> Result {
8	if !self.services.server.config.allow_federation {
9		return Err!("Federation is disabled on this homeserver.",);
10	}
11
12	if server == self.services.globals.server_name() {
13		return Err!(
14			"Not allowed to send federation requests to ourselves. Please use `get-pdu` for \
15			 fetching local PDUs from the database.",
16		);
17	}
18
19	if self.body.len() < 2
20		|| !self.body[0].trim().starts_with("```")
21		|| self.body.last().unwrap_or(&EMPTY).trim() != "```"
22	{
23		return Err!("Expected code block in command body. Add --help for details.",);
24	}
25
26	let list = self
27		.body
28		.iter()
29		.collect::<Vec<_>>()
30		.drain(1..self.body.len().saturating_sub(1))
31		.filter_map(|pdu| EventId::parse(pdu).ok())
32		.collect::<Vec<_>>();
33
34	let mut failed_count: usize = 0;
35	let mut success_count: usize = 0;
36
37	for event_id in list {
38		let result = self
39			.get_remote_pdu(event_id, server.clone())
40			.await;
41
42		if !force {
43			result?;
44		} else if let Err(e) = result {
45			warn!("Failed to get remote PDU, ignoring error: {e}");
46			failed_count = failed_count.saturating_add(1);
47			continue;
48		}
49
50		success_count = success_count.saturating_add(1);
51	}
52
53	let out =
54		format!("Fetched {success_count} remote PDUs successfully with {failed_count} failures");
55
56	self.write_str(&out).await
57}