pub async fn share_group_session(
        &self,
        room_id: &RoomId,
        users: impl Iterator<Item = &UserId>,
        encryption_settings: impl Into<EncryptionSettings>,
    ) -> OlmResult<Vec<Arc<ToDeviceRequest>>> {
        self.create_outbound_group_session(room_id, encryption_settings.into())
            .await?;
        let session = self.outbound_group_sessions.get(room_id).unwrap();

        if session.shared() {
            panic!("Session is already shared");
        }

        let mut devices: Vec<Device> = Vec::new();

        for user_id in users {
            session.add_recipient(user_id);
            let user_devices = self.store.get_user_devices(&user_id).await?;
            devices.extend(user_devices.devices().filter(|d| !d.is_blacklisted()));
        }

        let mut requests = Vec::new();
        let key_content = session.as_json().await;

        for device_map_chunk in devices.chunks(Self::MAX_TO_DEVICE_MESSAGES) {
            let mut messages = BTreeMap::new();

            for device in device_map_chunk {
                let encrypted = device
                    .encrypt(EventType::RoomKey, key_content.clone())
                    .await;

                let encrypted = match encrypted {
                    Ok(c) => c,
                    Err(OlmError::MissingSession)
                    | Err(OlmError::EventError(EventError::MissingSenderKey)) => {
                        continue;
                    }
                    Err(e) => return Err(e),
                };

                messages
                    .entry(device.user_id().clone())
                    .or_insert_with(BTreeMap::new)
                    .insert(
                        DeviceIdOrAllDevices::DeviceId(device.device_id().into()),
                        serde_json::value::to_raw_value(&encrypted)?,
                    );
            }

            let id = Uuid::new_v4();

            let request = Arc::new(ToDeviceRequest {
                event_type: EventType::RoomEncrypted,
                txn_id: id,
                messages,
            });

            session.add_request(id, request.clone());
            self.outbound_sessions_being_shared
                .insert(id, session.clone());
            requests.push(request);
        }

        if requests.is_empty() {
            debug!(
                "Session {} for room {} doesn't need to be shared with anyone, marking as shared",
                session.session_id(),
                session.room_id()
            );
            session.mark_as_shared();
        }

        Ok(requests)
    }
/// Get to-device requests to share a group session with users in a room.
///
/// # Arguments
///
/// `room_id` - The room id of the room where the group session will be
/// used.
///
/// `users` - The list of users that should receive the group session.
Get to-device requests to share a group session with users in a room.
Get to - device requests to share a group session with users in a room .
pub async fn share_group_session(
        &self,
        room_id: &RoomId,
        users: impl Iterator<Item = &UserId>,
        encryption_settings: impl Into<EncryptionSettings>,
    ) -> OlmResult<Vec<Arc<ToDeviceRequest>>> {
        self.create_outbound_group_session(room_id, encryption_settings.into())
            .await?;
        let session = self.outbound_group_sessions.get(room_id).unwrap();
        if session.shared() {
            panic!("Session is already shared");
        }
        let mut devices: Vec<Device> = Vec::new();
        for user_id in users {
            session.add_recipient(user_id);
            let user_devices = self.store.get_user_devices(&user_id).await?;
            devices.extend(user_devices.devices().filter(|d| !d.is_blacklisted()));
        }
        let mut requests = Vec::new();
        let key_content = session.as_json().await;
        for device_map_chunk in devices.chunks(Self::MAX_TO_DEVICE_MESSAGES) {
            let mut messages = BTreeMap::new();
            for device in device_map_chunk {
                let encrypted = device
                    .encrypt(EventType::RoomKey, key_content.clone())
                    .await;
                let encrypted = match encrypted {
                    Ok(c) => c,
                    Err(OlmError::MissingSession)
                    | Err(OlmError::EventError(EventError::MissingSenderKey)) => {
                        continue;
                    }
                    Err(e) => return Err(e),
                };
                messages
                    .entry(device.user_id().clone())
                    .or_insert_with(BTreeMap::new)
                    .insert(
                        DeviceIdOrAllDevices::DeviceId(device.device_id().into()),
                        serde_json::value::to_raw_value(&encrypted)?,
                    );
            }
            let id = Uuid::new_v4();
            let request = Arc::new(ToDeviceRequest {
                event_type: EventType::RoomEncrypted,
                txn_id: id,
                messages,
            });
            session.add_request(id, request.clone());
            self.outbound_sessions_being_shared
                .insert(id, session.clone());
            requests.push(request);
        }
        if requests.is_empty() {
            debug!(
                "Session {} for room {} doesn't need to be shared with anyone, marking as shared",
                session.session_id(),
                session.room_id()
            );
            session.mark_as_shared();
        }
        Ok(requests)
    }
pub async fn share_group_session ( & self , room_id : & RoomId , users : impl Iterator < Item = & UserId > , encryption_settings : impl Into < EncryptionSettings > , ) -> OlmResult < Vec < Arc < ToDeviceRequest > > > { self . create_outbound_group_session ( room_id , encryption_settings . into ( ) ) . await ? ; let session = self . outbound_group_sessions . get ( room_id ) . unwrap ( ) ; if session . shared ( ) { panic ! ( " " ) ; } let mut devices : Vec < Device > = Vec :: new ( ) ; for user_id in users { session . add_recipient ( user_id ) ; let user_devices = self . store . get_user_devices ( & user_id ) . await ? ; devices . extend ( user_devices . devices ( ) . filter ( | d | ! d . is_blacklisted ( ) ) ) ; } let mut requests = Vec :: new ( ) ; let key_content = session . as_json ( ) . await ; for device_map_chunk in devices . chunks ( Self :: MAX_TO_DEVICE_MESSAGES ) { let mut messages = BTreeMap :: new ( ) ; for device in device_map_chunk { let encrypted = device . encrypt ( EventType :: RoomKey , key_content . clone ( ) ) . await ; let encrypted = match encrypted { Ok ( c ) => c , Err ( OlmError :: MissingSession ) | Err ( OlmError :: EventError ( EventError :: MissingSenderKey ) ) => { continue ; } Err ( e ) => return Err ( e ) , } ; messages . entry ( device . user_id ( ) . clone ( ) ) . or_insert_with ( BTreeMap :: new ) . insert ( DeviceIdOrAllDevices :: DeviceId ( device . device_id ( ) . into ( ) ) , serde_json :: value :: to_raw_value ( & encrypted ) ? , ) ; } let id = Uuid :: new_v4 ( ) ; let request = Arc :: new ( ToDeviceRequest { event_type : EventType :: RoomEncrypted , txn_id : id , messages , } ) ; session . add_request ( id , request . clone ( ) ) ; self . outbound_sessions_being_shared . insert ( id , session . clone ( ) ) ; requests . push ( request ) ; } if requests . is_empty ( ) { debug ! ( " " session session_id ( ) session room_id ( ) ) ; session . mark_as_shared ( ) ; } Ok ( requests ) }
Get to-device requests to share a group session with users in a room.
Get to - device requests to share a group session with users in a room .
&self
room_id
&RoomId
users
impl Iterator<Item = &UserId>
encryption_settings
impl Into<EncryptionSettings>
&self
room_id
&RoomId
users
impl Iterator<Item = &UserId>
encryption_settings
impl Into<EncryptionSettings>