Skip to content

Commit

Permalink
nickref refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
casperstorm committed Mar 11, 2024
1 parent 58eaf10 commit 4a26081
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
10 changes: 5 additions & 5 deletions data/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,10 +703,10 @@ impl Client {

pub fn user_from_target(
&self,
user: &User,
nick: NickRef,
target: &str,
) -> User {
let user = user.clone();
let user = User::from(nick.to_owned());

let Some(channel) = self.chanmap.get(target) else {
return user;
Expand Down Expand Up @@ -841,13 +841,13 @@ impl Map {

pub fn own_user(&self, server: &Server, target: Option<String>) -> Option<User> {
let client = self.client(server)?;
let partial_user = User::from(Client::nickname(client).to_owned());
let nickname = Client::nickname(client);

let Some(target) = target else {
return Some(partial_user);
return Some(User::from(nickname.to_owned()));
};

Some(client.user_from_target(&partial_user, &target))
Some(client.user_from_target(nickname, &target))
}

pub fn receive(&mut self, server: &Server, message: message::Encoded) -> Vec<Event> {
Expand Down
24 changes: 12 additions & 12 deletions data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ fn target(message: Encoded, our_nick: &Nick, client: &Client) -> Option<Target>
}
Command::PRIVMSG(target, text) => {
let is_action = is_action(&text);
let source = |user| {
let user = client.user_from_target(&user, &target);
let source = |nickname| {
let user = client.user_from_target(nickname, &target);

if is_action {
Source::Action
Expand All @@ -186,23 +186,23 @@ fn target(message: Encoded, our_nick: &Nick, client: &Client) -> Option<Target>
match (proto::is_channel(&target), user) {
(true, Some(user)) => Some(Target::Channel {
channel: target.clone(),
source: source(user),
source: source(user.nickname()),
}),
(false, Some(user)) => {
let target = User::try_from(target.as_str()).ok()?;

(target.nickname() == *our_nick).then(|| Target::Query {
nick: user.nickname().to_owned(),
source: source(user),
source: source(user.nickname()),
})
}
_ => None,
}
}
Command::NOTICE(target, text) => {
let is_action = is_action(&text);
let source = |user| {
let user = client.user_from_target(&user, &target);
let source = |nickname| {
let user = client.user_from_target(nickname, &target);

if is_action {
Source::Action
Expand All @@ -214,14 +214,14 @@ fn target(message: Encoded, our_nick: &Nick, client: &Client) -> Option<Target>
match (proto::is_channel(&target), user) {
(true, Some(user)) => Some(Target::Channel {
channel: target.clone(),
source: source(user),
source: source(user.nickname()),
}),
(false, Some(user)) => {
let target = User::try_from(target.as_str()).ok()?;

(target.nickname() == *our_nick).then(|| Target::Query {
nick: user.nickname().to_owned(),
source: source(user),
source: source(user.nickname()),
})
}
_ => Some(Target::Server {
Expand Down Expand Up @@ -290,14 +290,14 @@ fn text(message: &Encoded, our_nick: &Nick, config: &Config, client: &Client) ->
let user = message.user();
match &message.command {
Command::TOPIC(target, topic) => {
let user = client.user_from_target(&user?, target);
let user = client.user_from_target(user?.nickname(), target);
let topic = topic.as_ref()?;

Some(format!(" ∙ {user} changed topic to {topic}"))
}
Command::PART(target, text) => {
let user = client
.user_from_target(&user?, target)
.user_from_target(user?.nickname(), target)
.formatted(config.buffer.server_messages.part.username_format);
let text = text
.as_ref()
Expand All @@ -307,7 +307,7 @@ fn text(message: &Encoded, our_nick: &Nick, config: &Config, client: &Client) ->
Some(format!("⟵ {user} has left the channel{text}"))
}
Command::JOIN(target, _) => {
let user = client.user_from_target(&user?, target);
let user = client.user_from_target(user?.nickname(), target);

(user.nickname() != *our_nick).then(|| {
format!(
Expand All @@ -317,7 +317,7 @@ fn text(message: &Encoded, our_nick: &Nick, config: &Config, client: &Client) ->
})
}
Command::MODE(target, modes, args) if proto::is_channel(target) => {
let user = client.user_from_target(&user?, target);
let user = client.user_from_target(user?.nickname(), target);
let modes = modes
.iter()
.map(|mode| mode.to_string())
Expand Down

0 comments on commit 4a26081

Please sign in to comment.