Skip to content

Commit fcc4393

Browse files
committed
Fix emergency priority (2) not working if retry/expire aren't provided
1 parent 8865fff commit fcc4393

File tree

5 files changed

+159
-7
lines changed

5 files changed

+159
-7
lines changed

src/pushover/data/attachment_message.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ pub struct AttachmentMessage {
3131
/// Send as -2 to generate no notification/alert, -1 to always send as a quiet notification, 1 to display as high-priority and bypass the user's quiet hours, or 2 to also require confirmation from the user
3232
#[serde(skip_serializing_if = "Option::is_none")]
3333
pub priority: Option<String>,
34+
/// When the priority is set to 2, sets the amount of seconds between each retries. Must be at least 30 seconds.
35+
#[serde(skip_serializing_if = "Option::is_none")]
36+
pub retry: Option<String>, // Required if priority is set to 2
37+
/// When the priority is set to 2, sets the amount of seconds before the notification is expired. The maximum value is 10800 (3 hours).be between 60 and 10800.
38+
#[serde(skip_serializing_if = "Option::is_none")]
39+
pub expire: Option<String>, // Required if priority is set to 2
3440
/// The name of one of the sounds supported by device clients to override the user's default sound choice. (See sound list: https://pushover.net/api#sounds)
3541
#[serde(skip_serializing_if = "Option::is_none")]
3642
pub sound: Option<String>,
@@ -56,6 +62,8 @@ impl AttachmentMessage {
5662
.text("url", self.url.clone().unwrap_or(String::from("")))
5763
.text("url_title", self.url_title.clone().unwrap_or(String::from("")))
5864
.text("priority", self.priority.unwrap_or(String::from("")))
65+
.text("retry", self.retry.clone().unwrap_or(String::from("")))
66+
.text("expire", self.expire.clone().unwrap_or(String::from("")))
5967
.text("sound", self.sound.clone().unwrap_or(String::from("")))
6068
.text("timestamp", self.timestamp.unwrap_or(String::from("")))
6169
.text("device", self.device.clone().unwrap_or(String::from("")));
@@ -78,6 +86,8 @@ impl Default for AttachmentMessage {
7886
url: None,
7987
url_title: None,
8088
priority: None,
89+
retry: None,
90+
expire: None,
8191
sound: None,
8292
timestamp: None,
8393
device: None,

src/pushover/data/attachment_message_builder.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::io::Error;
22
use std::io::ErrorKind;
3-
43
use crate::pushover::constants;
54

65
use super::PushoverSound;
@@ -126,6 +125,44 @@ impl AttachmentMessageBuilder {
126125
/// Resets the priority to default (0, normal)
127126
pub fn remove_priority(mut self) -> AttachmentMessageBuilder {
128127
self.build.priority = Some("0".into());
128+
self.build.retry = None;
129+
self.build.expire = None;
130+
self
131+
}
132+
133+
/// When the priority is set to 2, sets the amount of seconds between each retries. Must be at least 30 seconds.
134+
pub fn set_retry(mut self, retry_secs: i32) -> AttachmentMessageBuilder {
135+
if self.build.priority != Some("2".into()) {
136+
// Retry only makes sense if priority is 2
137+
return self;
138+
}
139+
140+
if retry_secs < 30 {
141+
self.build.retry = Some("30".into());
142+
return self;
143+
}
144+
145+
self.build.retry = Some(retry_secs.to_string());
146+
self
147+
}
148+
149+
/// When the priority is set to 2, sets the amount of seconds before the notification is expired. The maximum value is 10800 (3 hours). Must be between 60 and 10800.
150+
pub fn set_expire(mut self, expire_secs: i32) -> AttachmentMessageBuilder {
151+
if self.build.priority != Some("2".into()) {
152+
// Expire only makes sense if priority is 2
153+
return self;
154+
}
155+
156+
if expire_secs < 60 {
157+
self.build.expire = Some("60".into());
158+
return self;
159+
}
160+
else if expire_secs > 10800 {
161+
self.build.expire = Some("10800".into());
162+
return self;
163+
}
164+
165+
self.build.expire = Some(expire_secs.to_string());
129166
self
130167
}
131168

@@ -195,7 +232,16 @@ impl AttachmentMessageBuilder {
195232
}
196233

197234
/// Transforms the MessageBuilder into a useable Message
198-
pub fn build(self) -> Result<AttachmentMessage, Box<dyn std::error::Error>> {
235+
pub fn build(mut self) -> Result<AttachmentMessage, Box<dyn std::error::Error>> {
236+
if self.build.priority == Some("2".into()) {
237+
if self.build.retry.is_none() {
238+
self.build.retry = Some("30".into());
239+
}
240+
if self.build.expire.is_none() {
241+
self.build.expire = Some("10800".into());
242+
}
243+
}
244+
199245
if self.build.app_token.is_empty() {
200246
return Err(Box::new(Error::new(ErrorKind::InvalidInput, "Application token is empty")));
201247
}

src/pushover/data/message.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ pub struct Message {
3030
/// Send as -2 to generate no notification/alert, -1 to always send as a quiet notification, 1 to display as high-priority and bypass the user's quiet hours, or 2 to also require confirmation from the user
3131
#[serde(skip_serializing_if = "Option::is_none")]
3232
pub priority: Option<i8>,
33+
/// When the priority is set to 2, sets the amount of seconds between each retries. Must be at least 30 seconds.
34+
#[serde(skip_serializing_if = "Option::is_none")]
35+
pub retry: Option<i32>, // Required if priority is set to 2
36+
/// When the priority is set to 2, sets the amount of seconds before the notification is expired. The maximum value is 10800 (3 hours).be between 60 and 10800.
37+
#[serde(skip_serializing_if = "Option::is_none")]
38+
pub expire: Option<i32>, // Required if priority is set to 2
3339
/// The name of one of the sounds supported by device clients to override the user's default sound choice. (See sound list: https://pushover.net/api#sounds)
3440
#[serde(skip_serializing_if = "Option::is_none")]
3541
pub sound: Option<String>,
@@ -55,6 +61,8 @@ impl Default for Message {
5561
url: None,
5662
url_title: None,
5763
priority: None,
64+
retry: None,
65+
expire: None,
5866
sound: None,
5967
timestamp: None,
6068
device: None,

src/pushover/data/message_builder.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,44 @@ impl MessageBuilder {
118118
/// Resets the priority to default (0, normal)
119119
pub fn remove_priority(mut self) -> MessageBuilder {
120120
self.build.priority = Some(0);
121+
self.build.retry = None;
122+
self.build.expire = None;
123+
self
124+
}
125+
126+
/// When the priority is set to 2, sets the amount of seconds between each retries. Must be at least 30 seconds.
127+
pub fn set_retry(mut self, retry_secs: i32) -> MessageBuilder {
128+
if self.build.priority != Some(2) {
129+
// Retry only makes sense if priority is 2
130+
return self;
131+
}
132+
133+
if retry_secs < 30 {
134+
self.build.retry = Some(30);
135+
return self;
136+
}
137+
138+
self.build.retry = Some(retry_secs);
139+
self
140+
}
141+
142+
/// When the priority is set to 2, sets the amount of seconds before the notification is expired. The maximum value is 10800 (3 hours). Must be between 60 and 10800.
143+
pub fn set_expire(mut self, expire_secs: i32) -> MessageBuilder {
144+
if self.build.priority != Some(2) {
145+
// Expire only makes sense if priority is 2
146+
return self;
147+
}
148+
149+
if expire_secs < 60 {
150+
self.build.expire = Some(60);
151+
return self;
152+
}
153+
else if expire_secs > 10800 {
154+
self.build.expire = Some(10800);
155+
return self;
156+
}
157+
158+
self.build.expire = Some(expire_secs);
121159
self
122160
}
123161

@@ -172,8 +210,16 @@ impl MessageBuilder {
172210
self
173211
}
174212

175-
/// Transforms the MessageBuilder into a useable Message
176-
pub fn build(self) -> Message {
177-
self.build.clone()
213+
/// Transforms the MessageBuilder into a usable Message
214+
pub fn build(mut self) -> Message {
215+
if self.build.priority == Some(2) {
216+
if self.build.retry.is_none() {
217+
self.build.retry = Some(30);
218+
}
219+
if self.build.expire.is_none() {
220+
self.build.expire = Some(10800);
221+
}
222+
}
223+
self.build
178224
}
179225
}

src/tests/no_github_actions.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,27 @@ async fn test_send_request_min_message_with_ttl() {
5555
credentials.app_token.as_str(),
5656
format!("Test from pushover-rs with {ttl} seconds TTL").as_str(),
5757
)
58-
.set_ttl(ttl)
59-
.build();
58+
.set_ttl(ttl)
59+
.build();
60+
let response = send_pushover_request(message).await;
61+
assert_eq!(response.is_ok(), true);
62+
} else {
63+
panic!("Could not read test data.");
64+
}
65+
}
66+
67+
#[tokio::test]
68+
async fn test_send_request_emergency_priority() {
69+
if let Ok(credentials) = read_test_data() {
70+
let message: Message = MessageBuilder::new(
71+
credentials.user_key.as_str(),
72+
credentials.app_token.as_str(),
73+
"Server down wake up!!!",
74+
)
75+
.set_priority(2)
76+
.set_retry(30)
77+
.set_expire(60)
78+
.build();
6079
let response = send_pushover_request(message).await;
6180
assert_eq!(response.is_ok(), true);
6281
} else {
@@ -103,3 +122,26 @@ fn test_send_with_good_attachment() {
103122
panic!("Could not read test data.");
104123
}
105124
}
125+
#[test]
126+
fn test_send_with_good_attachment_and_emergency_priority() {
127+
if let Ok(credentials) = read_test_data() {
128+
let attachment_path: String = "./testdata/attachment_test.jpg".to_owned();
129+
let message: AttachmentMessage = AttachmentMessageBuilder::new(
130+
credentials.user_key.as_str(),
131+
credentials.app_token.as_str(),
132+
"Test from pushover-rs, with attachment and ttl",
133+
)
134+
.set_attachment(attachment_path)
135+
.set_priority(2)
136+
.set_retry(30)
137+
.set_expire(120)
138+
.build()
139+
.unwrap();
140+
141+
let response = send_pushover_request_with_attachment(message);
142+
143+
assert_eq!(response.is_ok(), true);
144+
} else {
145+
panic!("Could not read test data.");
146+
}
147+
}

0 commit comments

Comments
 (0)