From 2a611fbd49a01a7439c5452def370a8135c438ae Mon Sep 17 00:00:00 2001 From: Ginger Date: Wed, 4 Nov 2026 15:47:45 -0500 Subject: [PATCH] feat: Add support for MSC4322 --- Cargo.lock | 22 +++++++++--------- Cargo.toml | 5 +++-- src/api/client/media.rs | 23 ++++++++++++++++++- src/api/router.rs | 1 + src/api/server/media.rs | 4 ++++ src/service/media/data.rs | 31 +++++++++++++++++++++++--- src/service/media/mod.rs | 47 +++++++++++++++++++++++++++++++++++++-- 7 files changed, 114 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f604b2b51..f4d9fe03f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5049,7 +5049,7 @@ dependencies = [ [[package]] name = "ruma" version = "0.18.0" -source = "git+https://github.com/ruma/ruma.git?rev=d6621e1355419c5ef9a7c260b28ae1c0e6722b86#d6621e1355419c5ef9a7c260b28ae1c0e6722b86" +source = "git+https://github.com/gingershaped/ruwuma.git?rev=4eeece8f461b653d16f91064256080e8a959f584#4eeece8f461b653d16f91064256080e8a959f584" dependencies = [ "assign", "js_int", @@ -5068,7 +5068,7 @@ dependencies = [ [[package]] name = "ruma-appservice-api" version = "0.18.0" -source = "git+https://github.com/ruma/ruma.git?rev=d6621e1355419c5ef9a7c260b28ae1c0e6722b86#d6621e1355419c5ef9a7c260b28ae1c0e6722b86" +source = "git+https://github.com/gingershaped/ruwuma.git?rev=4eeece8f461b653d16f91064256080e8a959f584#4eeece8f461b653d16f91064256080e8a959f584" dependencies = [ "http", "js_int", @@ -5081,7 +5081,7 @@ dependencies = [ [[package]] name = "ruma-client-api" version = "0.26.0" -source = "git+https://github.com/ruma/ruma.git?rev=d6621e1355419c5ef9a7c260b28ae1c0e6722b86#d6621e1355419c5ef9a7c260b28ae1c0e6722b86" +source = "git+https://github.com/gingershaped/ruwuma.git?rev=4eeece8f461b653d16f91064256080e8a959f584#4eeece8f461b653d16f91064256080e8a959f584" dependencies = [ "as_variant", "assign", @@ -5103,7 +5103,7 @@ dependencies = [ [[package]] name = "ruma-common" version = "0.21.0" -source = "git+https://github.com/ruma/ruma.git?rev=d6621e1355419c5ef9a7c260b28ae1c0e6722b86#d6621e1355419c5ef9a7c260b28ae1c0e6722b86" +source = "git+https://github.com/gingershaped/ruwuma.git?rev=4eeece8f461b653d16f91064256080e8a959f584#4eeece8f461b653d16f91064256080e8a959f584" dependencies = [ "as_variant", "base64 0.23.1", @@ -5136,7 +5136,7 @@ dependencies = [ [[package]] name = "ruma-events" version = "0.36.0" -source = "git+https://github.com/ruma/ruma.git?rev=d6621e1355419c5ef9a7c260b28ae1c0e6722b86#d6621e1355419c5ef9a7c260b28ae1c0e6722b86" +source = "git+https://github.com/gingershaped/ruwuma.git?rev=4eeece8f461b653d16f91064256080e8a959f584#4eeece8f461b653d16f91064256080e8a959f584" dependencies = [ "as_variant", "indexmap 2.14.2", @@ -5157,7 +5157,7 @@ dependencies = [ [[package]] name = "ruma-federation-api" version = "0.17.0" -source = "git+https://github.com/ruma/ruma.git?rev=d6621e1355419c5ef9a7c260b28ae1c0e6722b86#d6621e1355419c5ef9a7c260b28ae1c0e6722b86" +source = "git+https://github.com/gingershaped/ruwuma.git?rev=4eeece8f461b653d16f91064256080e8a959f584#4eeece8f461b653d16f91064256080e8a959f584" dependencies = [ "bytes", "http", @@ -5179,7 +5179,7 @@ dependencies = [ [[package]] name = "ruma-identifiers-validation" version = "0.14.1" -source = "git+https://github.com/ruma/ruma.git?rev=d6621e1355419c5ef9a7c260b28ae1c0e6722b86#d6621e1355419c5ef9a7c260b28ae1c0e6722b86" +source = "git+https://github.com/gingershaped/ruwuma.git?rev=4eeece8f461b653d16f91064256080e8a959f584#4eeece8f461b653d16f91064256080e8a959f584" dependencies = [ "js_int", "thiserror 2.0.21", @@ -5188,7 +5188,7 @@ dependencies = [ [[package]] name = "ruma-macros" version = "0.20.3" -source = "git+https://github.com/ruma/ruma.git?rev=d6621e1355419c5ef9a7c260b28ae1c0e6722b86#d6621e1355419c5ef9a7c260b28ae1c0e6722b86" +source = "git+https://github.com/gingershaped/ruwuma.git?rev=4eeece8f461b653d16f91064256080e8a959f584#4eeece8f461b653d16f91064256080e8a959f584" dependencies = [ "as_variant", "cfg-if", @@ -5204,7 +5204,7 @@ dependencies = [ [[package]] name = "ruma-push-gateway-api" version = "0.17.0" -source = "git+https://github.com/ruma/ruma.git?rev=d6621e1355419c5ef9a7c260b28ae1c0e6722b86#d6621e1355419c5ef9a7c260b28ae1c0e6722b86" +source = "git+https://github.com/gingershaped/ruwuma.git?rev=4eeece8f461b653d16f91064256080e8a959f584#4eeece8f461b653d16f91064256080e8a959f584" dependencies = [ "js_int", "ruma-common", @@ -5216,7 +5216,7 @@ dependencies = [ [[package]] name = "ruma-signatures" version = "0.23.0" -source = "git+https://github.com/ruma/ruma.git?rev=d6621e1355419c5ef9a7c260b28ae1c0e6722b86#d6621e1355419c5ef9a7c260b28ae1c0e6722b86" +source = "git+https://github.com/gingershaped/ruwuma.git?rev=4eeece8f461b653d16f91064256080e8a959f584#4eeece8f461b653d16f91064256080e8a959f584" dependencies = [ "base64 0.23.1", "ed25519-dalek", @@ -5233,7 +5233,7 @@ dependencies = [ [[package]] name = "ruma-state-res" version = "0.19.0" -source = "git+https://github.com/ruma/ruma.git?rev=d6621e1355419c5ef9a7c260b28ae1c0e6722b86#d6621e1355419c5ef9a7c260b28ae1c0e6722b86" +source = "git+https://github.com/gingershaped/ruwuma.git?rev=4eeece8f461b653d16f91064256080e8a959f584#4eeece8f461b653d16f91064256080e8a959f584" dependencies = [ "js_int", "ruma-common", diff --git a/Cargo.toml b/Cargo.toml index 6926aa125..3f8e00fd3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -356,8 +356,8 @@ version = "1.1.1" # Used for matrix spec type definitions and helpers [workspace.dependencies.ruma] -git = "https://github.com/ruma/ruma.git" -rev = "d6621e1355419c5ef9a7c260b28ae1c0e6722b86" +git = "https://github.com/gingershaped/ruwuma.git" +rev = "4eeece8f461b653d16f91064256080e8a959f584" features = [ "appservice-api-c", "client-api", @@ -390,6 +390,7 @@ features = [ "unstable-msc4363", "unstable-msc4406", "unstable-msc4439", + "unstable-msc4322", "unstable-msc4354", "unstable-msc4388", "unstable-msc4480", diff --git a/src/api/client/media.rs b/src/api/client/media.rs index a078bb968..79e9a4bb9 100644 --- a/src/api/client/media.rs +++ b/src/api/client/media.rs @@ -18,7 +18,7 @@ use ruma::{ get_content, get_content_as_filename, get_content_thumbnail, get_media_config, get_media_preview, }, - media::create_content, + media::{self, create_content}, }, }; use service::media::mxc::Mxc; @@ -240,6 +240,27 @@ pub(crate) async fn get_media_preview_route( }) } +/// # `POST /_matrix/client/unstable/uk.timedout.msc4322/media/redact/{server_name}/{media_id}` +pub(crate) async fn unstable_redact_media_route( + State(services): State, + body: Ruma, +) -> Result { + let user = body.identity.expect_sender_user()?; + + let mxc = Mxc { + server_name: &body.server_name, + media_id: &body.media_id, + }; + + if !(services.media.user_owns(user, &mxc).await || services.admin.user_is_admin(user).await) { + return Err!(Request(Forbidden("You cannot redact this media."))); + } + + services.media.redact(&mxc).await?; + + Ok(media::redact::msc4322::Response::new()) +} + async fn fetch_thumbnail_meta( services: &Services, mxc: &Mxc<'_>, diff --git a/src/api/router.rs b/src/api/router.rs index 0eff6408c..6c07a256c 100644 --- a/src/api/router.rs +++ b/src/api/router.rs @@ -158,6 +158,7 @@ pub fn build(router: Router, state: State) -> Router { .ruma_route(&client::get_content_as_filename_route) .ruma_route(&client::get_media_preview_route) .ruma_route(&client::get_media_config_route) + .ruma_route(&client::unstable_redact_media_route) .ruma_route(&client::get_devices_route) .ruma_route(&client::get_device_route) .ruma_route(&client::update_device_route) diff --git a/src/api/server/media.rs b/src/api/server/media.rs index 98b3ae7ed..eb8aa6c02 100644 --- a/src/api/server/media.rs +++ b/src/api/server/media.rs @@ -38,6 +38,10 @@ pub(crate) async fn get_content_route( content_disposition, ); + services + .media + .track_remote_download(&body.identity, &body.media_id); + Ok(get_content::v1::Response::new( ContentMetadata::new(), FileOrLocation::File(content), diff --git a/src/service/media/data.rs b/src/service/media/data.rs index bc20ee0b8..5eb4a0107 100644 --- a/src/service/media/data.rs +++ b/src/service/media/data.rs @@ -1,12 +1,15 @@ -use std::{sync::Arc, time::Duration}; +use std::{collections::HashSet, sync::Arc, time::Duration}; use conduwuit::{ - Err, Result, debug, debug_info, err, + Err, Result, at, debug, debug_info, err, utils::{ReadyExt, str_from_bytes, stream::TryIgnore, string_from_bytes}, }; use database::{Database, Interfix, Map}; use futures::StreamExt; -use ruma::{OwnedMxcUri, OwnedUserId, UserId, http_headers::ContentDisposition}; +use ruma::{ + OwnedMxcUri, OwnedServerName, OwnedUserId, ServerName, UserId, + http_headers::ContentDisposition, +}; use super::{preview::UrlPreviewData, thumbnail::Dim}; use crate::media::mxc::Mxc; @@ -15,6 +18,8 @@ pub(crate) struct Data { mediaid_file: Arc, mediaid_user: Arc, url_previews: Arc, + mediaidremote_remotedownload: Arc, + mediaid_redacted: Arc, } #[derive(Debug)] @@ -30,6 +35,8 @@ impl Data { mediaid_file: db["mediaid_file"].clone(), mediaid_user: db["mediaid_user"].clone(), url_previews: db["url_previews"].clone(), + mediaidremote_remotedownload: db["mediaidremote_remotedownload"].clone(), + mediaid_redacted: db["mediaid_redacted"].clone(), } } @@ -330,4 +337,22 @@ impl Data { * I'll do it later. */ }) } + + pub(super) fn track_remote_download(&self, remote: &ServerName, media_id: &str) { + self.mediaidremote_remotedownload + .put_raw((media_id, remote), []); + } + + pub(super) async fn find_remote_downloads(&self, media_id: &str) -> HashSet { + self.mediaidremote_remotedownload + .keys_prefix::<(String, OwnedServerName), _>(media_id) + .ignore_err() + .map(at!(1)) + .collect() + .await + } + + pub(super) fn mark_redacted(&self, media_id: &str) { + self.mediaid_redacted.insert(media_id, []); + } } diff --git a/src/service/media/mod.rs b/src/service/media/mod.rs index d7021be01..f8c16affa 100644 --- a/src/service/media/mod.rs +++ b/src/service/media/mod.rs @@ -5,7 +5,7 @@ mod preview; mod remote; mod tests; mod thumbnail; -use std::{path::PathBuf, sync::Arc, time::SystemTime}; +use std::{collections::HashSet, path::PathBuf, sync::Arc, time::SystemTime}; use async_trait::async_trait; use base64::{Engine as _, engine::general_purpose}; @@ -19,7 +19,11 @@ use conduwuit::{ }, warn, }; -use ruma::{OwnedMxcUri, UserId, http_headers::ContentDisposition}; +use ruma::{ + OwnedMxcUri, OwnedServerName, ServerName, UserId, + api::federation::transactions::edu::{Edu, MediaRedactionContent}, + http_headers::ContentDisposition, +}; use tokio::{ fs, io::{AsyncReadExt, AsyncWriteExt, BufReader}, @@ -171,6 +175,31 @@ impl Service { } } + /// Mark a media file as redacted and delete it from the database. + pub async fn redact(&self, mxc: &Mxc<'_>) -> Result<()> { + self.db.mark_redacted(mxc.media_id); + self.delete(mxc).await?; + + let edu = Edu::MediaRedaction(MediaRedactionContent::new(mxc.media_id.to_owned())); + + for server in self.find_remote_downloads(mxc.media_id).await { + if let Err(err) = self.services.sending.send_edu_server(&server, &edu) { + error!(?server, %mxc, ?err, "Failed to queue media redaction EDU"); + } + } + + Ok(()) + } + + /// Check if a user uploaded a file with the given MXC. + pub async fn user_owns(&self, user: &UserId, mxc: &Mxc<'_>) -> bool { + // TODO: do this more efficiently when we refactor this garbage fire of + // a service + + let mxc = OwnedMxcUri::from(mxc.to_string()); + self.db.get_all_user_mxcs(user).await.contains(&mxc) + } + /// Deletes all media by the specified user /// /// currently, this is only practical for local users @@ -263,6 +292,20 @@ impl Service { Ok(mxcs) } + /// Mark a remote server as having downloaded a media file. + pub fn track_remote_download(&self, remote: &ServerName, media_id: &str) { + self.db.track_remote_download(remote, media_id); + } + + /// Find all remote servers that have downloaded the local media file with + /// the given ID. + pub fn find_remote_downloads( + &self, + media_id: &str, + ) -> impl Future> { + self.db.find_remote_downloads(media_id) + } + /// Deletes all media files in the given time frame. /// Returns a usize with the amount of media files deleted. pub async fn delete_all_media_within_timeframe(