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
122    }
123
124    /// Sets the sound to be used to notify the user.
125    /// 
126    /// See this list of available sounds: https://pushover.net/api#sounds
127    pub fn set_sound(mut self, sound: PushoverSound) -> MessageBuilder {
128        self.build.sound = Some(sound.to_string());
129        self
130    }
131
132    /// Removes the custom sound and reverts to the default sound.
133    pub fn remove_sound(mut self) -> MessageBuilder {
134        self.build.sound = None;
135        self
136    }
137
138    /// 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
139    pub fn set_timestamp(mut self, unix_timestamp: u64) -> MessageBuilder {
140        self.build.timestamp = Some(unix_timestamp);
141        self
142    }
143
144    /// Resets the custom unix timestamp
145    pub fn remove_timestamp(mut self) -> MessageBuilder {
146        self.build.timestamp = None;
147        self
148    }
149
150    /// Add a device name to send the notification to.
151    ///
152    /// Overrides the current device if a new device name is set.
153    pub fn set_device(mut self, device_name: &str) -> MessageBuilder {
154        self.build.device = Some(device_name.to_string());
155        self
156    }
157
158    /// Clears the device if set.
159    pub fn remove_device(mut self) -> MessageBuilder {
160        self.build.device = None;
161        self
162    }
163
164    /// Set the TTL (Time to Live), in seconds
165    pub fn set_ttl(mut self, ttl_secs: u32) -> MessageBuilder {
166        if ttl_secs <= 0 {
167            self.build.ttl = None;
168        }
169        else {
170            self.build.ttl = Some(ttl_secs);
171        }
172        self
173    }
174
175    /// Transforms the MessageBuilder into a useable Message
176    pub fn build(self) -> Message {
177        self.build.clone()
178    }
179}