Skip to main content

tuwunel_admin/media/
delete_by_event.rs

1use ruma::{CanonicalJsonObject, CanonicalJsonValue, Mxc, OwnedEventId};
2use tuwunel_core::{
3	Err, Result, debug, err, info,
4	utils::{BoolExt, to_canonical_object},
5	warn,
6};
7use tuwunel_matrix::Event;
8
9use crate::admin_command;
10
11#[admin_command]
12pub(super) async fn delete_by_event(&self, event_id: OwnedEventId) -> Result {
13	let event = self
14		.services
15		.timeline
16		.get_pdu(&event_id)
17		.await
18		.map_err(|error| match error.is_not_found() {
19			| true => err!("Event ID does not exist or is not known to us."),
20			| false => error,
21		})?;
22
23	let event_json = event
24		.is_redacted()
25		.then_async(async || {
26			self.services
27				.retention
28				.get_original_pdu_json(&event_id)
29				.await
30				.map_err(|error| match error.is_not_found() {
31					| true => err!("Redacted event original unavailable."),
32					| false => error,
33				})
34		})
35		.await
36		.unwrap_or_else(|| to_canonical_object(event).map_err(Into::into))?;
37
38	let content = event_json
39		.get("content")
40		.and_then(CanonicalJsonValue::as_object)
41		.ok_or_else(|| {
42			err!(
43				"Event ID does not have a \"content\" key, this is not a message or an event \
44				 type that contains media.",
45			)
46		})?;
47
48	let mut mxc_urls = Vec::with_capacity(3);
49	mxc_urls.extend(url_mxc_from_content(content));
50	mxc_urls.extend(thumbnail_mxc_from_content(content));
51	mxc_urls.extend(file_mxc_from_content(content));
52
53	if mxc_urls.is_empty() {
54		return Err!("Parsed event ID but found no MXC URLs.",);
55	}
56
57	let mut mxc_deletion_count: usize = 0;
58
59	for mxc_url in mxc_urls {
60		if !mxc_url.starts_with("mxc://") {
61			warn!("Ignoring non-mxc url {mxc_url}");
62			continue;
63		}
64
65		let mxc: Mxc<'_> = mxc_url.as_str().try_into()?;
66
67		match self.services.media.delete(&mxc).await {
68			| Ok(()) => {
69				info!("Successfully deleted {mxc_url} from filesystem and database");
70				mxc_deletion_count = mxc_deletion_count.saturating_add(1);
71			},
72			| Err(e) => {
73				warn!("Failed to delete {mxc_url}, ignoring error and skipping: {e}");
74			},
75		}
76	}
77
78	write!(
79		self,
80		"Deleted {mxc_deletion_count} total MXCs from our database and the filesystem from \
81		 event ID {event_id}."
82	)
83	.await
84}
85
86fn url_mxc_from_content(content: &CanonicalJsonObject) -> Option<String> {
87	debug!("Attempting to go into \"url\" key for main media file");
88	let url = content
89		.get("url")
90		.and_then(CanonicalJsonValue::as_str)?;
91
92	debug!("Got main media URL: {url}");
93	Some(url.to_owned())
94}
95
96fn thumbnail_mxc_from_content(content: &CanonicalJsonObject) -> Option<String> {
97	debug!("Attempting to go into \"info\" key for thumbnails");
98	let thumbnail_url = content
99		.get("info")
100		.and_then(CanonicalJsonValue::as_object)
101		.and_then(|info| info.get("thumbnail_url"))
102		.and_then(CanonicalJsonValue::as_str)?;
103
104	debug!("Found a thumbnail_url in info key: {thumbnail_url}");
105	Some(thumbnail_url.to_owned())
106}
107
108fn file_mxc_from_content(content: &CanonicalJsonObject) -> Option<String> {
109	debug!("Attempting to go into \"file\" key");
110	let url = content
111		.get("file")
112		.and_then(CanonicalJsonValue::as_object)
113		.and_then(|file| file.get("url"))
114		.and_then(CanonicalJsonValue::as_str)?;
115
116	debug!("Found url in file key: {url}");
117	Some(url.to_owned())
118}