Skip to content

Commit

Permalink
Add highlights buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Oct 4, 2024
1 parent 01335de commit c46cff9
Show file tree
Hide file tree
Showing 22 changed files with 553 additions and 142 deletions.
16 changes: 11 additions & 5 deletions data/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ pub enum State {
#[derive(Debug)]
pub enum Notification {
DirectMessage(User),
Highlight(User, String),
Highlight {
enabled: bool,
user: User,
channel: String,
},
MonitoredOnline(Vec<User>),
MonitoredOffline(Vec<Nick>),
}
Expand Down Expand Up @@ -650,13 +654,15 @@ impl Client {
}

// Highlight notification
if message::references_user_text(user.nickname(), self.nickname(), text)
&& self.highlight_blackout.allow_highlights()
{
if message::references_user_text(user.nickname(), self.nickname(), text) {
return Some(vec![Event::Notification(
message.clone(),
self.nickname().to_owned(),
Notification::Highlight(user, channel.clone()),
Notification::Highlight {
enabled: self.highlight_blackout.allow_highlights(),
user,
channel: channel.clone(),
},
)]);
} else if user.nickname() == self.nickname() && context.is_some() {
// If we sent (echo) & context exists (we sent from this client), ignore
Expand Down
5 changes: 5 additions & 0 deletions data/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum Kind {
Channel(String),
Query(Nick),
Logs,
Highlights,
}

impl Kind {
Expand All @@ -41,6 +42,7 @@ impl Kind {
Kind::Channel(channel) => Some(channel),
Kind::Query(nick) => Some(nick.as_ref()),
Kind::Logs => None,
Kind::Highlights => None,
}
}
}
Expand All @@ -52,6 +54,7 @@ impl fmt::Display for Kind {
Kind::Channel(channel) => write!(f, "channel {channel}"),
Kind::Query(nick) => write!(f, "user {}", nick),
Kind::Logs => write!(f, "logs"),
Kind::Highlights => write!(f, "highlights"),
}
}
}
Expand All @@ -63,6 +66,7 @@ impl From<message::Target> for Kind {
message::Target::Channel { channel, .. } => Kind::Channel(channel),
message::Target::Query { nick, .. } => Kind::Query(nick),
message::Target::Logs => Kind::Logs,
message::Target::Highlights { .. } => Kind::Highlights,
}
}
}
Expand Down Expand Up @@ -159,6 +163,7 @@ async fn path(server: &server::Server, kind: &Kind) -> Result<PathBuf, Error> {
Kind::Channel(channel) => format!("{server}channel{channel}"),
Kind::Query(nick) => format!("{server}nickname{}", nick),
Kind::Logs => "logs".to_string(),
Kind::Highlights => "highlights".to_string(),
};

let hashed_name = seahash::hash(name.as_bytes());
Expand Down
18 changes: 18 additions & 0 deletions data/src/history/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ impl Resource {
kind: history::Kind::Logs,
}
}

pub fn highlights() -> Self {
Self {
server: server::HIGHLIGHTS.clone(),
kind: history::Kind::Highlights,
}
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -250,6 +257,17 @@ impl Manager {
)
}

pub fn record_highlight(
&mut self,
message: crate::Message,
) -> Option<impl Future<Output = Message>> {
self.data.add_message(
server::HIGHLIGHTS.clone(),
history::Kind::Highlights,
message,
)
}

pub fn update_read_marker(
&mut self,
server: Server,
Expand Down
1 change: 1 addition & 0 deletions data/src/history/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ async fn path(server: &server::Server, kind: &Kind) -> Result<PathBuf, Error> {
Kind::Channel(channel) => format!("{server}channel{channel}-metadata"),
Kind::Query(nick) => format!("{server}nickname{}-metadata", nick),
Kind::Logs => "logs-metadata".to_string(),
Kind::Highlights => "highlights-metadata".to_string(),
};

let hashed_name = seahash::hash(name.as_bytes());
Expand Down
41 changes: 39 additions & 2 deletions data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use self::source::Source;
use crate::config::buffer::UsernameFormat;
use crate::time::{self, Posix};
use crate::user::{Nick, NickRef};
use crate::{ctcp, Config, User};
use crate::{ctcp, Config, Server, User};

// References:
// - https://datatracker.ietf.org/doc/html/rfc1738#section-5
Expand Down Expand Up @@ -118,6 +118,11 @@ pub enum Target {
source: Source,
},
Logs,
Highlights {
server: Server,
channel: Channel,
source: Source,
},
}

impl Target {
Expand All @@ -127,6 +132,7 @@ impl Target {
Target::Channel { prefix, .. } => prefix.as_ref(),
Target::Query { .. } => None,
Target::Logs => None,
Target::Highlights { .. } => None,
}
}

Expand All @@ -136,6 +142,7 @@ impl Target {
Target::Channel { source, .. } => source,
Target::Query { source, .. } => source,
Target::Logs => &Source::Internal(source::Internal::Logs),
Target::Highlights { source, .. } => source,
}
}
}
Expand Down Expand Up @@ -287,6 +294,23 @@ impl Message {
hash,
}
}

pub fn into_highlight(mut self, server: Server) -> Option<Self> {
self.target = match self.target {
Target::Channel {
channel,
source: Source::User(user),
..
} => Target::Highlights {
server,
channel,
source: Source::User(user),
},
_ => return None,
};

Some(self)
}
}

impl Serialize for Message {
Expand Down Expand Up @@ -1147,14 +1171,27 @@ pub fn references_user(sender: NickRef, own_nick: NickRef, message: &Message) ->
}

pub fn references_user_text(sender: NickRef, own_nick: NickRef, text: &str) -> bool {
sender != own_nick && text_references_nickname(text, own_nick).is_some()
sender != own_nick
&& text
.chars()
.group_by(|c| c.is_whitespace())
.into_iter()
.any(|(is_whitespace, chars)| {
if !is_whitespace {
let text = chars.collect::<String>();
text_references_nickname(&text, own_nick).is_some()
} else {
false
}
})
}

#[derive(Debug, Clone)]
pub enum Link {
Channel(String),
Url(String),
User(User),
GoToMessage(Server, String, Hash),
}

fn fail_as_none<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
Expand Down
1 change: 1 addition & 0 deletions data/src/pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum Pane {
Empty,
FileTransfers,
Logs,
Highlights,
}

#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
Expand Down
1 change: 1 addition & 0 deletions data/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::config::Error;

// Hack since log messages are app wide and not scoped to any server
pub static LOGS: Lazy<Server> = Lazy::new(|| Server("<halloy-logs>".to_string()));
pub static HIGHLIGHTS: Lazy<Server> = Lazy::new(|| Server("<halloy-highlights>".to_string()));

pub type Handle = Sender<proto::Message>;

Expand Down
4 changes: 3 additions & 1 deletion src/appearance/theme/selectable_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ impl selectable_rich_text::Link for message::Link {
fn underline(&self) -> bool {
match self {
data::message::Link::Url(_) => true,
data::message::Link::User(_) | data::message::Link::Channel(_) => false,
data::message::Link::User(_)
| data::message::Link::Channel(_)
| data::message::Link::GoToMessage(..) => false,
}
}
}
Loading

0 comments on commit c46cff9

Please sign in to comment.