Skip to content

Commit

Permalink
broadcast nickname change
Browse files Browse the repository at this point in the history
  • Loading branch information
casperstorm committed Jul 6, 2023
1 parent 68585a2 commit e3bbcee
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
11 changes: 10 additions & 1 deletion data/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum State {
#[derive(Debug)]
pub enum Brodcast {
Quit(User, Option<String>),
Nickname(User, String, bool),
}

#[derive(Debug)]
Expand Down Expand Up @@ -89,9 +90,17 @@ impl Connection {
return None;
};

if self.resolved_nick.as_ref() == Some(old_nick) {
let changed_own_nickname = self.resolved_nick.as_ref() == Some(old_nick);
if changed_own_nickname {
self.resolved_nick = Some(nick.clone());
}

let user = message.user()?;
return Some(Event::Brodcast(Brodcast::Nickname(
user,
nick.clone(),
changed_own_nickname,
)));
}
Command::Response(Response::RPL_WELCOME, args) => {
if let Some(nick) = args.first() {
Expand Down
21 changes: 21 additions & 0 deletions data/src/history/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ impl Manager {

message::broadcast::quit(user_channels, user_query, &user, &comment)
}
Broadcast::Nickname {
user,
new_nick,
changed_own_nickname,
user_channels,
} => {
let user_query = queries.find(|nick| user.nickname() == *nick);
message::broadcast::nickname(
user_channels,
user_query,
&user,
&new_nick,
changed_own_nickname,
)
}
};

messages.into_iter().for_each(|message| {
Expand Down Expand Up @@ -452,4 +467,10 @@ pub enum Broadcast {
comment: Option<String>,
user_channels: Vec<String>,
},
Nickname {
user: User,
new_nick: String,
changed_own_nickname: bool,
user_channels: Vec<String>,
},
}
16 changes: 16 additions & 0 deletions data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,20 @@ pub(crate) mod broadcast {

expand(channels, queries, false, text)
}

pub fn nickname(
channels: impl IntoIterator<Item = String>,
queries: impl IntoIterator<Item = Nick>,
user: &User,
new_nick: &str,
changed_own_nickname: bool,
) -> Vec<Message> {
let text = if changed_own_nickname {
format!(" ∙ You're now known as {new_nick}")
} else {
format!(" ∙ {user} is now known as {new_nick}")
};

expand(channels, queries, false, text)
}
}
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,23 @@ impl Application for Halloy {
user_channels,
);
}
data::client::Brodcast::Nickname(
user,
new_nick,
changed_own_nickname,
) => {
let user_channels = self
.clients
.get_user_channels(&server, user.nickname());

dashboard.broadcast_nickname(
&server,
user,
new_nick,
changed_own_nickname,
user_channels,
);
}
},
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/screen/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,25 @@ impl Dashboard {
);
}

pub fn broadcast_nickname(
&mut self,
server: &Server,
user: User,
new_nick: String,
changed_own_nickname: bool,
user_channels: Vec<String>,
) {
self.history.broadcast(
server,
Broadcast::Nickname {
user,
new_nick,
changed_own_nickname,
user_channels,
},
);
}

pub fn broadcast_disconnected(&mut self, server: &Server) {
self.history.broadcast(server, Broadcast::Disconnected);
}
Expand Down

0 comments on commit e3bbcee

Please sign in to comment.