Skip to content

Commit

Permalink
Disentangle topic banner from smart message filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
andymandias committed Feb 27, 2024
1 parent 6ffff9e commit f46b1c5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 91 deletions.
6 changes: 3 additions & 3 deletions data/src/config/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ pub struct ServerMessages {
impl ServerMessages {
pub fn get(&self, server: &source::Server) -> Option<ServerMessage> {
match server {
source::Server::Join(_) => Some(self.join),
source::Server::Part(_) => Some(self.part),
source::Server::Quit(_) => Some(self.quit),
source::Server::Join => Some(self.join),
source::Server::Part => Some(self.part),
source::Server::Quit => Some(self.quit),
source::Server::ReplyTopic(_) => None,
source::Server::ReplyTopicWhoTime(_, _) => None,
source::Server::Topic(_, _) => None,
Expand Down
41 changes: 2 additions & 39 deletions data/src/history/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use itertools::Itertools;
use tokio::time::Instant;

use crate::config;
use crate::config::buffer::Exclude;
use crate::history::{self, History};
use crate::message::{self, Limit};
use crate::time::Posix;
Expand Down Expand Up @@ -452,50 +451,14 @@ impl Data {
.filter(|message| match message.target.source() {
message::Source::Server(Some(source)) => {
if let Some(source_config) = buffer_config.server_messages.get(source) {
match source_config.exclude {
Exclude::All => false,
Exclude::None => true,
Exclude::Smart(seconds) => {
let nick = match source {
message::source::Server::Join(nick) => nick,
message::source::Server::Part(nick) => nick,
message::source::Server::Quit(nick) => nick,
_ => &None,
};

if nick.is_some() {
!smart_filter_message(
message,
&seconds,
most_recent_messages.get(&nick.to_owned().unwrap()),
)
} else if let Some(nickname) =
message.text.split(' ').collect::<Vec<_>>().get(1)
{
let nick = Nick::from(*nickname);

!smart_filter_message(
message,
&seconds,
most_recent_messages.get(&nick),
)
} else {
true
}
}
}
!source_config.exclude
} else if buffer_config.topic_banner.enabled {
matches!(source, message::source::Server::Topic(_, _))
} else {
true
}
}
crate::message::Source::User(message_user) => {
most_recent_messages
.insert(message_user.nickname().to_owned(), message.server_time);

true
}
_ => true,
})
.collect::<Vec<_>>();

Expand Down
53 changes: 4 additions & 49 deletions data/src/message/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,17 @@ pub enum Source {
Internal(Internal),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Server {
#[serde(rename = "joinwithnick")]
Join(Option<Nick>),
#[serde(rename = "partwithnick")]
Part(Option<Nick>),
#[serde(rename = "quitwithnick")]
Quit(Option<Nick>),
#[serde(rename = "replytopic")]
Join,
Part,
Quit,
ReplyTopic(String),
#[serde(rename = "replytopicwhotime")]
ReplyTopicWhoTime(Nick, Posix),
#[serde(rename = "topic")]
Topic(Option<String>, Nick),
}

impl<'de> Deserialize<'de> for Server {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
enum Mapping {
Join,
JoinWithNick(Option<Nick>),
Part,
PartWithNick(Option<Nick>),
Quit,
QuitWithNick(Option<Nick>),
ReplyTopic(String),
ReplyTopicWhoTime(Nick, Posix),
Topic(Option<String>, Nick),
}

if let Ok(mapping) = Mapping::deserialize(deserializer) {
match mapping {
Mapping::Join => Ok(Server::Join(None)),
Mapping::JoinWithNick(nick) => Ok(Server::Join(nick)),
Mapping::Part => Ok(Server::Part(None)),
Mapping::PartWithNick(nick) => Ok(Server::Part(nick)),
Mapping::Quit => Ok(Server::Quit(None)),
Mapping::QuitWithNick(nick) => Ok(Server::Quit(nick)),
Mapping::ReplyTopic(topic) => Ok(Server::ReplyTopic(topic)),
Mapping::ReplyTopicWhoTime(nick, posix) => {
Ok(Server::ReplyTopicWhoTime(nick, posix))
}
Mapping::Topic(topic, nick) => Ok(Server::Topic(topic, nick)),
}
} else {
Err(D::Error::custom("could not map to Server enum"))
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Internal {
Status(Status),
Expand Down

0 comments on commit f46b1c5

Please sign in to comment.