Compare commits

...
Author SHA1 Message Date
Ginger 6e526b40c4 chore: News fragment 2026-11-04 15:50:50 -05:00
Ginger 7b8af120aa feat: Add support for MSC4322 2026-11-04 15:47:54 -05:00
Ginger 4b4918d677 refactor(sending): Stop passing byte buffers(?!) to EDU send methods 2026-11-04 15:47:54 -05:00
12 changed files with 161 additions and 79 deletions

No files matched your search

Generated
+11 -11
View File
@@ -4997,7 +4997,7 @@ dependencies = [
[[package]]
name = "ruma"
version = "0.17.2"
source = "git+https://github.com/ruma/ruma.git?rev=0ed7fba34213f18a628a56f527c6cc795cd7eef5#0ed7fba34213f18a628a56f527c6cc795cd7eef5"
source = "git+https://github.com/gingershaped/ruwuma.git?rev=1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62#1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62"
dependencies = [
"assign",
"js_int",
@@ -5016,7 +5016,7 @@ dependencies = [
[[package]]
name = "ruma-appservice-api"
version = "0.17.1"
source = "git+https://github.com/ruma/ruma.git?rev=0ed7fba34213f18a628a56f527c6cc795cd7eef5#0ed7fba34213f18a628a56f527c6cc795cd7eef5"
source = "git+https://github.com/gingershaped/ruwuma.git?rev=1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62#1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62"
dependencies = [
"http",
"js_int",
@@ -5029,7 +5029,7 @@ dependencies = [
[[package]]
name = "ruma-client-api"
version = "0.25.2"
source = "git+https://github.com/ruma/ruma.git?rev=0ed7fba34213f18a628a56f527c6cc795cd7eef5#0ed7fba34213f18a628a56f527c6cc795cd7eef5"
source = "git+https://github.com/gingershaped/ruwuma.git?rev=1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62#1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62"
dependencies = [
"as_variant",
"assign",
@@ -5051,7 +5051,7 @@ dependencies = [
[[package]]
name = "ruma-common"
version = "0.20.2"
source = "git+https://github.com/ruma/ruma.git?rev=0ed7fba34213f18a628a56f527c6cc795cd7eef5#0ed7fba34213f18a628a56f527c6cc795cd7eef5"
source = "git+https://github.com/gingershaped/ruwuma.git?rev=1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62#1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62"
dependencies = [
"as_variant",
"base64 0.23.1",
@@ -5084,7 +5084,7 @@ dependencies = [
[[package]]
name = "ruma-events"
version = "0.35.2"
source = "git+https://github.com/ruma/ruma.git?rev=0ed7fba34213f18a628a56f527c6cc795cd7eef5#0ed7fba34213f18a628a56f527c6cc795cd7eef5"
source = "git+https://github.com/gingershaped/ruwuma.git?rev=1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62#1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62"
dependencies = [
"as_variant",
"indexmap 2.14.2",
@@ -5105,7 +5105,7 @@ dependencies = [
[[package]]
name = "ruma-federation-api"
version = "0.16.1"
source = "git+https://github.com/ruma/ruma.git?rev=0ed7fba34213f18a628a56f527c6cc795cd7eef5#0ed7fba34213f18a628a56f527c6cc795cd7eef5"
source = "git+https://github.com/gingershaped/ruwuma.git?rev=1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62#1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62"
dependencies = [
"bytes",
"http",
@@ -5127,7 +5127,7 @@ dependencies = [
[[package]]
name = "ruma-identifiers-validation"
version = "0.14.0"
source = "git+https://github.com/ruma/ruma.git?rev=0ed7fba34213f18a628a56f527c6cc795cd7eef5#0ed7fba34213f18a628a56f527c6cc795cd7eef5"
source = "git+https://github.com/gingershaped/ruwuma.git?rev=1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62#1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62"
dependencies = [
"js_int",
"thiserror 2.0.21",
@@ -5136,7 +5136,7 @@ dependencies = [
[[package]]
name = "ruma-macros"
version = "0.20.2"
source = "git+https://github.com/ruma/ruma.git?rev=0ed7fba34213f18a628a56f527c6cc795cd7eef5#0ed7fba34213f18a628a56f527c6cc795cd7eef5"
source = "git+https://github.com/gingershaped/ruwuma.git?rev=1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62#1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62"
dependencies = [
"as_variant",
"cfg-if",
@@ -5152,7 +5152,7 @@ dependencies = [
[[package]]
name = "ruma-push-gateway-api"
version = "0.16.0"
source = "git+https://github.com/ruma/ruma.git?rev=0ed7fba34213f18a628a56f527c6cc795cd7eef5#0ed7fba34213f18a628a56f527c6cc795cd7eef5"
source = "git+https://github.com/gingershaped/ruwuma.git?rev=1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62#1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62"
dependencies = [
"js_int",
"ruma-common",
@@ -5164,7 +5164,7 @@ dependencies = [
[[package]]
name = "ruma-signatures"
version = "0.22.1"
source = "git+https://github.com/ruma/ruma.git?rev=0ed7fba34213f18a628a56f527c6cc795cd7eef5#0ed7fba34213f18a628a56f527c6cc795cd7eef5"
source = "git+https://github.com/gingershaped/ruwuma.git?rev=1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62#1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62"
dependencies = [
"base64 0.23.1",
"ed25519-dalek 3.0.0",
@@ -5181,7 +5181,7 @@ dependencies = [
[[package]]
name = "ruma-state-res"
version = "0.18.0"
source = "git+https://github.com/ruma/ruma.git?rev=0ed7fba34213f18a628a56f527c6cc795cd7eef5#0ed7fba34213f18a628a56f527c6cc795cd7eef5"
source = "git+https://github.com/gingershaped/ruwuma.git?rev=1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62#1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62"
dependencies = [
"js_int",
"ruma-common",
+3 -2
View File
@@ -348,8 +348,8 @@ version = "1.1.1"
# Used for matrix spec type definitions and helpers
[workspace.dependencies.ruma]
git = "https://github.com/ruma/ruma.git"
rev = "0ed7fba34213f18a628a56f527c6cc795cd7eef5"
git = "https://github.com/gingershaped/ruwuma.git"
rev = "1e2c07dc7b65df9a35d9e3c2cedc13857dae8a62"
features = [
"appservice-api-c",
"client-api",
@@ -381,6 +381,7 @@ features = [
"unstable-msc4143",
"unstable-msc4293",
"unstable-msc4363",
"unstable-msc4322",
"unstable-msc4406",
"unstable-msc4439",
"unstable-msc4354",
+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;
@@ -238,6 +238,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::unstable::Request>,
) -> Result<media::redact::unstable::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::unstable::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
@@ -157,6 +157,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(),
}
}
@@ -348,4 +355,22 @@ impl Data {
audio_size,
})
}
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},
@@ -158,6 +162,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
@@ -255,6 +284,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,
@@ -69,12 +65,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();
}
@@ -156,19 +150,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
}
}
+27 -21
View File
@@ -24,8 +24,9 @@ use ruma::{
OwnedServerName, RoomId, ServerName, UserId,
api::{
OutgoingRequest,
appservice::event::push_events::v1::EphemeralData,
auth_scheme::{NoAccessToken, NoAuthentication, SendAccessToken},
federation::authentication::ServerSignatures,
federation::{authentication::ServerSignatures, transactions::edu::Edu},
path_builder::PathBuilder,
},
};
@@ -220,10 +221,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 {
@@ -233,10 +236,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 {
@@ -246,21 +251,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
@@ -292,7 +297,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:?}",
@@ -304,26 +309,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;