refactor: Fix errors in api/client/to_device.rs

This commit is contained in:
Ginger
2026-04-12 10:52:50 -04:00
parent 8d8c310a64
commit 4dfdce303f
+31 -26
View File
@@ -1,14 +1,15 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use axum::extract::State; use axum::extract::State;
use conduwuit::{Error, Result}; use conduwuit::{Err, Result};
use conduwuit_service::sending::EduBuf; use conduwuit_service::sending::EduBuf;
use futures::StreamExt; use futures::StreamExt;
use ruma::{ use ruma::{
api::{ api::{
client::{error::ErrorKind, to_device::send_event_to_device}, client::to_device::send_event_to_device,
federation::{self, transactions::edu::DirectDeviceContent}, federation::transactions::edu::{DirectDeviceContent, Edu},
}, },
assign,
to_device::DeviceIdOrAllDevices, to_device::DeviceIdOrAllDevices,
}; };
@@ -31,14 +32,14 @@ pub(crate) async fn send_event_to_device_route(
.await .await
.is_ok() .is_ok()
{ {
return Ok(send_event_to_device::v3::Response {}); return Ok(send_event_to_device::v3::Response::new());
} }
for (target_user_id, map) in &body.messages { for (target_user_id, map) in &body.messages {
for (target_device_id_maybe, event) in map { for (target_device, event) in map {
if !services.globals.user_is_local(target_user_id) { if !services.globals.user_is_local(target_user_id) {
let mut map = BTreeMap::new(); let mut map = BTreeMap::new();
map.insert(target_device_id_maybe.clone(), event.clone()); map.insert(target_device.clone(), event.clone());
let mut messages = BTreeMap::new(); let mut messages = BTreeMap::new();
messages.insert(target_user_id.clone(), map); messages.insert(target_user_id.clone(), map);
let count = services.globals.next_count()?; let count = services.globals.next_count()?;
@@ -46,12 +47,14 @@ pub(crate) async fn send_event_to_device_route(
let mut buf = EduBuf::new(); let mut buf = EduBuf::new();
serde_json::to_writer( serde_json::to_writer(
&mut buf, &mut buf,
&federation::transactions::edu::Edu::DirectToDevice(DirectDeviceContent { &Edu::DirectToDevice(assign!(
sender: sender_user.to_owned(), DirectDeviceContent::new(
ev_type: body.event_type.clone(), sender_user.to_owned(),
message_id: count.to_string().into(), body.event_type.clone(),
messages, count.to_string().into(),
}), ),
{ messages }
)),
) )
.expect("DirectToDevice EDU can be serialized"); .expect("DirectToDevice EDU can be serialized");
@@ -64,11 +67,11 @@ pub(crate) async fn send_event_to_device_route(
let event_type = &body.event_type.to_string(); let event_type = &body.event_type.to_string();
let event = event let Ok(event) = event.deserialize_as() else {
.deserialize_as() return Err!(Request(InvalidParam("Failed to deserialize event body.")));
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid"))?; };
match target_device_id_maybe { match target_device {
| DeviceIdOrAllDevices::DeviceId(target_device_id) => { | DeviceIdOrAllDevices::DeviceId(target_device_id) => {
services services
.users .users
@@ -81,20 +84,22 @@ pub(crate) async fn send_event_to_device_route(
) )
.await; .await;
}, },
| DeviceIdOrAllDevices::AllDevices => { | DeviceIdOrAllDevices::AllDevices => {
let (event_type, event) = (&event_type, &event); let (event_type, event) = (&event_type, &event);
services services
.users .users
.all_device_ids(target_user_id) .all_device_ids(target_user_id)
.for_each(|target_device_id| { .for_each(async |target_device_id| {
services.users.add_to_device_event( services
sender_user, .users
target_user_id, .add_to_device_event(
target_device_id, sender_user,
event_type, target_user_id,
event.clone(), &target_device_id,
) event_type,
event.clone(),
)
.await
}) })
.await; .await;
}, },
@@ -107,5 +112,5 @@ pub(crate) async fn send_event_to_device_route(
.transactions .transactions
.add_client_txnid(sender_user, sender_device, &body.txn_id, &[]); .add_client_txnid(sender_user, sender_device, &body.txn_id, &[]);
Ok(send_event_to_device::v3::Response {}) Ok(send_event_to_device::v3::Response::new())
} }