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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
///
/// Conversations
///
use super::link::LinkType;
use crate::api::MailchimpApi;
use crate::internal::request::MailchimpResult;
use crate::iter::{BuildIter, MailchimpCollection, ResourceFilter, MalchimpIter};
use std::collections::HashMap;
use super::conversation_messages::{ConversationMessage, MessagesFilter, CollectionConversationMessages, MessagesBuider};
use log::error;

///
/// Conversation tracking is a paid feature that lets you view subscribers’
/// replies to your campaigns in your Mailchimp account.
///
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Conversation {
    /// A string that uniquely identifies this conversation.
    #[serde(default)]
    pub id: String,
    /// The total number of messages in this conversation.
    #[serde(default)]
    pub message_count: u64,
    /// The unique identifier of the campaign for this conversation.
    #[serde(default)]
    pub campaign_id: String,
    /// The unique identifier of the list for this conversation.
    #[serde(default)]
    pub list_id: String,
    /// The number of unread messages in this conversation.
    #[serde(default)]
    pub unread_messages: u64,
    /// A label representing the sender of this message.
    #[serde(default)]
    pub from_label: String,
    /// A label representing the email of the sender of this message.
    #[serde(default)]
    pub from_email: String,
    /// The subject of the message.
    #[serde(default)]
    pub subject: String,
    /// The most recent message in the conversation.
    #[serde(default)]
    pub last_message: ConversationMessage,
    /// A list of link types and descriptions for the API schema documents.
    #[serde(default)]
    pub _links: Vec<LinkType>,

    /// Mailchimp API
    #[serde(skip)]
    _api: MailchimpApi,
}

///
/// ParamMessage
///
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamMessage {
    /// A label representing the email of the sender of this message
    #[serde(default)]
    pub from_email: String,
    /// The subject of this message.
    #[serde(default)]
    pub subject: String,
    /// The plain-text content of the message.
    #[serde(default)]
    pub message: String,
    /// Whether this message has been marked as read.
    #[serde(default)]
    pub read: bool,
}

impl Conversation {
    ///
    /// Post a new message to a conversation.
    ///
    pub fn create_message(&self, message: ParamMessage) -> MailchimpResult<ConversationMessage> {
        // POST /conversations/{conversation_id}/messages
        let mut endpoint = self.get_base_endpoint();
        endpoint.push_str("/messages");
        self._api.post::<ConversationMessage, ParamMessage>(&endpoint, message)
    }

    ///
    /// Get conversation messages
    ///
    pub fn get_conversation_messages(
        &self,
        filter: Option<MessagesFilter>,
    ) -> MalchimpIter<MessagesBuider> {
        // GET  /conversations/{conversation_id}/messages
        let mut endpoint = self.get_base_endpoint();
        endpoint.push_str("/messages");

        let mut filter_params = MessagesFilter::default();

        if let Some(f) = filter {
            filter_params = f;
        }

        match self
            ._api
            .get::<CollectionConversationMessages>(&endpoint, filter_params.build_payload())
        {
            Ok(collection) => MalchimpIter {
                builder: MessagesBuider {},
                data: collection.conversation_messages,
                cur_filters: filter_params.clone(),
                cur_it: 0,
                total_items: collection.total_items,
                api: self._api.clone(),
                endpoint: endpoint.to_string(),
            },
            Err(e) => {
                error!( target: "mailchimp",  "Get Activities: Response Error details: {:?}", e);
                MalchimpIter {
                    builder: MessagesBuider {},
                    data: Vec::new(),
                    cur_filters: filter_params.clone(),
                    cur_it: 0,
                    total_items: 0,
                    api: self._api.clone(),
                    endpoint: endpoint.to_string(),
                }
            }
        }
    }

    ///
    /// Get a specific conversation message
    ///
    pub fn get_conversation_message<'a>(&self, message_id: &'a str) -> MailchimpResult<ConversationMessage> {
        let mut endpoint = self.get_base_endpoint();
        endpoint.push_str("/messages/");
        endpoint.push_str(message_id);

        let mut payload = HashMap::new();
        payload.insert("message_id".to_string(), message_id.to_string());
        self._api.get::<ConversationMessage>(&endpoint, payload)
    }

    pub fn set_api(&mut self, n_api: &MailchimpApi) {
        self._api = n_api.clone();
    }

    fn get_base_endpoint(&self) -> String {
        let mut endpoint = "conversations/".to_string();
        endpoint.push_str(&self.id);
        endpoint
    }
}

