Compare commits

...
Author SHA1 Message Date
Ginger b08a47e035 chore: News fragment 2026-12-03 09:27:52 -05:00
Ginger 2a611fbd49 feat: Add support for MSC4322 2026-12-03 09:27:52 -05:00
Ginger 8fb898c84d refactor(sending): Stop passing byte buffers(?!) to EDU send methods 2026-12-03 09:19:17 -05:00
12 changed files with 162 additions and 80 deletions

No files matched your search

Generated
+11 -11
View File
@@ -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",
+3 -2
View File
@@ -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",
+3
View File
@@ -0,0 +1,3 @@
Added unstable support for [MSC4322] media redaction. Contributed by @ginger.
[MSC4322]: https://github.com/matrix-org/matrix-spec-proposals/pull/4322
+22 -1
View File
@@ -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<crate::State>,
body: Ruma<media::redact::msc4322::Request>,
) -> Result<media::redact::msc4322::Response> {
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<'_>,
+3 -10
View File
@@ -2,7 +2,6 @@ use std::collections::BTreeMap;
use axum::extract::State;
use conduwuit::{Err, Result};
use conduwuit_service::sending::EduBuf;
use futures::StreamExt;
use ruma::{
api::{
@@ -44,9 +43,8 @@ pub(crate) async fn send_event_to_device_route(
messages.insert(target_user_id.clone(), map);
let count = services.globals.next_count()?;
let mut buf = EduBuf::new();
serde_json::to_writer(
&mut buf,
services.sending.send_edu_server(
target_user_id.server_name(),
&Edu::DirectToDevice(assign!(
DirectDeviceContent::new(
sender_user.to_owned(),
@@ -55,12 +53,7 @@ pub(crate) async fn send_event_to_device_route(
),
{ messages }
)),
)
.expect("DirectToDevice EDU can be serialized");
services
.sending
.send_edu_server(target_user_id.server_name(), buf)?;
)?;
continue;
}
+1
View File
@@ -158,6 +158,7 @@ pub fn build(router: Router<State>, state: State) -> Router<State> {
.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)
+4
View File
@@ -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),
+28 -3
View File
@@ -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<Map>,
mediaid_user: Arc<Map>,
url_previews: Arc<Map>,
mediaidremote_remotedownload: Arc<Map>,
mediaid_redacted: Arc<Map>,
}
#[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<OwnedServerName> {
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, []);
}
}
+45 -2
View File
@@ -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<Output = HashSet<OwnedServerName>> {
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(
+10 -18
View File
@@ -23,11 +23,7 @@ use ruma::{
};
use self::data::{Data, ReceiptItem};
use crate::{
Dep, rooms,
sending::{self, EduBuf},
sync,
};
use crate::{Dep, rooms, sending, sync};
pub struct Service {
services: Services,
@@ -72,12 +68,10 @@ impl Service {
self.services.sync.wake_all_joined(room_id).await;
// update appservices
let edu = EphemeralData::Receipt(event);
let mut buf = EduBuf::new();
serde_json::to_writer(&mut buf, &edu).expect("Serialized EphemeralData::Receipt");
_ = self
.services
.sending
.send_edu_appservice_room(room_id, buf)
.send_edu_appservice_room(room_id, &edu)
.await
.log_err();
}
@@ -160,19 +154,17 @@ impl Service {
)]),
)]),
)];
let event = ReceiptEvent::new(
room_id.to_owned(),
ReceiptEventContent::from_iter(receipt_content),
);
let edu = EphemeralData::Receipt(event);
let mut buf = EduBuf::new();
serde_json::to_writer(&mut buf, &edu).expect("Serialized EphemeralData::Receipt");
_ = self
.services
.sending
.send_edu_appservice_room_filtered(room_id, buf, |appservice| {
appservice.is_user_match(user_id)
})
.send_edu_appservice_room_filtered(
room_id,
&EphemeralData::Receipt(ReceiptEvent::new(
room_id.to_owned(),
ReceiptEventContent::from_iter(receipt_content),
)),
|appservice| appservice.is_user_match(user_id),
)
.await
.log_err();
}
+4 -11
View File
@@ -15,7 +15,7 @@ use ruma::{
events::{EphemeralRoomEvent, SyncEphemeralRoomEvent, typing::TypingEventContent},
};
use crate::{Dep, globals, sending, sending::EduBuf, sync, users};
use crate::{Dep, globals, sending, sync, users};
pub struct Service {
server: Arc<Server>,
@@ -244,13 +244,9 @@ impl Service {
return Ok(());
}
let content = TypingContent::new(room_id.to_owned(), user_id.to_owned(), typing);
let edu = Edu::Typing(content);
let edu = Edu::Typing(TypingContent::new(room_id.to_owned(), user_id.to_owned(), typing));
let mut buf = EduBuf::new();
serde_json::to_writer(&mut buf, &edu).expect("Serialized Edu::Typing");
self.services.sending.send_edu_room(room_id, buf).await?;
self.services.sending.send_edu_room(room_id, &edu).await?;
Ok(())
}
@@ -261,12 +257,9 @@ impl Service {
self.typings_content(room_id),
));
let mut buf = EduBuf::new();
serde_json::to_writer(&mut buf, &edu).expect("Serialized Edu::Typing");
self.services
.sending
.send_edu_appservice_room(room_id, buf)
.send_edu_appservice_room(room_id, &edu)
.await
}
}
+28 -22
View File
@@ -30,8 +30,9 @@ use futures::{FutureExt, Stream, StreamExt, stream::FuturesUnordered};
use ruma::{
OwnedServerName, RoomId, ServerName, UserId,
api::{
OutgoingRequest, auth_scheme::NoAuthentication,
federation::authentication::ServerSignatures, path_builder::PathBuilder,
OutgoingRequest,
appservice::event::push_events::v1::EphemeralData, auth_scheme::NoAuthentication,
federation::{authentication::ServerSignatures, transactions::edu::Edu}, path_builder::PathBuilder,
},
};
use tokio::{task, task::JoinSet, time::sleep};
@@ -278,10 +279,12 @@ impl Service {
Ok(())
}
#[tracing::instrument(skip(self, server, serialized), level = "debug")]
pub fn send_edu_server(&self, server: &ServerName, serialized: EduBuf) -> Result {
#[tracing::instrument(skip(self, server, edu), level = "debug")]
pub fn send_edu_server(&self, server: &ServerName, edu: &Edu) -> Result {
let dest = Destination::Federation(server.to_owned());
let event = SendingEvent::Edu(serialized);
let mut buf = EduBuf::new();
serde_json::to_writer(&mut buf, edu).expect("should be able to serialize edu");
let event = SendingEvent::Edu(buf);
let _cork = self.db.db.cork();
let keys = self.db.queue_requests(once((&event, &dest)));
self.dispatch(Msg {
@@ -291,10 +294,12 @@ impl Service {
})
}
#[tracing::instrument(skip(self, serialized), level = "debug")]
pub fn send_edu_appservice(&self, appservice_id: &str, serialized: EduBuf) -> Result {
#[tracing::instrument(skip(self, edu), level = "debug")]
pub fn send_edu_appservice(&self, appservice_id: &str, edu: &EphemeralData) -> Result {
let dest = Destination::Appservice(appservice_id.to_owned());
let event = SendingEvent::Edu(serialized);
let mut buf = EduBuf::new();
serde_json::to_writer(&mut buf, edu).expect("should be able to serialize edu");
let event = SendingEvent::Edu(buf);
let _cork = self.db.db.cork();
let keys = self.db.queue_requests(once((&event, &dest)));
self.dispatch(Msg {
@@ -304,21 +309,21 @@ impl Service {
})
}
#[tracing::instrument(skip(self, room_id, serialized), level = "debug")]
#[tracing::instrument(skip(self, room_id, edu), level = "debug")]
pub async fn send_edu_appservice_room(
&self,
room_id: &RoomId,
serialized: EduBuf,
edu: &EphemeralData,
) -> Result<()> {
self.send_edu_appservice_room_filtered(room_id, serialized, |_| true)
self.send_edu_appservice_room_filtered(room_id, edu, |_| true)
.await
}
#[tracing::instrument(skip(self, room_id, serialized, filter), level = "debug")]
#[tracing::instrument(skip(self, room_id, edu, filter), level = "debug")]
pub async fn send_edu_appservice_room_filtered<F>(
&self,
room_id: &RoomId,
serialized: EduBuf,
edu: &EphemeralData,
filter: F,
) -> Result<()>
where
@@ -350,7 +355,7 @@ impl Service {
|| matching_aliases(appservice.aliases.clone()).await
{
_ = self
.send_edu_appservice(&appservice.registration.id, serialized.clone())
.send_edu_appservice(&appservice.registration.id, edu)
.inspect_err(|e| {
warn!(
"failed to send EDU to appservice {}: {e:?}",
@@ -362,26 +367,27 @@ impl Service {
Ok(())
}
#[tracing::instrument(skip(self, room_id, serialized), level = "debug")]
pub async fn send_edu_room(&self, room_id: &RoomId, serialized: EduBuf) -> Result {
#[tracing::instrument(skip(self, room_id, edu), level = "debug")]
pub async fn send_edu_room(&self, room_id: &RoomId, edu: &Edu) -> Result {
let servers = self
.services
.state_cache
.room_servers(room_id)
.ready_filter(|server_name| !self.services.globals.server_is_ours(server_name));
self.send_edu_servers(servers, serialized).await
self.send_edu_servers(servers, edu).await
}
#[tracing::instrument(skip(self, servers, serialized), level = "debug")]
pub async fn send_edu_servers<S>(&self, servers: S, serialized: EduBuf) -> Result
#[tracing::instrument(skip(self, servers, edu), level = "debug")]
pub async fn send_edu_servers<S>(&self, servers: S, edu: &Edu) -> Result
where
S: Stream<Item = OwnedServerName> + Send,
{
let mut buf = EduBuf::new();
serde_json::to_writer(&mut buf, edu).expect("should be able to serialize edu");
let requests = servers
.map(|server| {
(Destination::Federation(server), SendingEvent::Edu(serialized.clone()))
})
.map(|server| (Destination::Federation(server), SendingEvent::Edu(buf.clone())))
.collect::<Vec<_>>()
.await;