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,6 +182,38 @@ impl Room {
Ok(()) Ok(())
} }
pub async fn send_subject<C: ComponentTrait>(
&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<C: ComponentTrait>( pub async fn add_session<C: ComponentTrait>(
&mut self, &mut self,
component: &mut C, component: &mut C,
@ -197,66 +229,51 @@ impl Room {
Ok(()) Ok(())
})?; })?;
if let Some(occupant) = self.occupants.get(&new_nick) { let mode: Option<BroadcastPresence> = {
// TODO: Use get_mut combined with immutable usage of occupant in the if let Some(occupant) = self.occupants.get_mut(&new_nick) {
// broadcast_presence call below. Instead of get / reinsert. match occupant.add_session(realjid.clone()) {
let mut occupant = occupant.clone(); Ok(_) => None,
match occupant.add_session(realjid.clone()) { Err(Error::SessionAlreadyExists(_)) => {
Ok(_) => (), Some(BroadcastPresence::Resync)
Err(Error::SessionAlreadyExists(_)) => { },
self.broadcast_presence( Err(err) => return Err(err),
component, }
&occupant.participant, } else {
&realjid, Some(BroadcastPresence::Join)
&new_nick,
BroadcastPresence::Resync,
).await?;
},
err => err?,
} }
self.occupants.insert(new_nick, occupant); };
// TODO: Send presence if ! self.occupants.contains_key(&new_nick) {
} else { let _ = self.occupants.insert(
debug!("{} is joining {}", realjid, self.jid); new_nick.clone(),
Occupant::new(&self, realjid.clone(), new_nick.clone()),
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()),
); );
subject.type_ = MessageType::Groupchat; }
subject.payloads = vec![Delay { let occupant = self.occupants.get(&new_nick).unwrap();
from: Some(Jid::Bare(self.jid.clone())),
stamp: self.subject.as_ref().unwrap().2.clone(), match mode {
data: None, Some(BroadcastPresence::Resync) => {
} self.broadcast_presence(
.into()]; component,
component.send_stanza(subject).await?; &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(()) Ok(())