summaryrefslogtreecommitdiff
path: root/crates/jmap/src/email/get.rs
blob: 1709e36c856f5bdbeea485d5ecdbc2ab6d547f67 (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd <hello@stalw.art>
 *
 * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
 */

use jmap_proto::{
    method::get::{GetRequest, GetResponse},
    object::{email::GetArguments, Object},
    types::{
        acl::Acl,
        blob::BlobId,
        collection::Collection,
        date::UTCDate,
        id::Id,
        keyword::Keyword,
        property::{HeaderForm, Property},
        value::Value,
    },
};
use mail_parser::HeaderName;
use store::{write::Bincode, BlobClass};
use trc::{AddContext, StoreEvent};

use crate::{auth::AccessToken, email::headers::HeaderToValue, mailbox::UidMailbox, JMAP};

use super::{
    body::{ToBodyPart, TruncateBody},
    headers::IntoForm,
    metadata::{MessageMetadata, MetadataPartType},
};

impl JMAP {
    pub async fn email_get(
        &self,
        mut request: GetRequest<GetArguments>,
        access_token: &AccessToken,
    ) -> trc::Result<GetResponse> {
        let ids = request.unwrap_ids(self.core.jmap.get_max_objects)?;
        let properties = request.unwrap_properties(&[
            Property::Id,
            Property::BlobId,
            Property::ThreadId,
            Property::MailboxIds,
            Property::Keywords,
            Property::Size,
            Property::ReceivedAt,
            Property::MessageId,
            Property::InReplyTo,
            Property::References,
            Property::Sender,
            Property::From,
            Property::To,
            Property::Cc,
            Property::Bcc,
            Property::ReplyTo,
            Property::Subject,
            Property::SentAt,
            Property::HasAttachment,
            Property::Preview,
            Property::BodyValues,
            Property::TextBody,
            Property::HtmlBody,
            Property::Attachments,
        ]);
        let body_properties = request.arguments.body_properties.unwrap_or_else(|| {
            vec![
                Property::PartId,
                Property::BlobId,
                Property::Size,
                Property::Name,
                Property::Type,
                Property::Charset,
                Property::Disposition,
                Property::Cid,
                Property::Language,
                Property::Location,
            ]
        });
        let fetch_text_body_values = request.arguments.fetch_text_body_values.unwrap_or(false);
        let fetch_html_body_values = request.arguments.fetch_html_body_values.unwrap_or(false);
        let fetch_all_body_values = request.arguments.fetch_all_body_values.unwrap_or(false);
        let max_body_value_bytes = request.arguments.max_body_value_bytes.unwrap_or(0);

        let account_id = request.account_id.document_id();
        let message_ids = self
            .owned_or_shared_messages(access_token, account_id, Acl::ReadItems)
            .await?;
        let ids = if let Some(ids) = ids {
            ids
        } else {
            let document_ids = message_ids
                .iter()
                .take(self.core.jmap.get_max_objects)
                .collect::<Vec<_>>();
            self.get_cached_thread_ids(account_id, document_ids.iter().copied())
                .await
                .caused_by(trc::location!())?
                .into_iter()
                .filter_map(|(document_id, thread_id)| {
                    Id::from_parts(thread_id, document_id).into()
                })
                .collect()
        };
        let mut response = GetResponse {
            account_id: request.account_id.into(),
            state: self.get_state(account_id, Collection::Email).await?.into(),
            list: Vec::with_capacity(ids.len()),
            not_found: vec![],
        };

        // Check if we need to fetch the raw headers or body
        let mut needs_body = false;
        for property in &properties {
            if matches!(
                property,
                Property::BodyValues
                    | Property::TextBody
                    | Property::HtmlBody
                    | Property::Attachments
                    | Property::BodyStructure
            ) {
                needs_body = true;
                break;
            }
        }

        'outer: for id in ids {
            // Obtain the email object
            if !message_ids.contains(id.document_id()) {
                response.not_found.push(id.into());
                continue;
            }
            let mut metadata = match self
                .get_property::<Bincode<MessageMetadata>>(
                    account_id,
                    Collection::Email,
                    id.document_id(),
                    &Property::BodyStructure,
                )
                .await?
            {
                Some(metadata) => metadata.inner,
                None => {
                    response.not_found.push(id.into());
                    continue;
                }
            };

            // Retrieve raw message if needed
            let raw_message = if needs_body {
                if let Some(raw_message) = self.get_blob(&metadata.blob_hash, 0..usize::MAX).await?
                {
                    raw_message
                } else {
                    trc::event!(
                        Store(StoreEvent::NotFound),
                        AccountId = account_id,
                        DocumentId = id.document_id(),
                        Collection = Collection::Email,
                        BlobId = metadata.blob_hash.to_hex(),
                        Details = "Blob not found.",
                        CausedBy = trc::location!(),
                    );

                    response.not_found.push(id.into());
                    continue;
                }
            } else {
                metadata.raw_headers
            };
            let blob_id = BlobId {
                hash: metadata.blob_hash.clone(),
                class: BlobClass::Linked {
                    account_id,
                    collection: Collection::Email.into(),
                    document_id: id.document_id(),
                },
                section: None,
            };

            // Prepare response
            let mut email = Object::with_capacity(properties.len());
            for property in &properties {
                match property {
                    Property::Id => {
                        email.append(Property::Id, Id::from(*id));
                    }
                    Property::ThreadId => {
                        email.append(Property::ThreadId, Id::from(id.prefix_id()));
                    }
                    Property::BlobId => {
                        email.append(Property::BlobId, blob_id.clone());
                    }
                    Property::MailboxIds => {
                        if let Some(mailboxes) = self
                            .get_property::<Vec<UidMailbox>>(
                                account_id,
                                Collection::Email,
                                id.document_id(),
                                &Property::MailboxIds,
                            )
                            .await?
                            .map(|ids| {
                                let mut obj = Object::with_capacity(ids.len());
                                for id in ids {
                                    debug_assert!(id.uid != 0);
                                    obj.append(
                                        Property::_T(Id::from(id.mailbox_id).to_string()),
                                        true,
                                    );
                                }
                                Value::Object(obj)
                            })
                        {
                            email.append(property.clone(), mailboxes);
                        } else {
                            trc::event!(
                                Store(StoreEvent::NotFound),
                                AccountId = account_id,
                                DocumentId = id.document_id(),
                                Collection = Collection::Email,
                                Details = "Mailbox property not found.",
                                CausedBy = trc::location!(),
                            );

                            response.not_found.push(id.into());
                            continue 'outer;
                        }
                    }
                    Property::Keywords => {
                        if let Some(keywords) = self
                            .get_property::<Vec<Keyword>>(
                                account_id,
                                Collection::Email,
                                id.document_id(),
                                &Property::Keywords,
                            )
                            .await?
                            .map(|keywords| {
                                let mut obj = Object::with_capacity(keywords.len());
                                for keyword in keywords {
                                    obj.append(Property::_T(keyword.to_string()), true);
                                }
                                Value::Object(obj)
                            })
                        {
                            email.append(property.clone(), keywords);
                        } else {
                            trc::event!(
                                Store(StoreEvent::NotFound),
                                AccountId = account_id,
                                DocumentId = id.document_id(),
                                Collection = Collection::Email,
                                Details = "Keywords property not found.",
                                CausedBy = trc::location!(),
                            );

                            response.not_found.push(id.into());
                            continue 'outer;
                        }
                    }
                    Property::Size => {
                        email.append(Property::Size, metadata.size);
                    }
                    Property::ReceivedAt => {
                        email.append(
                            Property::ReceivedAt,
                            Value::Date(UTCDate::from_timestamp(metadata.received_at as i64)),
                        );
                    }
                    Property::Preview => {
                        if !metadata.preview.is_empty() {
                            email.append(Property::Preview, std::mem::take(&mut metadata.preview));
                        }
                    }
                    Property::HasAttachment => {
                        email.append(Property::HasAttachment, metadata.has_attachments);
                    }
                    Property::Subject => {
                        email.append(
                            Property::Subject,
                            metadata.contents.parts[0]
                                .remove_header(&HeaderName::Subject)
                                .map(|value| value.into_form(&HeaderForm::Text))
                                .unwrap_or_default(),
                        );
                    }
                    Property::SentAt => {
                        email.append(
                            Property::SentAt,
                            metadata.contents.parts[0]
                                .remove_header(&HeaderName::Date)
                                .map(|value| value.into_form(&HeaderForm::Date))
                                .unwrap_or_default(),
                        );
                    }
                    Property::MessageId | Property::InReplyTo | Property::References => {
                        email.append(
                            property.clone(),
                            metadata.contents.parts[0]
                                .remove_header(&match property {
                                    Property::MessageId => HeaderName::MessageId,
                                    Property::InReplyTo => HeaderName::InReplyTo,
                                    Property::References => HeaderName::References,
                                    _ => unreachable!(),
                                })
                                .map(|value| value.into_form(&HeaderForm::MessageIds))
                                .unwrap_or_default(),
                        );
                    }

                    Property::Sender
                    | Property::From
                    | Property::To
                    | Property::Cc
                    | Property::Bcc
                    | Property::ReplyTo => {
                        email.append(
                            property.clone(),
                            metadata.contents.parts[0]
                                .remove_header(&match property {
                                    Property::Sender => HeaderName::Sender,
                                    Property::From => HeaderName::From,
                                    Property::To => HeaderName::To,
                                    Property::Cc => HeaderName::Cc,
                                    Property::Bcc => HeaderName::Bcc,
                                    Property::ReplyTo => HeaderName::ReplyTo,
                                    _ => unreachable!(),
                                })
                                .map(|value| value.into_form(&HeaderForm::Addresses))
                                .unwrap_or_default(),
                        );
                    }
                    Property::Header(_) => {
                        email.append(
                            property.clone(),
                            metadata.contents.parts[0]
                                .headers
                                .header_to_value(property, &raw_message),
                        );
                    }
                    Property::Headers => {
                        email.append(
                            Property::Headers,
                            metadata.contents.parts[0]
                                .headers
                                .headers_to_value(&raw_message),
                        );
                    }
                    Property::TextBody | Property::HtmlBody | Property::Attachments => {
                        let list = match property {
                            Property::TextBody => &metadata.contents.text_body,
                            Property::HtmlBody => &metadata.contents.html_body,
                            Property::Attachments => &metadata.contents.attachments,
                            _ => unreachable!(),
                        }
                        .iter();
                        email.append(
                            property.clone(),
                            list.map(|part_id| {
                                metadata.contents.to_body_part(
                                    *part_id,
                                    &body_properties,
                                    &raw_message,
                                    &blob_id,
                                )
                            })
                            .collect::<Vec<_>>(),
                        );
                    }
                    Property::BodyStructure => {
                        email.append(
                            Property::BodyStructure,
                            metadata.contents.to_body_part(
                                0,
                                &body_properties,
                                &raw_message,
                                &blob_id,
                            ),
                        );
                    }
                    Property::BodyValues => {
                        let mut body_values = Object::with_capacity(metadata.contents.parts.len());
                        for (part_id, part) in metadata.contents.parts.iter().enumerate() {
                            if ((metadata.contents.html_body.contains(&part_id)
                                && (fetch_all_body_values || fetch_html_body_values))
                                || (metadata.contents.text_body.contains(&part_id)
                                    && (fetch_all_body_values || fetch_text_body_values)))
                                && matches!(
                                    part.body,
                                    MetadataPartType::Text | MetadataPartType::Html
                                )
                            {
                                let (is_truncated, value) = part
                                    .decode_contents(&raw_message)
                                    .truncate(max_body_value_bytes);
                                body_values.append(
                                    Property::_T(part_id.to_string()),
                                    Object::with_capacity(3)
                                        .with_property(
                                            Property::IsEncodingProblem,
                                            part.is_encoding_problem,
                                        )
                                        .with_property(Property::IsTruncated, is_truncated)
                                        .with_property(Property::Value, value),
                                );
                            }
                        }
                        email.append(Property::BodyValues, body_values);
                    }

                    _ => {
                        return Err(trc::JmapEvent::InvalidArguments
                            .into_err()
                            .details(format!("Invalid property {property:?}")));
                    }
                }
            }
            response.list.push(email);
        }

        Ok(response)
    }
}