pub fn update(&mut self, group_name: Option<String>) -> Result<Vec<String>, String> {
        log::debug!("Updating {} ...", self.username);

        let mut messages_out = Vec::new();

        // Go through the list of messages and process or store them.
        for message in self.backend.recv_msgs(self)?.drain(..) {
            match message {
                Message::Welcome(welcome) => {
                    // Join the group. (Later we should ask the user to
                    // approve first ...)
                    self.join_group(welcome)?;
                }
                Message::MlsMessage(message) => {
                    let mut groups = self.groups.borrow_mut();
                    let mut group = match groups.get(message.group_id()) {
                        Some(g) => g.mls_group.borrow_mut(),
                        None => {
                            log::error!(
                                "Error getting group {:?} for a message. Dropping message.",
                                message.group_id()
                            );
                            continue;
                        }
                    };
                    let msg = match message {
                        DsMlsMessage::Ciphertext(ctxt) => {
                            match group.decrypt(&ctxt, &self.crypto) {
                                Ok(msg) => msg,
                                Err(e) => {
                                    log::error!(
                                        "Error decrypting MlsCiphertext: {:?} -  Dropping message.",
                                        e
                                    );
                                    continue;
                                }
                            }
                        }
                        DsMlsMessage::Plaintext(msg) => match group.verify(msg, &self.crypto) {
                            Ok(msg) => msg,
                            Err(e) => {
                                log::error!(
                                    "Error verifying MlsPlaintext: {:?} -  Dropping message.",
                                    e
                                );
                                continue;
                            }
                        },
                    };
                    drop(group);
                    let group = match groups.get_mut(&msg.group_id().to_vec()) {
                        Some(g) => g,
                        None => {
                            log::error!(
                                "Error getting group {:?} for a message. Dropping message.",
                                msg.group_id().as_slice()
                            );
                            continue;
                        }
                    };
                    match msg.content() {
                        MlsPlaintextContentType::Application(application_message) => {
                            let application_message =
                                String::from_utf8(application_message.as_slice().to_vec()).unwrap();
                            if group_name.is_none()
                                || group_name.clone().unwrap() == group.group_name
                            {
                                messages_out.push(application_message.clone());
                            }
                            group.conversation.add(application_message);
                        }
                        MlsPlaintextContentType::Proposal(_proposal) => {
                            // Store the proposal to use later when we got a
                            // corresponding commit.
                            group.pending_proposals.push(msg);
                        }
                        MlsPlaintextContentType::Commit(_commit) => {
                            match group.mls_group.borrow_mut().apply_commit(
                                &msg,
                                &(group
                                    .pending_proposals
                                    .iter()
                                    .collect::<Vec<&MlsPlaintext>>()),
                                &[], // TODO: store key packages.
                                None,
                                &self.crypto,
                            ) {
                                Ok(_) => (),
                                Err(e) => {
                                    let s = format!("Error applying commit: {:?}", e);
                                    log::error!("{}", s);
                                    return Err(s);
                                }
                            }
                            group.pending_proposals.clear();
                        }
                    }
                }
            }
        }
        log::trace!("done with messages ...");

        for c in self.backend.list_clients()?.drain(..) {
            if c.id != self.identity.borrow().credential.credential().identity()
                && self
                    .contacts
                    .insert(
                        c.id.clone(),
                        Contact {
                            username: c.client_name,
                            public_keys: c.key_packages,
                            id: c.id,
                        },
                    )
                    .is_some()
            {
                log::trace!("Updated client {}", "");
            }
        }
        log::trace!("done with clients ...");

        Ok(messages_out)
    }
