use std::io::Error;
use std::io::ErrorKind;
use crate::pushover::constants;
use super::PushoverSound;
use super::AttachmentMessage;
pub struct AttachmentMessageBuilder {
build: AttachmentMessage,
}
impl AttachmentMessageBuilder {
pub fn new(user_key: &str, application_token: &str, message: &str) -> Self {
let mut build = AttachmentMessage::default();
build.user_key = user_key.to_owned();
build.app_token = application_token.to_owned();
build.message = message.to_owned();
AttachmentMessageBuilder {
build,
}
}
pub fn modify_message(mut self, message: &str) -> AttachmentMessageBuilder {
if message.trim().len() == 0 {
return self;
}
self.build.message = message.to_owned();
self
}
pub fn set_title(mut self, title: &str) -> AttachmentMessageBuilder {
if title.trim().len() == 0 {
self.build.title = None;
}
self.build.title = Some(title.to_owned());
self
}
#[deprecated(since="0.3.12", note="Please use set_title instead.")]
pub fn add_title(mut self, title: &str) -> AttachmentMessageBuilder {
if title.trim().len() == 0 {
self.build.title = None;
}
self.build.title = Some(title.to_owned());
self
}
pub fn remove_title(mut self) -> AttachmentMessageBuilder {
self.build.title = None;
self
}
pub fn set_url(mut self, url: &str, url_title: Option<&str>) -> AttachmentMessageBuilder {
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
}
#[deprecated(since="0.3.12", note="Please use set_url instead.")]
pub fn add_url(mut self, url: &str, url_title: Option<&str>) -> AttachmentMessageBuilder {
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
}
pub fn remove_url(mut self) -> AttachmentMessageBuilder {
self.build.url = None;
self.build.url_title = None;
self
}
pub fn set_priority(mut self, priority: i8) -> AttachmentMessageBuilder {
if priority < -2 || priority > 2 {
self.build.priority = Some("0".into());
return self;
}
self.build.priority = Some(priority.to_string());
self
}
pub fn remove_priority(mut self) -> AttachmentMessageBuilder {
self.build.priority = Some("0".into());
self.build.retry = None;
self.build.expire = None;
self
}
pub fn set_retry(mut self, retry_secs: i32) -> AttachmentMessageBuilder {
if self.build.priority != Some("2".into()) {
return self;
}
if retry_secs < 30 {
self.build.retry = Some("30".into());
return self;
}
self.build.retry = Some(retry_secs.to_string());
self
}
pub fn set_expire(mut self, expire_secs: i32) -> AttachmentMessageBuilder {
if self.build.priority != Some("2".into()) {
return self;
}
if expire_secs < 60 {
self.build.expire = Some("60".into());
return self;
}
else if expire_secs > 10800 {
self.build.expire = Some("10800".into());
return self;
}
self.build.expire = Some(expire_secs.to_string());
self
}
pub fn set_sound(mut self, sound: PushoverSound) -> AttachmentMessageBuilder {
self.build.sound = Some(sound.to_string());
self
}
pub fn remove_sound(mut self) -> AttachmentMessageBuilder {
self.build.sound = None;
self
}
pub fn set_timestamp(mut self, unix_timestamp: u64) -> AttachmentMessageBuilder {
self.build.timestamp = Some(unix_timestamp.to_string());
self
}
pub fn remove_timestamp(mut self) -> AttachmentMessageBuilder {
self.build.timestamp = None;
self
}
pub fn set_device(mut self, device_name: &str) -> AttachmentMessageBuilder {
self.build.device = Some(device_name.to_string());
self
}
pub fn remove_device(mut self) -> AttachmentMessageBuilder {
self.build.device = None;
self
}
pub fn set_ttl(mut self, ttl_secs: u32) -> AttachmentMessageBuilder {
if ttl_secs <= 0 {
self.build.ttl = None;
}
else {
self.build.ttl = Some(ttl_secs);
}
self
}
pub fn set_attachment(mut self, attachment_path: String) -> AttachmentMessageBuilder {
if attachment_path.trim().len() == 0 {
return self;
}
self.build.attachment = attachment_path;
self
}
pub fn build(mut self) -> Result<AttachmentMessage, Box<dyn std::error::Error>> {
if self.build.priority == Some("2".into()) {
if self.build.retry.is_none() {
self.build.retry = Some("30".into());
}
if self.build.expire.is_none() {
self.build.expire = Some("10800".into());
}
}
if self.build.app_token.is_empty() {
return Err(Box::new(Error::new(ErrorKind::InvalidInput, "Application token is empty")));
}
if self.build.user_key.is_empty() {
return Err(Box::new(Error::new(ErrorKind::InvalidInput, "User key is empty")));
}
if self.build.message.is_empty() {
return Err(Box::new(Error::new(ErrorKind::InvalidInput, "Message is empty")));
}
if self.build.attachment.is_empty() {
return Err(Box::new(Error::new(ErrorKind::InvalidInput, "Attachment is empty")));
}
if !std::path::Path::new(&self.build.attachment).exists() {
return Err(Box::new(Error::new(ErrorKind::InvalidInput, "Attachment file doesn't exist.")));
}
let file_size = std::fs::metadata(&self.build.attachment).unwrap().len();
if file_size > constants::PUSHOVER_API_ATTACHMENT_MAX_SIZE_BYTES {
return Err(Box::new(Error::new(ErrorKind::InvalidInput, "Attachment file is too large. (> 2621440 bytes)")));
}
Ok(self.build.clone())
}
}