1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-06-27 16:35:59 +00:00
conduit/src/api/client_server/to_device.rs

93 lines
3.5 KiB
Rust
Raw Normal View History

use std::collections::BTreeMap;
2022-10-05 20:34:31 +02:00
use crate::{services, Error, Result, Ruma};
2021-06-17 20:12:36 +02:00
use ruma::{
api::{
2022-02-18 15:33:14 +01:00
client::{error::ErrorKind, to_device::send_event_to_device},
federation::{self, transactions::edu::DirectDeviceContent},
},
2021-06-17 20:12:36 +02:00
to_device::DeviceIdOrAllDevices,
2020-07-30 18:14:47 +02:00
};
2021-08-31 19:14:37 +02:00
/// # `PUT /_matrix/client/r0/sendToDevice/{eventType}/{txnId}`
///
/// Send a to-device event to a set of client devices.
pub async fn send_event_to_device_route(
2022-12-14 13:09:10 +01:00
body: Ruma<send_event_to_device::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<send_event_to_device::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sender_device = body.sender_device.as_deref();
2020-08-25 13:24:38 +02:00
// Check if this is a new transaction id
if services()
2020-08-25 13:24:38 +02:00
.transaction_ids
.existing_txnid(sender_user, sender_device, &body.txn_id)?
2020-08-25 13:24:38 +02:00
.is_some()
{
return Ok(send_event_to_device::v3::Response {});
2020-08-25 13:24:38 +02:00
}
2020-07-30 18:14:47 +02:00
for (target_user_id, map) in &body.messages {
for (target_device_id_maybe, event) in map {
if target_user_id.server_name() != services().globals.server_name() {
let mut map = BTreeMap::new();
map.insert(target_device_id_maybe.clone(), event.clone());
let mut messages = BTreeMap::new();
messages.insert(target_user_id.clone(), map);
2022-10-13 10:14:52 +02:00
let count = services().globals.next_count()?;
services().sending.send_reliable_edu(
target_user_id.server_name(),
2021-07-29 20:17:47 +02:00
serde_json::to_vec(&federation::transactions::edu::Edu::DirectToDevice(
DirectDeviceContent {
sender: sender_user.clone(),
2023-02-26 16:29:06 +01:00
ev_type: body.event_type.clone(),
2022-10-13 10:14:52 +02:00
message_id: count.to_string().into(),
messages,
},
))
.expect("DirectToDevice EDU can be serialized"),
2022-10-13 10:14:52 +02:00
count,
)?;
continue;
}
2020-07-30 18:14:47 +02:00
match target_device_id_maybe {
2022-10-05 20:34:31 +02:00
DeviceIdOrAllDevices::DeviceId(target_device_id) => {
services().users.add_to_device_event(
sender_user,
target_user_id,
2022-10-10 14:09:11 +02:00
target_device_id,
2023-02-26 16:29:06 +01:00
&body.event_type.to_string(),
2022-10-05 20:34:31 +02:00
event.deserialize_as().map_err(|_| {
Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid")
})?,
)?
}
2020-07-30 18:14:47 +02:00
2021-06-17 20:12:36 +02:00
DeviceIdOrAllDevices::AllDevices => {
for target_device_id in services().users.all_device_ids(target_user_id) {
services().users.add_to_device_event(
sender_user,
target_user_id,
2020-07-30 18:14:47 +02:00
&target_device_id?,
2023-02-26 16:29:06 +01:00
&body.event_type.to_string(),
2021-06-17 20:12:36 +02:00
event.deserialize_as().map_err(|_| {
2020-07-30 18:14:47 +02:00
Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid")
})?,
)?;
}
}
}
}
}
2020-08-25 13:24:38 +02:00
// Save transaction id with empty data
2022-10-05 20:34:31 +02:00
services()
.transaction_ids
.add_txnid(sender_user, sender_device, &body.txn_id, &[])?;
2020-08-25 13:24:38 +02:00
2022-02-18 15:33:14 +01:00
Ok(send_event_to_device::v3::Response {})
2020-07-30 18:14:47 +02:00
}