Room: simplify add_session; abstract away send_subject

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-09-18 14:07:53 +02:00
parent 44bec0e232
commit 562aadb488
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -182,61 +182,16 @@ impl Room {
Ok(())
}
pub async fn add_session<C: ComponentTrait>(
pub async fn send_subject<C: ComponentTrait>(
&mut self,
component: &mut C,
realjid: Session,
new_nick: Nick,
occupant: Occupant,
) -> Result<(), Error> {
// Ensure nick isn't already assigned
let _ = self.occupants.iter().try_for_each(|(nick, occupant)| {
let new_nick = new_nick.as_str();
if new_nick == nick && occupant.real != BareJid::from(realjid.clone()) {
return Err(Error::NickAlreadyAssigned(String::from(new_nick)));
}
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?,
}
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 setter = occupant;
let stamp = DateTime(Utc::now().with_timezone(&FixedOffset::east(0)));
self.subject = Some((subject, setter, stamp));
}
@ -256,7 +211,69 @@ impl Room {
data: None,
}
.into()];
component.send_stanza(subject).await?;
component.send_stanza(subject).await
}
pub async fn add_session<C: ComponentTrait>(
&mut self,
component: &mut C,
realjid: Session,
new_nick: Nick,
) -> Result<(), Error> {
// Ensure nick isn't already assigned
let _ = self.occupants.iter().try_for_each(|(nick, occupant)| {
let new_nick = new_nick.as_str();
if new_nick == nick && occupant.real != BareJid::from(realjid.clone()) {
return Err(Error::NickAlreadyAssigned(String::from(new_nick)));
}
Ok(())
})?;
let mode: Option<BroadcastPresence> = {
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)
}
};
if ! self.occupants.contains_key(&new_nick) {
let _ = self.occupants.insert(
new_nick.clone(),
Occupant::new(&self, realjid.clone(), new_nick.clone()),
);
}
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(())