@@ -0,0 +1,204 @@ |
| 1 | +//! Rule actions for modifying notifications |
| 2 | + |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | +use tracing::debug; |
| 5 | + |
| 6 | +use crate::notification::{Notification, Urgency}; |
| 7 | + |
| 8 | +/// Actions that can be performed on a matched notification |
| 9 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 10 | +#[serde(tag = "type", rename_all = "snake_case")] |
| 11 | +pub enum RuleAction { |
| 12 | + /// Suppress the notification (don't show it) |
| 13 | + Suppress, |
| 14 | + |
| 15 | + /// Set the urgency level |
| 16 | + SetUrgency { |
| 17 | + urgency: String, // "low", "normal", "critical" |
| 18 | + }, |
| 19 | + |
| 20 | + /// Override the timeout (in milliseconds, 0 = use default, -1 = never expire) |
| 21 | + SetTimeout { timeout: i32 }, |
| 22 | + |
| 23 | + /// Replace the summary text |
| 24 | + SetSummary { summary: String }, |
| 25 | + |
| 26 | + /// Replace the body text |
| 27 | + SetBody { body: String }, |
| 28 | + |
| 29 | + /// Append text to the body |
| 30 | + AppendBody { text: String }, |
| 31 | + |
| 32 | + /// Prepend text to the summary |
| 33 | + PrependSummary { text: String }, |
| 34 | + |
| 35 | + /// Set a custom sound (hint) |
| 36 | + SetSound { sound: String }, |
| 37 | + |
| 38 | + /// Mark as transient (won't be saved to history) |
| 39 | + SetTransient { transient: bool }, |
| 40 | + |
| 41 | + /// Execute a shell command (the notification data is available as env vars) |
| 42 | + Exec { command: String }, |
| 43 | +} |
| 44 | + |
| 45 | +impl RuleAction { |
| 46 | + /// Apply this action to a notification |
| 47 | + /// Returns None if the notification should be suppressed |
| 48 | + pub fn apply(&self, notification: &mut Notification) -> Option<()> { |
| 49 | + match self { |
| 50 | + RuleAction::Suppress => { |
| 51 | + debug!("Suppressing notification {}", notification.id); |
| 52 | + return None; |
| 53 | + } |
| 54 | + |
| 55 | + RuleAction::SetUrgency { urgency } => { |
| 56 | + let new_urgency = match urgency.to_lowercase().as_str() { |
| 57 | + "low" => Urgency::Low, |
| 58 | + "normal" => Urgency::Normal, |
| 59 | + "critical" => Urgency::Critical, |
| 60 | + _ => { |
| 61 | + debug!("Invalid urgency '{}', ignoring", urgency); |
| 62 | + return Some(()); |
| 63 | + } |
| 64 | + }; |
| 65 | + debug!( |
| 66 | + "Setting urgency for notification {} to {:?}", |
| 67 | + notification.id, new_urgency |
| 68 | + ); |
| 69 | + notification.hints.urgency = new_urgency; |
| 70 | + } |
| 71 | + |
| 72 | + RuleAction::SetTimeout { timeout } => { |
| 73 | + debug!( |
| 74 | + "Setting timeout for notification {} to {}ms", |
| 75 | + notification.id, timeout |
| 76 | + ); |
| 77 | + notification.expire_timeout = *timeout; |
| 78 | + } |
| 79 | + |
| 80 | + RuleAction::SetSummary { summary } => { |
| 81 | + debug!( |
| 82 | + "Setting summary for notification {} to '{}'", |
| 83 | + notification.id, summary |
| 84 | + ); |
| 85 | + notification.summary = summary.clone(); |
| 86 | + } |
| 87 | + |
| 88 | + RuleAction::SetBody { body } => { |
| 89 | + debug!( |
| 90 | + "Setting body for notification {} to '{}'", |
| 91 | + notification.id, body |
| 92 | + ); |
| 93 | + notification.body = body.clone(); |
| 94 | + } |
| 95 | + |
| 96 | + RuleAction::AppendBody { text } => { |
| 97 | + debug!("Appending to body of notification {}", notification.id); |
| 98 | + notification.body.push_str(text); |
| 99 | + } |
| 100 | + |
| 101 | + RuleAction::PrependSummary { text } => { |
| 102 | + debug!("Prepending to summary of notification {}", notification.id); |
| 103 | + notification.summary = format!("{}{}", text, notification.summary); |
| 104 | + } |
| 105 | + |
| 106 | + RuleAction::SetSound { sound } => { |
| 107 | + debug!( |
| 108 | + "Setting sound for notification {} to '{}'", |
| 109 | + notification.id, sound |
| 110 | + ); |
| 111 | + notification.hints.sound_name = Some(sound.clone()); |
| 112 | + } |
| 113 | + |
| 114 | + RuleAction::SetTransient { transient } => { |
| 115 | + debug!( |
| 116 | + "Setting transient for notification {} to {}", |
| 117 | + notification.id, transient |
| 118 | + ); |
| 119 | + notification.hints.transient = *transient; |
| 120 | + } |
| 121 | + |
| 122 | + RuleAction::Exec { command } => { |
| 123 | + debug!( |
| 124 | + "Executing command for notification {}: {}", |
| 125 | + notification.id, command |
| 126 | + ); |
| 127 | + // Execute command in background with notification data as env vars |
| 128 | + let cmd = command.clone(); |
| 129 | + let app_name = notification.app_name.clone(); |
| 130 | + let summary = notification.summary.clone(); |
| 131 | + let body = notification.body.clone(); |
| 132 | + let id = notification.id; |
| 133 | + |
| 134 | + std::thread::spawn(move || { |
| 135 | + let result = std::process::Command::new("sh") |
| 136 | + .arg("-c") |
| 137 | + .arg(&cmd) |
| 138 | + .env("NOTIFICATION_ID", id.to_string()) |
| 139 | + .env("NOTIFICATION_APP", &app_name) |
| 140 | + .env("NOTIFICATION_SUMMARY", &summary) |
| 141 | + .env("NOTIFICATION_BODY", &body) |
| 142 | + .output(); |
| 143 | + |
| 144 | + if let Err(e) = result { |
| 145 | + tracing::warn!("Failed to execute rule command '{}': {}", cmd, e); |
| 146 | + } |
| 147 | + }); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + Some(()) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +#[cfg(test)] |
| 156 | +mod tests { |
| 157 | + use super::*; |
| 158 | + |
| 159 | + #[test] |
| 160 | + fn test_suppress_action() { |
| 161 | + let action = RuleAction::Suppress; |
| 162 | + let mut notif = Notification::default(); |
| 163 | + assert!(action.apply(&mut notif).is_none()); |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn test_set_urgency() { |
| 168 | + let action = RuleAction::SetUrgency { |
| 169 | + urgency: "critical".to_string(), |
| 170 | + }; |
| 171 | + let mut notif = Notification::default(); |
| 172 | + assert!(action.apply(&mut notif).is_some()); |
| 173 | + assert_eq!(notif.hints.urgency, Urgency::Critical); |
| 174 | + } |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn test_set_timeout() { |
| 178 | + let action = RuleAction::SetTimeout { timeout: 10000 }; |
| 179 | + let mut notif = Notification::default(); |
| 180 | + action.apply(&mut notif); |
| 181 | + assert_eq!(notif.expire_timeout, 10000); |
| 182 | + } |
| 183 | + |
| 184 | + #[test] |
| 185 | + fn test_set_summary() { |
| 186 | + let action = RuleAction::SetSummary { |
| 187 | + summary: "New Summary".to_string(), |
| 188 | + }; |
| 189 | + let mut notif = Notification::default(); |
| 190 | + action.apply(&mut notif); |
| 191 | + assert_eq!(notif.summary, "New Summary"); |
| 192 | + } |
| 193 | + |
| 194 | + #[test] |
| 195 | + fn test_prepend_summary() { |
| 196 | + let action = RuleAction::PrependSummary { |
| 197 | + text: "[Important] ".to_string(), |
| 198 | + }; |
| 199 | + let mut notif = Notification::default(); |
| 200 | + notif.summary = "Original".to_string(); |
| 201 | + action.apply(&mut notif); |
| 202 | + assert_eq!(notif.summary, "[Important] Original"); |
| 203 | + } |
| 204 | +} |