summaryrefslogtreecommitdiff
path: root/melib/src/backends/jmap/protocol.rs
blob: 0e4db8c19ae02892f14b11faad000dc0b574082c (plain)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * meli - jmap module.
 *
 * Copyright 2019 Manos Pitsidianakis
 *
 * This file is part of meli.
 *
 * meli is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * meli is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with meli. If not, see <http://www.gnu.org/licenses/>.
 */

use super::mailbox::JmapMailbox;
use super::*;
use serde::Serialize;
use serde_json::{json, Value};
use std::convert::{TryFrom, TryInto};

pub type UtcDate = String;

use super::rfc8620::Object;

macro_rules! get_request_no {
    ($lock:expr) => {{
        let mut lck = $lock.lock().unwrap();
        let ret = *lck;
        *lck += 1;
        ret
    }};
}

pub trait Response<OBJ: Object> {
    const NAME: &'static str;
}

pub trait Method<OBJ: Object>: Serialize {
    const NAME: &'static str;
}

static USING: &[&str] = &["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"];

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Request {
    using: &'static [&'static str],
    /* Why is this Value instead of Box<dyn Method<_>>? The Method trait cannot be made into a
     * Trait object because its serialize() will be generic. */
    method_calls: Vec<Value>,

    #[serde(skip)]
    request_no: Arc<Mutex<usize>>,
}

impl Request {
    pub fn new(request_no: Arc<Mutex<usize>>) -> Self {
        Request {
            using: USING,
            method_calls: Vec::new(),
            request_no,
        }
    }

    pub fn add_call<M: Method<O>, O: Object>(&mut self, call: &M) -> usize {
        let seq = get_request_no!(self.request_no);
        self.method_calls
            .push(serde_json::to_value((M::NAME, call, &format!("m{}", seq))).unwrap());
        seq
    }
}

pub async fn get_mailboxes(conn: &JmapConnection) -> Result<HashMap<MailboxHash, JmapMailbox>> {
    let seq = get_request_no!(conn.request_no);
    let api_url = conn.session.lock().unwrap().api_url.clone();
    let mut res = conn
        .client
        .post_async(
            api_url.as_str(),
            serde_json::to_string(&json!({
                "using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"],
                "methodCalls": [["Mailbox/get", {
                "accountId": conn.mail_account_id()
                },
                 format!("#m{}",seq).as_str()]],
            }))?,
        )
        .await?;

    let res_text = res.text().await?;
    let mut v: MethodResponse = match serde_json::from_str(&res_text) {
        Err(err) => {
            let err = MeliError::new(format!("BUG: Could not deserialize {} server JSON response properly, please report this!\nReply from server: {}", &conn.server_conf.server_url, &res_text)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug);
            *conn.store.online_status.lock().await = (Instant::now(), Err(err.clone()));
            return Err(err);
        }
        Ok(s) => s,
    };
    *conn.store.online_status.lock().await = (std::time::Instant::now(), Ok(()));
    let m = GetResponse::<MailboxObject>::try_from(v.method_responses.remove(0))?;
    let GetResponse::<MailboxObject> {
        list, account_id, ..
    } = m;
    // Is account set as `personal`? (`isPersonal` property). Then, even if `isSubscribed` is false
    // on a mailbox, it should be regarded as subscribed.
    let is_personal: bool = {
        let session = conn.session_guard();
        session
            .accounts
            .get(&account_id)
            .map(|acc| acc.is_personal)
            .unwrap_or(false)
    };
    *conn.store.account_id.lock().unwrap() = account_id;
    let mut ret: HashMap<MailboxHash, JmapMailbox> = list
        .into_iter()
        .map(|r| {
            let MailboxObject {
                id,
                is_subscribed,
                my_rights,
                name,
                parent_id,
                role,
                sort_order,
                total_emails,
                total_threads,
                unread_emails,
                unread_threads,
            } = r;
            let mut total_emails_set = LazyCountSet::default();
            total_emails_set.set_not_yet_seen(total_emails.try_into().unwrap_or(0));
            let total_emails = total_emails_set;
            let mut unread_emails_set = LazyCountSet::default();
            unread_emails_set.set_not_yet_seen(unread_emails.try_into().unwrap_or(0));
            let unread_emails = unread_emails_set;
            let hash = id.into_hash();
            let parent_hash = parent_id.clone().map(|id| id.into_hash());
            (
                hash,
                JmapMailbox {
                    name: name.clone(),
                    hash,
                    path: name,
                    children: Vec::new(),
                    id,
                    is_subscribed: is_subscribed || is_personal,
                    my_rights,
                    parent_id,
                    parent_hash,
                    role,
                    usage: Default::default(),
                    sort_order,
                    total_emails: Arc::new(Mutex::new(total_emails)),
                    total_threads,
                    unread_emails: Arc::new(Mutex::new(unread_emails)),
                    unread_threads,
                    email_state: Arc::new(Mutex::new(None)),
                    email_query_state: Arc::new(Mutex::new(None)),
                },
            )
        })
        .collect();
    for key in ret.keys().cloned().collect::<SmallVec<[MailboxHash; 24]>>() {
        if let Some(parent_hash) = ret[&key].parent_hash.clone() {
            ret.entry(parent_hash).and_modify(|e| e.children.push(key));
        }
    }
    Ok(ret)
}

