Skip to main content

pushover_rs/pushover/data/
message_builder.rs

1use super::{Message, PushoverSound};
2
3// TODO: Fix DRY principle with attachment_message_builder.rs
4
5/**
6Helps build a correct Pushover request.
7 */
8#[derive(Debug)]
9pub struct MessageBuilder {
10    build: Message,
11}
12
13#[allow(dead_code)]
14impl MessageBuilder {
15    /// Creates a new MessageBuilder instance with the required minimal informations (User key, App token & Message)
16    pub fn new(user_key: &str, application_token: &str, message: &str) -> Self {
17        let mut build = Message::default();
18        
19        build.user_key = user_key.to_owned();
20        build.app_token = application_token.to_owned();
21        build.message = message.to_owned();
22
23        MessageBuilder {
24            build,
25        }
26    }
27
28    /// Modifies the existing message.
29    pub fn modify_message(mut self, message: &str) -> MessageBuilder {
30        if message.trim().len() == 0 {
31            return self;
32        }
33
34        self.build.message = message.to_owned();
35        self
36    }
37
38    /// Sets a title to your message
39    pub fn set_title(mut self, title: &str) -> MessageBuilder {
40        if title.trim().len() == 0 {
41            self.build.title = None;
42        }
43
44        self.build.title = Some(title.to_owned());
45        self
46    }
47
48    /// Adds a title to your message
49    #[deprecated(since="0.3.12", note="Please use set_title instead.")]
50    pub fn add_title(mut self, title: &str) -> MessageBuilder {
51        if title.trim().len() == 0 {
52            self.build.title = None;
53        }
54
55        self.build.title = Some(title.to_owned());
56        self
57    }
58
59    /// Removes the title. The title will be defaulted to your application name.
60    pub fn remove_title(mut self) -> MessageBuilder {
61        self.build.title = None;
62        self
63    }
64
65    /// Sets an url (and optionally, an url title) to send along with your message.
66    ///
67    /// If set, the URL title will be shown, otherwise the URL will be shown.
68    pub fn set_url(mut self, url: &str, url_title: Option<&str>) -> MessageBuilder {
69        if url.trim().len() == 0 {
70            self.build.url = None;
71            self.build.url_title = None;
72            return self;
73        }
74
75        self.build.url = Some(url.to_owned());
76        if url_title.is_some() {
77            self.build.url_title = Some(url_title.unwrap().to_owned());
78        }
79        self
80    }
81
82    /// Adds an url (and optionally, an url title) to send along with your message.
83    /// 
84    /// If set, the URL title will be shown, otherwise the URL will be shown.
85    #[deprecated(since="0.3.12", note="Please use set_url instead.")]
86    pub fn add_url(mut self, url: &str, url_title: Option<&str>) -> MessageBuilder {
87        if url.trim().len() == 0 {
88            self.build.url = None;
89            self.build.url_title = None;
90            return self;
91        }
92
93        self.build.url = Some(url.to_owned());
94        if url_title.is_some() {
95            self.build.url_title = Some(url_title.unwrap().to_owned());
96        }
97        self
98    }
99
100    /// Removes both the url and url title from your message
101    pub fn remove_url(mut self) -> MessageBuilder {
102        self.build.url = None;
103        self.build.url_title = None;
104        self
105    }
106
107    /// 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.
108    pub fn set_priority(mut self, priority: i8) -> MessageBuilder {
109        if priority < -2 || priority > 2 {
110            self.build.priority = Some(0);
111            return self;
112        }
113
114        self.build.priority = Some(priority);
115        self
116    }
117
118    /// Resets the priority to default (0, normal)
119    pub fn remove_priority(mut self) -> MessageBuilder {
120        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);
159        self
160    }
161
162    /// Sets the sound to be used to notify the user.
163    /// 
164    /// See this list of available sounds: https://pushover.net/api#sounds
165    pub fn set_sound(mut self, sound: PushoverSound) -> MessageBuilder {
166        self.build.sound = Some(sound.to_string());
167        self
168    }
169
170    /// Removes the custom sound and reverts to the default sound.
171    pub fn remove_sound(mut self) -> MessageBuilder {
172        self.build.sound = None;
173        self
174    }
175
176    /// 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
177    pub fn set_timestamp(mut self, unix_timestamp: u64) -> MessageBuilder {
178        self.build.timestamp = Some(unix_timestamp);
179        self
180    }
181
182    /// Resets the custom unix timestamp
183    pub fn remove_timestamp(mut self) -> MessageBuilder {
184        self.build.timestamp = None;
185        self
186    }
187
188    /// Add a device name to send the notification to.
189    ///
190    /// Overrides the current device if a new device name is set.
191    pub fn set_device(mut self, device_name: &str) -> MessageBuilder {
192        self.build.device = Some(device_name.to_string());
193        self
194    }
195
196    /// Clears the device if set.
197    pub fn remove_device(mut self) -> MessageBuilder {
198        self.build.device = None;
199        self
200    }
201
202    /// Set the TTL (Time to Live), in seconds
203    pub fn set_ttl(mut self, ttl_secs: u32) -> MessageBuilder {
204        if ttl_secs <= 0 {
205            self.build.ttl = None;
206        }
207        else {
208            self.build.ttl = Some(ttl_secs);
209        }
210        self
211    }
212
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
224    }
225}