Skip to content
Snippets Groups Projects
to_device.rs 3.39 KiB
Newer Older
  • Learn to ignore specific revisions
  • Timo Kösters's avatar
    Timo Kösters committed
    use ruma::events::ToDeviceEventType;
    
    use std::collections::BTreeMap;
    
    
    use crate::{Error, Result, Ruma, services};
    
    Jonas Platte's avatar
    Jonas Platte committed
    use ruma::{
    
    Jonathan de Jong's avatar
    Jonathan de Jong committed
            client::{error::ErrorKind, to_device::send_event_to_device},
    
            federation::{self, transactions::edu::DirectDeviceContent},
        },
    
    Jonas Platte's avatar
    Jonas Platte committed
        to_device::DeviceIdOrAllDevices,
    
    /// # `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(
    
    Timo Kösters's avatar
    Timo Kösters committed
        body: Ruma<send_event_to_device::v3::IncomingRequest>,
    
    Jonathan de Jong's avatar
    Jonathan de Jong committed
    ) -> 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();
    
    Timo's avatar
    Timo committed
    
        // Check if this is a new transaction id
    
        if services()
    
    Timo's avatar
    Timo committed
            .transaction_ids
    
            .existing_txnid(sender_user, sender_device, &body.txn_id)?
    
    Timo's avatar
    Timo committed
            .is_some()
        {
    
            return Ok(send_event_to_device::v3::Response {});
    
    Timo's avatar
    Timo committed
        }
    
    
        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);
    
    
                    services().sending.send_reliable_edu(
    
                        target_user_id.server_name(),
    
                        serde_json::to_vec(&federation::transactions::edu::Edu::DirectToDevice(
    
                            DirectDeviceContent {
                                sender: sender_user.clone(),
    
    Timo Kösters's avatar
    Timo Kösters committed
                                ev_type: ToDeviceEventType::from(&*body.event_type),
                                message_id: body.txn_id.to_owned(),
    
                                messages,
                            },
                        ))
                        .expect("DirectToDevice EDU can be serialized"),
    
                        services().globals.next_count()?,
    
                match target_device_id_maybe {
    
                    DeviceIdOrAllDevices::DeviceId(target_device_id) => services().users.add_to_device_event(
    
    Jonas Platte's avatar
    Jonas Platte committed
                        sender_user,
    
    Timo Kösters's avatar
    Timo Kösters committed
                        &target_device_id,
    
    Jonas Platte's avatar
    Jonas Platte committed
                        &body.event_type,
                        event.deserialize_as().map_err(|_| {
                            Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid")
                        })?,
                    )?,
    
    Jonas Platte's avatar
    Jonas Platte committed
                    DeviceIdOrAllDevices::AllDevices => {
    
                        for target_device_id in services().users.all_device_ids(target_user_id) {
                            services().users.add_to_device_event(
    
                                sender_user,
    
                                &target_device_id?,
                                &body.event_type,
    
    Jonas Platte's avatar
    Jonas Platte committed
                                event.deserialize_as().map_err(|_| {
    
                                    Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid")
                                })?,
                            )?;
                        }
                    }
                }
            }
        }
    
    
    Timo's avatar
    Timo committed
        // Save transaction id with empty data
    
        services().transaction_ids
    
            .add_txnid(sender_user, sender_device, &body.txn_id, &[])?;
    
    Timo's avatar
    Timo committed
    
    
    Jonathan de Jong's avatar
    Jonathan de Jong committed
        Ok(send_event_to_device::v3::Response {})