pub async fn get_message_list(
    conn: &JmapConnection,
    mailbox: &JmapMailbox,
) -> Result<Vec<Id<EmailObject>>> {
    let email_call: EmailQuery = EmailQuery::new(
        Query::new()
            .account_id(conn.mail_account_id().clone())
            .filter(Some(Filter::Condition(
                EmailFilterCondition::new()
                    .in_mailbox(Some(mailbox.id.clone()))
                    .into(),
            )))
            .position(0),
    )
    .collapse_threads(false);

    let mut req = Request::new(conn.request_no.clone());
    req.add_call(&email_call);

    let api_url = conn.session.lock().unwrap().api_url.clone();
    let mut res = conn
        .client
        .post_async(api_url.as_str(), serde_json::to_string(&req)?)
        .await?;

    let res_text = res.text().await?;
    let mut v: MethodResponse = match serde_json::from_str(&res_text) {
        Err(err) => {
            let err = MeliError::new(format!("BUG: Could not deserialize {} server JSON response properly, please report this!\nReply from server: {}", &conn.server_conf.server_url, &res_text)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug);
            *conn.store.online_status.lock().await = (Instant::now(), Err(err.clone()));
            return Err(err);
        }
        Ok(s) => s,
    };
    *conn.store.online_status.lock().await = (std::time::Instant::now(), Ok(()));
    let m = QueryResponse::<EmailObject>::try_from(v.method_responses.remove(0))?;
    let QueryResponse::<EmailObject> { ids, .. } = m;
    Ok(ids)
}

/*
pub async fn get_message(conn: &JmapConnection, ids: &[String]) -> Result<Vec<Envelope>> {
    let email_call: EmailGet = EmailGet::new(
        Get::new()
            .ids(Some(JmapArgument::value(ids.to_vec())))
            .account_id(conn.mail_account_id().to_string()),
    );

    let mut req = Request::new(conn.request_no.clone());
    req.add_call(&email_call);
    let mut res = conn
        .client
        .post_async(&conn.session.api_url, serde_json::to_string(&req)?)
        .await?;

    let res_text = res.text().await?;
    let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap();
    let e = GetResponse::<EmailObject>::try_from(v.method_responses.remove(0))?;
    let GetResponse::<EmailObject> { list, .. } = e;
    Ok(list
        .into_iter()
        .map(std::convert::Into::into)
        .collect::<Vec<Envelope>>())
}
*/