///
/// CollectionConversations
///
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CollectionConversations {
    /// Recent list activity.
    #[serde(default)]
    pub conversations: Vec<Conversation>,
    /// The total number of items matching the query regardless of pagination.
    #[serde(default)]
    pub total_items: u64,
    /// A list of link types and descriptions for the API schema documents.
    #[serde(default)]
    pub _links: Vec<LinkType>,
}

impl MailchimpCollection<Conversation> for CollectionConversations {
    /// Total Items
    fn get_total_items(&self) -> u64 {
        self.total_items
    }

    /// Data
    fn get_values(&self) -> Vec<Conversation> {
        self.conversations.clone()
    }
}

impl Default for CollectionConversations {
    fn default() -> Self {
        CollectionConversations {
            conversations: Vec::new(),
            total_items: 0,
            _links: Vec::new(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct ConversationsFilter {
    /// A comma-separated list of fields to return. Reference
    /// parameters of sub-objects with dot notation.
    pub fields: Option<String>,
    /// A comma-separated list of fields to exclude. Reference
    /// parameters of sub-objects with dot notation.
    pub exclude_fields: Option<String>,
    /// The number of records to return. Default value is 10.
    pub count: Option<u64>,
    /// The number of records from a collection to skip. Iterating over
    /// large collections with this parameter can be slow. Default value is 0..
    pub offset: Option<u64>,
    /// Whether the conversation has any unread messages. Posible value "true" or "false"
    pub has_unread_messages: Option<String>,
    /// The unique id for the list.
    pub list_id: Option<String>,
    /// The unique id for the campaign.
    pub campaign_id: Option<u64>,
}

impl Default for ConversationsFilter {
    fn default() -> Self {
        ConversationsFilter {
            fields: None,
            exclude_fields: None,
            count: Some(50),
            offset: Some(0),
            has_unread_messages: None,
            list_id: None,
            campaign_id: None,
        }
    }
}

impl ResourceFilter for ConversationsFilter {
    fn build_payload(&self) -> HashMap<String, String> {
        let mut payload = HashMap::new();

        if self.fields.is_some() {
            payload.insert("fields".to_string(), self.fields.as_ref().unwrap().clone());
        }
        if self.exclude_fields.is_some() {
            payload.insert(
                "exclude_fields".to_string(),
                self.exclude_fields.as_ref().unwrap().clone(),
            );
        }
        if self.count.is_some() {
            payload.insert(
                "count".to_string(),
                format!("{:}", self.count.as_ref().unwrap().clone()),
            );
        }
        if self.offset.is_some() {
            payload.insert(
                "offset".to_string(),
                format!("{:}", self.offset.as_ref().unwrap().clone()),
            );
        }

        if self.has_unread_messages.is_some() {
            payload.insert(
                "has_unread_messages".to_string(),
                format!("{:}", self.has_unread_messages.as_ref().unwrap().clone()),
            );
        }
        if self.list_id.is_some() {
            payload.insert(
                "list_id".to_string(),
                format!("{:}", self.list_id.as_ref().unwrap().clone()),
            );
        }
        if self.campaign_id.is_some() {
            payload.insert(
                "campaign_id".to_string(),
                format!("{:}", self.campaign_id.as_ref().unwrap().clone()),
            );
        }
        payload
    }
}

///
/// Conversation Builder
///
#[derive(Debug)]
pub struct ConversationBuilder {}

impl BuildIter for ConversationBuilder {
    type Item = Conversation;
    type FilterItem = ConversationsFilter;
    type Collection = CollectionConversations;

    ///
    /// Return a new data updated
    ///
    fn update_item(&self, data: &Self::Item, api: &MailchimpApi) -> Self::Item {
        let mut in_data = data.clone();
        in_data.set_api(&api);
        in_data
    }
    ///
    /// Update the offset for pagination
    ///
    fn update_filter_offset(&self, filter: &Self::FilterItem) -> Self::FilterItem {
        let mut f = filter.clone();
        f.offset = Some(f.count.unwrap() + f.offset.unwrap());
        f
    }
}