Files
continuwuity/src/api/client/to_device.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

99 lines
2.7 KiB
Rust
Raw Normal View History

2021-07-20 21:17:15 +02:00
use std::collections::BTreeMap;
2024-07-16 08:05:25 +00:00
use axum::extract::State;
2024-07-22 07:43:51 +00:00
use conduit::{Error, Result};
2024-08-08 17:18:30 +00:00
use futures::StreamExt;
2021-06-17 20:12:36 +02:00
use ruma::{
2021-07-20 21:17:15 +02:00
api::{
2022-02-18 15:33:14 +01:00
client::{error::ErrorKind, to_device::send_event_to_device},
2021-07-20 21:17:15 +02:00
federation::{self, transactions::edu::DirectDeviceContent},
},
2021-06-17 20:12:36 +02:00
to_device::DeviceIdOrAllDevices,
2020-07-30 18:14:47 +02:00
};
2024-07-22 07:43:51 +00:00
use crate::Ruma;
2024-03-05 19:48:54 -05: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.
2024-04-22 23:48:57 -04:00
pub(crate) async fn send_event_to_device_route(
2024-07-16 08:05:25 +00:00
State(services): State<crate::State>, body: Ruma<send_event_to_device::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<send_event_to_device::v3::Response> {
2020-10-18 20:33:12 +02:00
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-12-08 10:33:44 +01:00
let sender_device = body.sender_device.as_deref();
2020-08-25 13:24:38 +02:00
// Check if this is a new transaction id
2024-07-16 08:05:25 +00:00
if services
2024-03-25 17:05:11 -04:00
.transaction_ids
2024-08-08 17:18:30 +00:00
.existing_txnid(sender_user, sender_device, &body.txn_id)
.await
.is_ok()
2024-03-25 17:05:11 -04:00
{
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 {
2024-07-22 07:43:51 +00:00
if !services.globals.user_is_local(target_user_id) {
2021-07-20 21:17:15 +02:00
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);
2024-07-16 08:05:25 +00:00
let count = services.globals.next_count()?;
2021-07-20 21:17:15 +02:00
2024-07-16 08:05:25 +00:00
services.sending.send_edu_server(
2021-07-20 21:17:15 +02:00
target_user_id.server_name(),
2021-07-29 20:17:47 +02:00
serde_json::to_vec(&federation::transactions::edu::Edu::DirectToDevice(DirectDeviceContent {
2021-07-20 21:17:15 +02:00
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(),
2021-07-20 21:17:15 +02:00
messages,
}))
.expect("DirectToDevice EDU can be serialized"),
)?;
continue;
}
2024-08-08 17:18:30 +00:00
let event_type = &body.event_type.to_string();
let event = event
.deserialize_as()
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid"))?;
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) => {
2024-08-08 17:18:30 +00:00
services
.users
.add_to_device_event(sender_user, target_user_id, target_device_id, event_type, event)
.await;
2022-10-05 20:34:31 +02:00
},
2020-07-30 18:14:47 +02:00
2021-06-17 20:12:36 +02:00
DeviceIdOrAllDevices::AllDevices => {
2024-08-08 17:18:30 +00:00
let (event_type, event) = (&event_type, &event);
services
.users
.all_device_ids(target_user_id)
.for_each(|target_device_id| {
services.users.add_to_device_event(
sender_user,
target_user_id,
target_device_id,
event_type,
event.clone(),
)
})
.await;
2020-07-30 18:14:47 +02:00
},
}
}
}
2020-08-25 13:24:38 +02:00
// Save transaction id with empty data
2024-07-16 08:05:25 +00:00
services
2024-03-25 17:05:11 -04:00
.transaction_ids
2024-08-08 17:18:30 +00:00
.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
}