/// Update the user. This involves:
/// * retrieving all new messages from the server
/// * update the contacts with all other clients known to the server
Update the user. This involves:
retrieving all new messages from the server
update the contacts with all other clients known to the server
Update the user . This involves : retrieving all new messages from the server update the contacts with all other clients known to the server
pub fn update(&mut self, group_name: Option<String>) -> Result<Vec<String>, String> {
        log::debug!("Updating {} ...", self.username);
        let mut messages_out = Vec::new();
        // Go through the list of messages and process or store them.
        for message in self.backend.recv_msgs(self)?.drain(..) {
            match message {
                Message::Welcome(welcome) => {
                    // Join the group. (Later we should ask the user to
                    // approve first ...)
                    self.join_group(welcome)?;
                }
                Message::MlsMessage(message) => {
                    let mut groups = self.groups.borrow_mut();
                    let mut group = match groups.get(message.group_id()) {
                        Some(g) => g.mls_group.borrow_mut(),
                        None => {
                            log::error!(
                                "Error getting group {:?} for a message. Dropping message.",
                                message.group_id()
                            );
                            continue;
                        }
                    };
                    let msg = match message {
                        DsMlsMessage::Ciphertext(ctxt) => {
                            match group.decrypt(&ctxt, &self.crypto) {
                                Ok(msg) => msg,
                                Err(e) => {
                                    log::error!(
                                        "Error decrypting MlsCiphertext: {:?} -  Dropping message.",
                                        e
                                    );
                                    continue;
                                }
                            }
                        }
                        DsMlsMessage::Plaintext(msg) => match group.verify(msg, &self.crypto) {
                            Ok(msg) => msg,
                            Err(e) => {
                                log::error!(
                                    "Error verifying MlsPlaintext: {:?} -  Dropping message.",
                                    e
                                );
                                continue;
                            }
                        },
                    };
                    drop(group);
                    let group = match groups.get_mut(&msg.group_id().to_vec()) {
                        Some(g) => g,
                        None => {
                            log::error!(
                                "Error getting group {:?} for a message. Dropping message.",
                                msg.group_id().as_slice()
                            );
                            continue;
                        }
                    };
                    match msg.content() {
                        MlsPlaintextContentType::Application(application_message) => {
                            let application_message =
                                String::from_utf8(application_message.as_slice().to_vec()).unwrap();
                            if group_name.is_none()
                                || group_name.clone().unwrap() == group.group_name
                            {
                                messages_out.push(application_message.clone());
                            }
                            group.conversation.add(application_message);
                        }
                        MlsPlaintextContentType::Proposal(_proposal) => {
                            // Store the proposal to use later when we got a
                            // corresponding commit.
                            group.pending_proposals.push(msg);
                        }
                        MlsPlaintextContentType::Commit(_commit) => {
                            match group.mls_group.borrow_mut().apply_commit(
                                &msg,
                                &(group
                                    .pending_proposals
                                    .iter()
                                    .collect::<Vec<&MlsPlaintext>>()),
                                &[], // TODO: store key packages.
                                None,
                                &self.crypto,
                            ) {
                                Ok(_) => (),
                                Err(e) => {
                                    let s = format!("Error applying commit: {:?}", e);
                                    log::error!("{}", s);
                                    return Err(s);
                                }
                            }
                            group.pending_proposals.clear();
                        }
                    }
                }
            }
        }
        log::trace!("done with messages ...");
        for c in self.backend.list_clients()?.drain(..) {
            if c.id != self.identity.borrow().credential.credential().identity()
                && self
                    .contacts
                    .insert(
                        c.id.clone(),
                        Contact {
                            username: c.client_name,
                            public_keys: c.key_packages,
                            id: c.id,
                        },
                    )
                    .is_some()
            {
                log::trace!("Updated client {}", "");
            }
        }
        log::trace!("done with clients ...");
        Ok(messages_out)
    }
pub fn update ( & mut self , group_name : Option < String > ) -> Result < Vec < String > , String > { log :: debug ! ( " " self username ) ; let mut messages_out = Vec :: new ( ) ; // Go through the list of messages and process or store them. for message in self . backend . recv_msgs ( self ) ? . drain ( .. ) { match message { Message :: Welcome ( welcome ) => { // Join the group. (Later we should ask the user to // approve first ...) self . join_group ( welcome ) ? ; } Message :: MlsMessage ( message ) => { let mut groups = self . groups . borrow_mut ( ) ; let mut group = match groups . get ( message . group_id ( ) ) { Some ( g ) => g . mls_group . borrow_mut ( ) , None => { log :: error ! ( " " message group_id ( ) ) ; continue ; } } ; let msg = match message { DsMlsMessage :: Ciphertext ( ctxt ) => { match group . decrypt ( & ctxt , & self . crypto ) { Ok ( msg ) => msg , Err ( e ) => { log :: error ! ( " " e ) ; continue ; } } } DsMlsMessage :: Plaintext ( msg ) => match group . verify ( msg , & self . crypto ) { Ok ( msg ) => msg , Err ( e ) => { log :: error ! ( " " e ) ; continue ; } } , } ; drop ( group ) ; let group = match groups . get_mut ( & msg . group_id ( ) . to_vec ( ) ) { Some ( g ) => g , None => { log :: error ! ( " " msg group_id ( ) as_slice ( ) ) ; continue ; } } ; match msg . content ( ) { MlsPlaintextContentType :: Application ( application_message ) => { let application_message = String :: from_utf8 ( application_message . as_slice ( ) . to_vec ( ) ) . unwrap ( ) ; if group_name . is_none ( ) || group_name . clone ( ) . unwrap ( ) == group . group_name { messages_out . push ( application_message . clone ( ) ) ; } group . conversation . add ( application_message ) ; } MlsPlaintextContentType :: Proposal ( _proposal ) => { // Store the proposal to use later when we got a // corresponding commit. group . pending_proposals . push ( msg ) ; } MlsPlaintextContentType :: Commit ( _commit ) => { match group . mls_group . borrow_mut ( ) . apply_commit ( & msg , & ( group . pending_proposals . iter ( ) . collect :: < Vec < & MlsPlaintext > > ( ) ) , & [ ] , // TODO: store key packages. None , & self . crypto , ) { Ok ( _ ) => ( ) , Err ( e ) => { let s = format ! ( " " e ) ; log :: error ! ( " " s ) ; return Err ( s ) ; } } group . pending_proposals . clear ( ) ; } } } } } log :: trace ! ( " " ) ; for c in self . backend . list_clients ( ) ? . drain ( .. ) { if c . id != self . identity . borrow ( ) . credential . credential ( ) . identity ( ) && self . contacts . insert ( c . id . clone ( ) , Contact { username : c . client_name , public_keys : c . key_packages , id : c . id , } , ) . is_some ( ) { log :: trace ! ( " " " " ) ; } } log :: trace ! ( " " ) ; Ok ( messages_out ) }
Update the user.
Update the user .
&mut self
group_name
Option<String>
&mut self
group_name
Option<String>