From 562aadb488e2a37b1b48b96c578ca7ada329d51f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 18 Sep 2022 14:07:53 +0200 Subject: [PATCH] Room: simplify add_session; abstract away send_subject MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/room.rs | 131 +++++++++++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 57 deletions(-) diff --git a/src/room.rs b/src/room.rs index 576e105..f807b2c 100644 --- a/src/room.rs +++ b/src/room.rs @@ -182,6 +182,38 @@ impl Room { Ok(()) } + pub async fn send_subject( + &mut self, + component: &mut C, + realjid: Session, + occupant: Occupant, + ) -> Result<(), Error> { + debug!("Sending subject!"); + if self.subject.is_none() { + let subject = String::from(""); + let setter = occupant; + let stamp = DateTime(Utc::now().with_timezone(&FixedOffset::east(0))); + self.subject = Some((subject, setter, stamp)); + } + + let mut subject = Message::new(Some(Jid::Full(realjid))); + subject.from = Some(Jid::Full( + self.subject.as_ref().unwrap().1.participant.clone(), + )); + subject.subjects.insert( + String::from("en"), + Subject(self.subject.as_ref().unwrap().0.clone()), + ); + subject.type_ = MessageType::Groupchat; + subject.payloads = vec![Delay { + from: Some(Jid::Bare(self.jid.clone())), + stamp: self.subject.as_ref().unwrap().2.clone(), + data: None, + } + .into()]; + component.send_stanza(subject).await + } + pub async fn add_session( &mut self, component: &mut C, @@ -197,66 +229,51 @@ impl Room { Ok(()) })?; - if let Some(occupant) = self.occupants.get(&new_nick) { - // TODO: Use get_mut combined with immutable usage of occupant in the - // broadcast_presence call below. Instead of get / reinsert. - let mut occupant = occupant.clone(); - match occupant.add_session(realjid.clone()) { - Ok(_) => (), - Err(Error::SessionAlreadyExists(_)) => { - self.broadcast_presence( - component, - &occupant.participant, - &realjid, - &new_nick, - BroadcastPresence::Resync, - ).await?; - }, - err => err?, + let mode: Option = { + if let Some(occupant) = self.occupants.get_mut(&new_nick) { + match occupant.add_session(realjid.clone()) { + Ok(_) => None, + Err(Error::SessionAlreadyExists(_)) => { + Some(BroadcastPresence::Resync) + }, + Err(err) => return Err(err), + } + } else { + Some(BroadcastPresence::Join) } - self.occupants.insert(new_nick, occupant); + }; - // TODO: Send presence - } else { - debug!("{} is joining {}", realjid, self.jid); - - let new_occupant = Occupant::new(&self, realjid.clone(), new_nick.clone()); - self.broadcast_presence( - component, - &new_occupant.participant, - &realjid, - &new_nick, - BroadcastPresence::Join, - ).await?; - - // Add into occupants - let _ = self.occupants.insert(new_nick.clone(), new_occupant.clone()); - - // Send subject - debug!("Sending subject!"); - if self.subject.is_none() { - let subject = String::from(""); - let setter = new_occupant; - let stamp = DateTime(Utc::now().with_timezone(&FixedOffset::east(0))); - self.subject = Some((subject, setter, stamp)); - } - - let mut subject = Message::new(Some(Jid::Full(realjid))); - subject.from = Some(Jid::Full( - self.subject.as_ref().unwrap().1.participant.clone(), - )); - subject.subjects.insert( - String::from("en"), - Subject(self.subject.as_ref().unwrap().0.clone()), + if ! self.occupants.contains_key(&new_nick) { + let _ = self.occupants.insert( + new_nick.clone(), + Occupant::new(&self, realjid.clone(), new_nick.clone()), ); - subject.type_ = MessageType::Groupchat; - subject.payloads = vec![Delay { - from: Some(Jid::Bare(self.jid.clone())), - stamp: self.subject.as_ref().unwrap().2.clone(), - data: None, - } - .into()]; - component.send_stanza(subject).await?; + } + let occupant = self.occupants.get(&new_nick).unwrap(); + + match mode { + Some(BroadcastPresence::Resync) => { + self.broadcast_presence( + component, + &occupant.participant, + &realjid, + &new_nick, + BroadcastPresence::Resync, + ).await?; + }, + Some(BroadcastPresence::Join) => { + debug!("{} is joining {}", realjid, self.jid); + + self.broadcast_presence( + component, + &occupant.participant, + &realjid, + &new_nick, + BroadcastPresence::Join, + ).await?; + self.send_subject(component, realjid, occupant.clone()).await?; + }, + _ => (), } Ok(())