Skip to content

Commit

Permalink
Merge pull request #135 from odilia-app/notification-improvements
Browse files Browse the repository at this point in the history
Notification improvements
  • Loading branch information
TTWNO authored Mar 9, 2024
2 parents e3dfd33 + f49f940 commit 3dd192f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ jobs:
systemctl --user start dbus;
xvfb-run dunst --screen 0 600x400x8 &
- name: cargo llvm-cov
run: cargo llvm-cov --locked --lcov --output-path lcov.info
run: cargo llvm-cov --lcov --output-path lcov.info
- name: Upload to codecov.io
uses: codecov/codecov-action@v3
with:
Expand Down
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions odilia-notify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ edition = "2021"

[dependencies]
futures = "0.3.30"
itertools = "0.12.1"
serde.workspace=true
serde_repr = "0.1.18"
thiserror = "1.0.56"
tokio.workspace=true
tracing.workspace=true
Expand Down
7 changes: 7 additions & 0 deletions odilia-notify/src/action.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Action {
pub name: String,
pub method: String,
}
2 changes: 2 additions & 0 deletions odilia-notify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use futures::{Stream, StreamExt};
use tracing::{debug, info, instrument};

use zbus::{fdo::MonitoringProxy, Connection, MatchRule, MessageStream, MessageType};
mod action;
mod notification;
mod urgency;
use notification::Notification;
mod error;
use error::NotifyError;
Expand Down
27 changes: 24 additions & 3 deletions odilia-notify/src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ use serde::{Deserialize, Serialize};

use zbus::{zvariant::Value, Message};

#[derive(Debug, Serialize, Deserialize)]
use crate::action::Action;
use crate::urgency::Urgency;
use itertools::Itertools;

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Notification {
pub app_name: String,
pub title: String,
pub body: String,
pub urgency: Urgency,
pub actions: Vec<Action>,
}

type MessageBody<'a> =
Expand All @@ -18,9 +24,24 @@ impl TryFrom<Arc<Message>> for Notification {
type Error = zbus::Error;

fn try_from(msg: Arc<Message>) -> Result<Self, Self::Error> {
let (app_name, _, _, title, body, ..) = msg.body::<MessageBody>()?;
let mb: MessageBody = msg.body()?;
let (app_name, _, _, title, body, actions, mut options, _) = mb;
let actions = actions
.iter()
.tuples()
.map(|(name, method)| Action {
name: name.to_string(),
method: method.to_string(),
})
.collect();
// any error in deserailizing the value (including lack of "urgency" key in options
// hashmap) will give it an urgency of Normal
let urgency = options
.remove("urgency")
.and_then(|o| o.try_into().ok())
.unwrap_or(Urgency::Normal);

Ok(Notification { app_name, title, body })
Ok(Notification { app_name, title, body, actions, urgency })
}
}
#[cfg(test)]
Expand Down
28 changes: 28 additions & 0 deletions odilia-notify/src/urgency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use serde::{Deserialize, Serialize};
use zbus::zvariant::{OwnedValue, Type, Value};

/// A priority/urgency level.
/// https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html#urgency-levels
#[derive(
Clone,
Copy,
Debug,
Type,
Serialize,
Deserialize,
Default,
Value,
OwnedValue,
Eq,
PartialEq,
PartialOrd,
Ord,
)]
#[zvariant(signature = "y")]
#[repr(u8)]
pub enum Urgency {
Low = 0,
#[default]
Normal = 1,
Critical = 2,
}

0 comments on commit 3dd192f

Please sign in to comment.