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.

116 lines
2.8 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;
use conduwuit::{Err, Result};
use conduwuit_service::sending::EduBuf;
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::{
client::to_device::send_event_to_device,
federation::transactions::edu::{DirectDeviceContent, Edu},
2021-07-20 21:17:15 +02:00
},
assign,
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(
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> {
let sender_user = body.identity.sender_user();
let sender_device = body.identity.sender_device();
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
.transactions
.get_client_txn(sender_user, sender_device, &body.txn_id)
2024-08-08 17:18:30 +00:00
.await
.is_ok()
2024-03-25 17:05:11 -04:00
{
return Ok(send_event_to_device::v3::Response::new());
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, 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.clone(), event.clone());
2021-07-20 21:17:15 +02:00
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
let mut buf = EduBuf::new();
serde_json::to_writer(
&mut buf,
&Edu::DirectToDevice(assign!(
DirectDeviceContent::new(
sender_user.to_owned(),
body.event_type.clone(),
count.to_string().into(),
),
{ messages }
)),
)
.expect("DirectToDevice EDU can be serialized");
services
.sending
.send_edu_server(target_user_id.server_name(), buf)?;
2021-07-20 21:17:15 +02:00
continue;
}
2024-08-08 17:18:30 +00:00
let event_type = &body.event_type.to_string();
let Ok(event) = event.deserialize_as() else {
return Err!(Request(InvalidParam("Failed to deserialize event body.")));
};
2024-08-08 17:18:30 +00:00
match target_device {
| 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,
)
2024-08-08 17:18:30 +00:00
.await;
2022-10-05 20:34:31 +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(async |target_device_id| {
services
.users
.add_to_device_event(
sender_user,
target_user_id,
&target_device_id,
event_type,
event.clone(),
)
2026-04-13 18:31:16 -04:00
.await;
2024-08-08 17:18:30 +00:00
})
.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
.transactions
.add_client_txnid(sender_user, sender_device, &body.txn_id, &[]);
2020-08-25 13:24:38 +02:00
Ok(send_event_to_device::v3::Response::new())
2020-07-30 18:14:47 +02:00
}