Conduit is a simple, fast and reliable chat server powered by Matrix https://conduit.rs
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.5 KiB
70 lines
2.5 KiB
use super::State; |
|
use crate::{ConduitResult, Database, Error, Ruma}; |
|
use ruma::api::client::{ |
|
error::ErrorKind, |
|
r0::to_device::{self, send_event_to_device}, |
|
}; |
|
|
|
#[cfg(feature = "conduit_bin")] |
|
use rocket::put; |
|
|
|
#[cfg_attr( |
|
feature = "conduit_bin", |
|
put("/_matrix/client/r0/sendToDevice/<_>/<_>", data = "<body>") |
|
)] |
|
pub fn send_event_to_device_route( |
|
db: State<'_, Database>, |
|
body: Ruma<send_event_to_device::Request<'_>>, |
|
) -> 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_ref().expect("user is authenticated"); |
|
|
|
// Check if this is a new transaction id |
|
if db |
|
.transaction_ids |
|
.existing_txnid(sender_user, sender_device, &body.txn_id)? |
|
.is_some() |
|
{ |
|
return Ok(send_event_to_device::Response.into()); |
|
} |
|
|
|
for (target_user_id, map) in &body.messages { |
|
for (target_device_id_maybe, event) in map { |
|
match target_device_id_maybe { |
|
to_device::DeviceIdOrAllDevices::DeviceId(target_device_id) => { |
|
db.users.add_to_device_event( |
|
sender_user, |
|
&target_user_id, |
|
&target_device_id, |
|
&body.event_type, |
|
serde_json::from_str(event.get()).map_err(|_| { |
|
Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid") |
|
})?, |
|
&db.globals, |
|
)? |
|
} |
|
|
|
to_device::DeviceIdOrAllDevices::AllDevices => { |
|
for target_device_id in db.users.all_device_ids(&target_user_id) { |
|
db.users.add_to_device_event( |
|
sender_user, |
|
&target_user_id, |
|
&target_device_id?, |
|
&body.event_type, |
|
serde_json::from_str(event.get()).map_err(|_| { |
|
Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid") |
|
})?, |
|
&db.globals, |
|
)?; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
// Save transaction id with empty data |
|
db.transaction_ids |
|
.add_txnid(sender_user, sender_device, &body.txn_id, &[])?; |
|
|
|
Ok(send_event_to_device::Response.into()) |
|
}
|
|
|