Skip to content

Commit

Permalink
Add named fields & only sync specific server
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Jul 6, 2023
1 parent 2599996 commit 5b2a9eb
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 60 deletions.
51 changes: 27 additions & 24 deletions data/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::{BTreeMap, HashMap};
use irc::client::Client;
use itertools::Itertools;

use crate::user::NickRef;
use crate::user::{Nick, NickRef};
use crate::{message, Message, Server, User};

#[derive(Debug, Clone, Copy)]
Expand All @@ -27,8 +27,15 @@ pub enum State {

#[derive(Debug)]
pub enum Brodcast {
Quit(User, Option<String>),
Nickname(String, String, bool),
Quit {
user: User,
comment: Option<String>,
},
Nickname {
old_user: User,
new_nick: Nick,
ourself: bool,
},
}

#[derive(Debug)]
Expand Down Expand Up @@ -83,23 +90,18 @@ impl Connection {

match &message.command {
Command::NICK(nick) => {
let Some(old_nick) = message.prefix.as_ref().and_then(|prefix| match prefix {
irc::proto::Prefix::ServerName(_) => None,
irc::proto::Prefix::Nickname(nick, _, _) => Some(nick),
}) else {
return None;
};

let changed_own_nickname = self.resolved_nick.as_ref() == Some(old_nick);
if changed_own_nickname {
let old_user = message.user()?;
let ourself = self.nickname() == old_user.nickname();

if ourself {
self.resolved_nick = Some(nick.clone());
}

return Some(Event::Brodcast(Brodcast::Nickname(
nick.clone(),
old_nick.clone(),
changed_own_nickname,
)));
return Some(Event::Brodcast(Brodcast::Nickname {
old_user,
new_nick: Nick::from(nick.as_str()),
ourself,
}));
}
Command::Response(Response::RPL_WELCOME, args) => {
if let Some(nick) = args.first() {
Expand All @@ -109,7 +111,10 @@ impl Connection {
Command::QUIT(comment) => {
let user = message.user()?;

return Some(Event::Brodcast(Brodcast::Quit(user, comment.clone())));
return Some(Event::Brodcast(Brodcast::Quit {
user,
comment: comment.clone(),
}));
}
_ => {}
}
Expand Down Expand Up @@ -212,12 +217,10 @@ impl Map {
.and_then(|connection| connection.receive(message))
}

pub fn sync(&mut self) {
self.0.values_mut().for_each(|state| {
if let State::Ready(connection) = state {
connection.sync();
}
});
pub fn sync(&mut self, server: &Server) {
if let Some(State::Ready(connection)) = self.0.get_mut(server) {
connection.sync();
}
}

pub fn send(&mut self, server: &Server, message: message::Encoded) {
Expand Down
42 changes: 25 additions & 17 deletions data/src/history/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,23 +284,31 @@ impl Manager {
message::broadcast::quit(user_channels, user_query, &user, &comment)
}
Broadcast::Nickname {
new_nick,
old_nick,
changed_own_nickname,
new_nick,
ourself,
user_channels,
} => {
let user_query = queries.find(|nick| {
let old_nick = NickRef::from(old_nick.as_str());
old_nick == *nick
});

message::broadcast::nickname(
user_channels,
user_query,
&new_nick,
&old_nick,
changed_own_nickname,
)
if ourself {
// If ourself, broadcast to all query channels (since we are in all of them)
message::broadcast::nickname(
user_channels,
queries,
&old_nick,
&new_nick,
ourself,
)
} else {
// Otherwise just the query channel of the user w/ nick change
let user_query = queries.find(|nick| old_nick == *nick);
message::broadcast::nickname(
user_channels,
user_query,
&old_nick,
&new_nick,
ourself,
)
}
}
};

Expand Down Expand Up @@ -472,9 +480,9 @@ pub enum Broadcast {
user_channels: Vec<String>,
},
Nickname {
new_nick: String,
old_nick: String,
changed_own_nickname: bool,
old_nick: Nick,
new_nick: Nick,
ourself: bool,
user_channels: Vec<String>,
},
}
8 changes: 4 additions & 4 deletions data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,11 @@ pub(crate) mod broadcast {
pub fn nickname(
channels: impl IntoIterator<Item = String>,
queries: impl IntoIterator<Item = Nick>,
new_nick: &str,
old_nick: &str,
changed_own_nickname: bool,
old_nick: &Nick,
new_nick: &Nick,
ourself: bool,
) -> Vec<Message> {
let text = if changed_own_nickname {
let text = if ourself {
format!(" ∙ You're now known as {new_nick}")
} else {
format!(" ∙ {old_nick} is now known as {new_nick}")
Expand Down
22 changes: 11 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl Application for Halloy {
dashboard.record_message(&server, message);
}
data::client::Event::Brodcast(brodcast) => match brodcast {
data::client::Brodcast::Quit(user, comment) => {
data::client::Brodcast::Quit { user, comment } => {
let user_channels = self
.clients
.get_user_channels(&server, user.nickname());
Expand All @@ -336,20 +336,20 @@ impl Application for Halloy {
user_channels,
);
}
data::client::Brodcast::Nickname(
data::client::Brodcast::Nickname {
old_user,
new_nick,
old_nick,
changed_own_nickname,
) => {
let user_channels = self
.clients
.get_user_channels(&server, old_nick.as_str().into());
ourself,
} => {
let old_nick = old_user.nickname();
let user_channels =
self.clients.get_user_channels(&server, old_nick);

dashboard.broadcast_nickname(
&server,
old_nick.to_owned(),
new_nick,
old_nick,
changed_own_nickname,
ourself,
user_channels,
);
}
Expand All @@ -360,7 +360,7 @@ impl Application for Halloy {

// Must be called after receiving message batches to ensure
// user & channel lists are in sync
self.clients.sync();
self.clients.sync(&server);

Command::none()
}
Expand Down
9 changes: 5 additions & 4 deletions src/screen/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod side_menu;
use std::time::{Duration, Instant};

use data::history::manager::Broadcast;
use data::user::Nick;
use data::{dashboard, history, Config, Server, User};
use iced::widget::pane_grid::{self, PaneGrid};
use iced::widget::{container, row};
Expand Down Expand Up @@ -516,17 +517,17 @@ impl Dashboard {
pub fn broadcast_nickname(
&mut self,
server: &Server,
new_nick: String,
old_nick: String,
changed_own_nickname: bool,
old_nick: Nick,
new_nick: Nick,
ourself: bool,
user_channels: Vec<String>,
) {
self.history.broadcast(
server,
Broadcast::Nickname {
new_nick,
old_nick,
changed_own_nickname,
ourself,
user_channels,
},
);
Expand Down

0 comments on commit 5b2a9eb

Please sign in to comment.