Skip to content

Commit

Permalink
Timeout delay for all notifications #600
Browse files Browse the repository at this point in the history
  • Loading branch information
englut committed Dec 19, 2024
1 parent 93bdb32 commit 9cbcd61
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 100 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Added:
- New configuration options
- Ability to disable dimming of away usernames. See [buffer configuartion](https://halloy.squidowl.org/configuration/buffer/away.html).
- Enable support for IRCv3 `chathistory`
- Timeout delay for notifications

# 2024.14 (2024-10-29)

Expand Down
12 changes: 11 additions & 1 deletion book/src/configuration/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Following notifications are available:
| `disconnected` | Triggered when a server disconnects |
| `file_transfer_request` | Triggered when a file transfer request is received |
| `highlight` | Triggered when you were highlighted in a buffer |
| `monitored_online` | Triggered when a user you're monitoring is online |
| `monitored_offline` | Triggered when a user you're monitoring is offline |
| `reconnected` | Triggered when a server reconnects |


Expand All @@ -38,4 +40,12 @@ Notification should trigger a OS toast.

- **type**: boolean
- **values**: `true`, `false`
- **default**: `false`
- **default**: `false`

## `delay`

Delay in milliseconds before triggering the next notification.

- **type**: integer
- **values**: `0` or greater
- **default**: `500`
6 changes: 5 additions & 1 deletion data/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ pub enum State {
Ready(Client),
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum Notification {
Connected,
Disconnected,
Reconnected,
DirectMessage(User),
Highlight {
enabled: bool,
user: User,
channel: String,
},
FileTransferRequest(Nick),
MonitoredOnline(Vec<User>),
MonitoredOffline(Vec<Nick>),
}
Expand Down
3 changes: 3 additions & 0 deletions data/src/config/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ pub struct Notification<T = String> {
#[serde(default)]
pub show_toast: bool,
pub sound: Option<T>,
pub delay: Option<u64>,
}

impl<T> Default for Notification<T> {
fn default() -> Self {
Self {
show_toast: false,
sound: None,
delay: Some(500),
}
}
}
Expand Down Expand Up @@ -61,6 +63,7 @@ impl Notifications {
Ok(Notification {
show_toast: notification.show_toast,
sound: notification.sound.as_deref().map(Sound::load).transpose()?,
delay: notification.delay,
})
};

Expand Down
1 change: 0 additions & 1 deletion irc/proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ macro_rules! command {
mod tests {
use super::*;


// Reference: https://defs.ircdocs.horse/defs/chanmembers
const CHANNEL_MEMBERSHIP_PREFIXES: &[char] = &['~', '&', '!', '@', '%', '+'];

Expand Down
75 changes: 34 additions & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use chrono::Utc;
use data::config::{self, Config};
use data::history::{self, manager::Broadcast};
use data::version::Version;
use data::{environment, server, version, Server, Url, User};
use data::{client::Notification, environment, server, version, Server, Url, User};
use iced::widget::{column, container};
use iced::{padding, Length, Subscription, Task};
use screen::{dashboard, help, migration, welcome};
Expand All @@ -34,6 +34,7 @@ use tokio_stream::wrappers::ReceiverStream;

use self::event::{events, Event};
use self::modal::Modal;
use self::notification::Notifications;
use self::widget::Element;
use self::window::Window;

Expand Down Expand Up @@ -131,6 +132,7 @@ struct Halloy {
modal: Option<Modal>,
main_window: Window,
pending_logs: Vec<data::log::Record>,
notifications: Notifications,
}

impl Halloy {
Expand Down Expand Up @@ -194,6 +196,7 @@ impl Halloy {
modal: None,
main_window,
pending_logs: vec![],
notifications: Notifications::new(),
},
command,
)
Expand Down Expand Up @@ -432,7 +435,11 @@ impl Halloy {
.broadcast(&server, &self.config, sent_time, Broadcast::Connecting)
.map(Message::Dashboard)
} else {
notification::disconnected(&self.config.notifications, &server);
self.notifications.notify(
&self.config.notifications,
&Notification::Disconnected,
Some(&server),
);

dashboard
.broadcast(
Expand All @@ -457,13 +464,21 @@ impl Halloy {
};

if is_initial {
notification::connected(&self.config.notifications, &server);
self.notifications.notify(
&self.config.notifications,
&Notification::Connected,
Some(&server),
);

dashboard
.broadcast(&server, &self.config, sent_time, Broadcast::Connected)
.map(Message::Dashboard)
} else {
notification::reconnected(&self.config.notifications, &server);
self.notifications.notify(
&self.config.notifications,
&Notification::Reconnected,
Some(&server),
);

dashboard
.broadcast(&server, &self.config, sent_time, Broadcast::Reconnected)
Expand Down Expand Up @@ -689,58 +704,36 @@ impl Halloy {
}
}

match notification {
match &notification {
data::client::Notification::DirectMessage(user) => {
// only send notification if query has unread
// or if window is not focused
if dashboard.history().has_unread(
&history::Kind::Query(
server.clone(),
user.nickname().to_owned(),
),
) || !self.main_window.focused
{
notification::direct_message(
self.notifications.notify(
&self.config.notifications,
user.nickname(),
&notification,
None::<Server>,
);
}
}
data::client::Notification::Highlight {
enabled,
user,
channel,
} => {
if enabled {
notification::highlight(
&self.config.notifications,
user.nickname(),
channel,
);
}
}
data::client::Notification::MonitoredOnline(
targets,
) => {
targets.into_iter().for_each(|target| {
notification::monitored_online(
&self.config.notifications,
target.nickname().to_owned(),
server.clone(),
);
});
enabled: _,
user: _,
channel: _,
}
data::client::Notification::MonitoredOffline(
targets,
) => {
targets.into_iter().for_each(|target| {
notification::monitored_offline(
&self.config.notifications,
target,
server.clone(),
);
});
| data::client::Notification::MonitoredOnline(_)
| data::client::Notification::MonitoredOffline(_) => {
self.notifications.notify(
&self.config.notifications,
&notification,
Some(&server),
);
}
_ => {}
}
}
data::client::Event::FileTransferRequest(request) => {
Expand Down
Loading

0 comments on commit 9cbcd61

Please sign in to comment.