pub async fn fetch(
    conn: &JmapConnection,
    store: &Store,
    mailbox_hash: MailboxHash,
) -> Result<Vec<Envelope>> {
    let mailbox_id = store.mailboxes.read().unwrap()[&mailbox_hash].id.clone();
    let email_query_call: EmailQuery = EmailQuery::new(
        Query::new()
            .account_id(conn.mail_account_id().clone())
            .filter(Some(Filter::Condition(
                EmailFilterCondition::new()
                    .in_mailbox(Some(mailbox_id))
                    .into(),
            )))
            .position(0),
    )
    .collapse_threads(false);

    let mut req = Request::new(conn.request_no.clone());
    let prev_seq = req.add_call(&email_query_call);

    let email_call: EmailGet = EmailGet::new(
        Get::new()
            .ids(Some(JmapArgument::reference(
                prev_seq,
                EmailQuery::RESULT_FIELD_IDS,
            )))
            .account_id(conn.mail_account_id().clone()),
    );

    req.add_call(&email_call);

    let api_url = conn.session.lock().unwrap().api_url.clone();
    let mut res = conn
        .client
        .post_async(api_url.as_str(), serde_json::to_string(&req)?)
        .await?;

    let res_text = res.text().await?;

    let mut v: MethodResponse = match serde_json::from_str(&res_text) {
        Err(err) => {
            let err = MeliError::new(format!("BUG: Could not deserialize {} server JSON response properly, please report this!\nReply from server: {}", &conn.server_conf.server_url, &res_text)).set_source(Some(Arc::new(err))).set_kind(ErrorKind::Bug);
            *conn.store.online_status.lock().await = (Instant::now(), Err(err.clone()));
            return Err(err);
        }
        Ok(s) => s,
    };
    let e = GetResponse::<EmailObject>::try_from(v.method_responses.pop().unwrap())?;
    let query_response = QueryResponse::<EmailObject>::try_from(v.method_responses.pop().unwrap())?;
    store
        .mailboxes
        .write()
        .unwrap()
        .entry(mailbox_hash)
        .and_modify(|mbox| {
            *mbox.email_query_state.lock().unwrap() = Some(query_response.query_state);
        });
    let GetResponse::<EmailObject> { list, state, .. } = e;
    {
        let (is_empty, is_equal) = {
            let mailboxes_lck = conn.store.mailboxes.read().unwrap();
            mailboxes_lck
                .get(&mailbox_hash)
                .map(|mbox| {
                    let current_state_lck = mbox.email_state.lock().unwrap();
                    (
                        current_state_lck.is_none(),
                        current_state_lck.as_ref() != Some(&state),
                    )
                })
                .unwrap_or((true, true))
        };
        if is_empty {
            let mut mailboxes_lck = conn.store.mailboxes.write().unwrap();
            debug!("{:?}: inserting state {}", EmailObject::NAME, &state);
            mailboxes_lck.entry(mailbox_hash).and_modify(|mbox| {
                *mbox.email_state.lock().unwrap() = Some(state);
            });
        } else if !is_equal {
            conn.email_changes(mailbox_hash).await?;
        }
    }
    let mut total = BTreeSet::default();
    let mut unread = BTreeSet::default();
    let mut ret = Vec::with_capacity(list.len());
    for obj in list {
        let env = store.add_envelope(obj);
        total.insert(env.hash());
        if !env.is_seen() {
            unread.insert(env.hash());
        }
        ret.push(env);
    }
    let mut mailboxes_lck = store.mailboxes.write().unwrap();
    mailboxes_lck.entry(mailbox_hash).and_modify(|mbox| {
        mbox.total_emails.lock().unwrap().insert_existing_set(total);
        mbox.unread_emails
            .lock()
            .unwrap()
            .insert_existing_set(unread);
    });
    Ok(ret)
}

pub fn keywords_to_flags(keywords: Vec<String>) -> (Flag, Vec<String>) {
    let mut f = Flag::default();
    let mut tags = vec![];
    for k in keywords {
        match k.as_str() {
            "$draft" => {
                f |= Flag::DRAFT;
            }
            "$seen" => {
                f |= Flag::SEEN;
            }
            "$flagged" => {
                f |= Flag::FLAGGED;
            }
            "$answered" => {
                f |= Flag::REPLIED;
            }
            "$junk" | "$notjunk" => { /* ignore */ }
            _ => tags.push(k),
        }
    }
    (f, tags)
}