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
use crate::NotificationItem;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Notification<T: Serialize> {
/// Notification item. Required.
notification: NotificationItem,
/// Recipients of the notification. Required.
/// Can be a list of emails or a list of user ids.
recipients: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Additional data to be sent with the notification.
/// Should be a serde serializable json object.
data: Option<T>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Category of the notification. If not provided, it will be sent to everyone.
category: Option<String>,
}
pub struct NotificationBuilder<'a, T: Serialize = ()> {
pub notification: NotificationItem,
pub recipients: &'a Vec<String>,
pub data: Option<T>,
pub category: Option<String>,
}
trait WithData<T: Serialize> {
fn data(self, data: T) -> Self;
}
impl<'a> NotificationBuilder<'a, ()> {
/// Create a new notification builder with the title and recipients.
/// Other fields can be set by chaining the methods.
/// **Example:**
/// ```
/// use engagespot::{NotificationBuilder, NotificationItem};
/// let notification = NotificationBuilder::new("Title", &vec!["email1".to_string(), "email2".to_string()]).build();
pub fn new(title: &str, recipients: &'a Vec<String>) -> Self {
NotificationBuilder {
notification: NotificationItem::new(title),
recipients,
data: None,
category: None,
}
}
}
impl<'a, T: Serialize> NotificationBuilder<'a, T> {
/// Create a new notification builder with the title and recipients and optional data.
/// Other fields can be set by chaining the methods.
/// **Example:**
/// ```
/// use serde::{Deserialize, Serialize};
/// #[derive(Serialize, Deserialize)]
/// struct Data<'a> {
///foo: &'a str,
/// }
/// use engagespot::{NotificationBuilder, NotificationItem};
/// let notification = NotificationBuilder::new_with_data("Title", &vec!["email1".to_string(), "email2".to_string()], &Data { foo: "bar" }).build();
pub fn new_with_data(title: &str, recipients: &'a Vec<String>, data: T) -> Self {
NotificationBuilder {
notification: NotificationItem::new(title),
recipients,
data: Some(data),
category: None,
}
}
/// Set the notification item
pub fn notification_item(mut self, notification: NotificationItem) -> Self {
self.notification = notification;
self
}
/// Set the title of the notification item.
pub fn title(mut self, title: &str) -> Self {
self.notification = self.notification.title(title);
self
}
/// Set the message of the notification item.
pub fn message(mut self, message: &str) -> Self {
self.notification = self.notification.message(message);
self
}
/// Set the url of the notification item.
pub fn url(mut self, url: &str) -> Self {
self.notification = self.notification.url(url);
self
}
/// Set the icon of the notification item.
pub fn icon(mut self, icon: &str) -> Self {
self.notification = self.notification.icon(icon);
self
}
/// Set the recipients of the notification.
pub fn recipients(mut self, recipients: &'a Vec<String>) -> Self {
self.recipients = recipients;
self
}
/// Set the category of the notification.
pub fn category(mut self, category: String) -> Self {
self.category = Some(category);
self
}
/// Set the additional data of the notification.
/// Should be a serde serializable json object.
pub fn data(mut self, data: T) -> Self {
self.data = Some(data);
self
}
/// Build the notification. Returns the Notification struct.
pub fn build(self) -> Notification<T> {
Notification {
notification: self.notification,
recipients: self.recipients.clone(),
data: self.data,
category: self.category,
}
}
}