conduit/src/client_server/to_device.rs

102 lines
3.6 KiB
Rust
Raw Normal View History

use std::collections::BTreeMap;
2021-07-14 08:07:08 +01:00
use crate::{database::DatabaseGuard, ConduitResult, Error, Ruma};
2021-06-17 19:12:36 +01:00
use ruma::{
api::{
client::{error::ErrorKind, r0::to_device::send_event_to_device},
federation::{self, transactions::edu::DirectDeviceContent},
},
events::EventType,
2021-06-17 19:12:36 +01:00
to_device::DeviceIdOrAllDevices,
2020-07-30 17:14:47 +01:00
};
#[cfg(feature = "conduit_bin")]
use rocket::put;
#[cfg_attr(
feature = "conduit_bin",
put("/_matrix/client/r0/sendToDevice/<_>/<_>", data = "<body>")
)]
2021-02-28 11:41:03 +00:00
#[tracing::instrument(skip(db, body))]
pub async fn send_event_to_device_route(
2021-07-14 08:07:08 +01:00
db: DatabaseGuard,
2020-09-08 16:32:03 +01:00
body: Ruma<send_event_to_device::Request<'_>>,
2020-07-30 17:14:47 +01:00
) -> ConduitResult<send_event_to_device::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sender_device = body.sender_device.as_deref();
2020-08-25 12:24:38 +01:00
2021-06-30 19:31:51 +01:00
// TODO: uncomment when https://github.com/vector-im/element-android/issues/3589 is solved
2020-08-25 12:24:38 +01:00
// Check if this is a new transaction id
2021-06-30 19:31:51 +01:00
/*
2020-08-25 12:24:38 +01:00
if db
.transaction_ids
.existing_txnid(sender_user, sender_device, &body.txn_id)?
2020-08-25 12:24:38 +01:00
.is_some()
{
return Ok(send_event_to_device::Response.into());
}
2021-06-30 19:31:51 +01:00
*/
2020-07-30 17:14:47 +01:00
for (target_user_id, map) in &body.messages {
for (target_device_id_maybe, event) in map {
if target_user_id.server_name() != db.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);
db.sending.send_reliable_edu(
target_user_id.server_name(),
2021-07-29 19:17:47 +01:00
serde_json::to_vec(&federation::transactions::edu::Edu::DirectToDevice(
DirectDeviceContent {
sender: sender_user.clone(),
ev_type: EventType::from(&body.event_type),
message_id: body.txn_id.clone(),
messages,
},
))
.expect("DirectToDevice EDU can be serialized"),
)?;
continue;
}
2020-07-30 17:14:47 +01:00
match target_device_id_maybe {
2021-06-17 19:12:36 +01:00
DeviceIdOrAllDevices::DeviceId(target_device_id) => db.users.add_to_device_event(
sender_user,
&target_user_id,
&target_device_id,
&body.event_type,
event.deserialize_as().map_err(|_| {
Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid")
})?,
&db.globals,
)?,
2020-07-30 17:14:47 +01:00
2021-06-17 19:12:36 +01:00
DeviceIdOrAllDevices::AllDevices => {
2020-07-30 17:14:47 +01:00
for target_device_id in db.users.all_device_ids(&target_user_id) {
db.users.add_to_device_event(
sender_user,
2020-07-30 17:14:47 +01:00
&target_user_id,
&target_device_id?,
&body.event_type,
2021-06-17 19:12:36 +01:00
event.deserialize_as().map_err(|_| {
2020-07-30 17:14:47 +01:00
Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid")
})?,
&db.globals,
)?;
}
}
}
}
}
2020-08-25 12:24:38 +01:00
// Save transaction id with empty data
db.transaction_ids
.add_txnid(sender_user, sender_device, &body.txn_id, &[])?;
2020-08-25 12:24:38 +01:00
db.flush().await?;
Ok(send_event_to_device::Response {}.into())
2020-07-30 17:14:47 +01:00
}