1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use super::{Message, PushoverSound};
// TODO: Fix DRY principle with attachment_message_builder.rs
/**
Helps build a correct Pushover request.
*/
#[derive(Debug)]
pub struct MessageBuilder {
build: Message,
}
#[allow(dead_code)]
impl MessageBuilder {
/// Creates a new MessageBuilder instance with the required minimal informations (User key, App token & Message)
pub fn new(user_key: &str, application_token: &str, message: &str) -> Self {
let mut build = Message::default();
build.user_key = user_key.to_owned();
build.app_token = application_token.to_owned();
build.message = message.to_owned();
MessageBuilder {
build,
}
}
/// Modifies the existing message.
pub fn modify_message(mut self, message: &str) -> MessageBuilder {
if message.trim().len() == 0 {
return self;
}
self.build.message = message.to_owned();
self
}
/// Sets a title to your message
pub fn set_title(mut self, title: &str) -> MessageBuilder {
if title.trim().len() == 0 {
self.build.title = None;
}
self.build.title = Some(title.to_owned());
self
}
/// Adds a title to your message
#[deprecated(since="0.3.12", note="Please use set_title instead.")]
pub fn add_title(mut self, title: &str) -> MessageBuilder {
if title.trim().len() == 0 {
self.build.title = None;
}
self.build.title = Some(title.to_owned());
self
}
/// Removes the title. The title will be defaulted to your application name.
pub fn remove_title(mut self) -> MessageBuilder {
self.build.title = None;
self
}
/// Sets an url (and optionally, an url title) to send along with your message.
///
/// If set, the URL title will be shown, otherwise the URL will be shown.
pub fn set_url(mut self, url: &str, url_title: Option<&str>) -> MessageBuilder {
if url.trim().len() == 0 {
self.build.url = None;
self.build.url_title = None;
return self;
}
self.build.url = Some(url.to_owned());
if url_title.is_some() {
self.build.url_title = Some(url_title.unwrap().to_owned());
}
self
}
/// Adds an url (and optionally, an url title) to send along with your message.
///
/// If set, the URL title will be shown, otherwise the URL will be shown.
#[deprecated(since="0.3.12", note="Please use set_url instead.")]
pub fn add_url(mut self, url: &str, url_title: Option<&str>) -> MessageBuilder {
if url.trim().len() == 0 {
self.build.url = None;
self.build.url_title = None;
return self;
}
self.build.url = Some(url.to_owned());
if url_title.is_some() {
self.build.url_title = Some(url_title.unwrap().to_owned());
}
self
}
/// Removes both the url and url title from your message
pub fn remove_url(mut self) -> MessageBuilder {
self.build.url = None;
self.build.url_title = None;
self
}
/// 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.
pub fn set_priority(mut self, priority: i8) -> MessageBuilder {
if priority < -2 || priority > 2 {
self.build.priority = Some(0);
return self;
}
self.build.priority = Some(priority);
self
}
/// Resets the priority to default (0, normal)
pub fn remove_priority(mut self) -> MessageBuilder {
self.build.priority = Some(0);
self.build.retry = None;
self.build.expire = None;
self
}
/// When the priority is set to 2, sets the amount of seconds between each retries. Must be at least 30 seconds.
pub fn set_retry(mut self, retry_secs: i32) -> MessageBuilder {
if self.build.priority != Some(2) {
// Retry only makes sense if priority is 2
return self;
}
if retry_secs < 30 {
self.build.retry = Some(30);
return self;
}
self.build.retry = Some(retry_secs);
self
}
/// 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.
pub fn set_expire(mut self, expire_secs: i32) -> MessageBuilder {
if self.build.priority != Some(2) {
// Expire only makes sense if priority is 2
return self;
}
if expire_secs < 60 {
self.build.expire = Some(60);
return self;
}
else if expire_secs > 10800 {
self.build.expire = Some(10800);
return self;
}
self.build.expire = Some(expire_secs);
self
}
/// Sets the sound to be used to notify the user.
///
/// See this list of available sounds: https://pushover.net/api#sounds
pub fn set_sound(mut self, sound: PushoverSound) -> MessageBuilder {
self.build.sound = Some(sound.to_string());
self
}
/// Removes the custom sound and reverts to the default sound.
pub fn remove_sound(mut self) -> MessageBuilder {
self.build.sound = None;
self
}
/// Sets an Unix timestamp of your message's date and time to display to the user, rather than the time your message is received by our API
pub fn set_timestamp(mut self, unix_timestamp: u64) -> MessageBuilder {
self.build.timestamp = Some(unix_timestamp);
self
}
/// Resets the custom unix timestamp
pub fn remove_timestamp(mut self) -> MessageBuilder {
self.build.timestamp = None;
self
}
/// Add a device name to send the notification to.
///
/// Overrides the current device if a new device name is set.
pub fn set_device(mut self, device_name: &str) -> MessageBuilder {
self.build.device = Some(device_name.to_string());
self
}
/// Clears the device if set.
pub fn remove_device(mut self) -> MessageBuilder {
self.build.device = None;
self
}
/// Set the TTL (Time to Live), in seconds
pub fn set_ttl(mut self, ttl_secs: u32) -> MessageBuilder {
if ttl_secs <= 0 {
self.build.ttl = None;
}
else {
self.build.ttl = Some(ttl_secs);
}
self
}
/// Transforms the MessageBuilder into a usable Message
pub fn build(mut self) -> Message {
if self.build.priority == Some(2) {
if self.build.retry.is_none() {
self.build.retry = Some(30);
}
if self.build.expire.is_none() {
self.build.expire = Some(10800);
}
}
self.build
}
}