Room: simplify add_session; abstract away send_subject
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
506e288b90
commit
ee4ea6dd9a
1 changed files with 73 additions and 59 deletions
132
src/room.rs
132
src/room.rs
|
@ -182,6 +182,38 @@ impl Room {
|
|||
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>(
|
||||
&mut self,
|
||||
component: &mut C,
|
||||
|
@ -197,70 +229,52 @@ 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?;
|
||||
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),
|
||||
}
|
||||
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,
|
||||
}
|
||||
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?;
|
||||
}
|
||||
.into()];
|
||||
component.send_stanza(subject).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(())
|
||||
|
|
Loading…
Reference in a new issue