summaryrefslogtreecommitdiff
path: root/crates/jmap/src/mailbox/set.rs
blob: ced9343402e1c99a3fc00e4da98ee9fe715dc655 (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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
use jmap_proto::{
    error::{
        method::MethodError,
        set::{SetError, SetErrorType},
    },
    method::set::{SetRequest, SetResponse},
    object::{
        index::{IndexAs, IndexProperty, ObjectIndexBuilder},
        mailbox::SetArguments,
        Object,
    },
    response::references::EvalObjectReferences,
    types::{
        acl::Acl,
        collection::Collection,
        id::Id,
        property::Property,
        state::StateChange,
        type_state::TypeState,
        value::{MaybePatchValue, SetValue, Value},
    },
};
use store::{
    query::Filter,
    roaring::RoaringBitmap,
    write::{assert::HashedValue, log::ChangeLogBuilder, BatchBuilder, F_BITMAP, F_CLEAR, F_VALUE},
};

use crate::{
    auth::{acl::EffectiveAcl, AclToken},
    JMAP, SUPERUSER_ID,
};

use super::{INBOX_ID, TRASH_ID};

struct SetContext<'x> {
    account_id: u32,
    acl_token: &'x AclToken,
    is_shared: bool,
    response: SetResponse,
    mailbox_ids: RoaringBitmap,
    will_destroy: Vec<Id>,
}

pub static SCHEMA: &[IndexProperty] = &[
    IndexProperty::new(Property::Name)
        .index_as(IndexAs::Text {
            tokenize: true,
            index: true,
        })
        .required(),
    IndexProperty::new(Property::Role).index_as(IndexAs::Text {
        tokenize: false,
        index: true,
    }),
    IndexProperty::new(Property::Role).index_as(IndexAs::HasProperty),
    IndexProperty::new(Property::ParentId).index_as(IndexAs::Integer),
    IndexProperty::new(Property::SortOrder).index_as(IndexAs::Integer),
    IndexProperty::new(Property::IsSubscribed).index_as(IndexAs::IntegerList),
    IndexProperty::new(Property::Acl).index_as(IndexAs::Acl),
];

impl JMAP {
    #[allow(clippy::blocks_in_if_conditions)]
    pub async fn mailbox_set(
        &self,
        mut request: SetRequest<SetArguments>,
        acl_token: &AclToken,
    ) -> Result<SetResponse, MethodError> {
        // Prepare response
        let account_id = request.account_id.document_id();
        let on_destroy_remove_emails = request.arguments.on_destroy_remove_emails.unwrap_or(false);
        let mut ctx = SetContext {
            account_id,
            is_shared: acl_token.is_shared(account_id),
            acl_token,
            response: self
                .prepare_set_response(&request, Collection::Mailbox)
                .await?,
            mailbox_ids: self.mailbox_get_or_create(account_id).await?,
            will_destroy: request.unwrap_destroy(),
        };

        // Process creates
        let mut changes = ChangeLogBuilder::new();
        'create: for (id, object) in request.unwrap_create() {
            match self.mailbox_set_item(object, None, &ctx).await? {
                Ok(builder) => {
                    let mut batch = BatchBuilder::new();
                    let document_id = self
                        .assign_document_id(account_id, Collection::Mailbox)
                        .await?;
                    batch
                        .with_account_id(account_id)
                        .with_collection(Collection::Mailbox)
                        .create_document(document_id)
                        .custom(builder);
                    changes.log_insert(Collection::Mailbox, document_id);
                    ctx.mailbox_ids.insert(document_id);
                    self.write_batch(batch).await?;
                    ctx.response.created(id, document_id);
                }
                Err(err) => {
                    ctx.response.not_created.append(id, err);
                    continue 'create;
                }
            }
        }

        // Process updates
        'update: for (id, object) in request.unwrap_update() {
            // Make sure id won't be destroyed
            if ctx.will_destroy.contains(&id) {
                ctx.response
                    .not_updated
                    .append(id, SetError::will_destroy());
                continue 'update;
            }

            // Obtain mailbox
            let document_id = id.document_id();
            if let Some(mailbox) = self
                .get_property::<HashedValue<Object<Value>>>(
                    account_id,
                    Collection::Mailbox,
                    document_id,
                    Property::Value,
                )
                .await?
            {
                // Validate ACL
                if ctx.is_shared {
                    let acl = mailbox.inner.effective_acl(acl_token);
                    if !acl.contains(Acl::Modify) {
                        ctx.response.not_updated.append(
                            id,
                            SetError::forbidden()
                                .with_description("You are not allowed to modify this mailbox."),
                        );
                        continue 'update;
                    } else if object.properties.contains_key(&Property::Acl)
                        && !acl.contains(Acl::Administer)
                    {
                        ctx.response.not_updated.append(
                            id,
                            SetError::forbidden().with_description(
                                "You are not allowed to change the permissions of this mailbox.",
                            ),
                        );
                        continue 'update;
                    }
                }

                match self
                    .mailbox_set_item(object, (document_id, mailbox).into(), &ctx)
                    .await?
                {
                    Ok(builder) => {
                        let mut batch = BatchBuilder::new();
                        batch
                            .with_account_id(account_id)
                            .with_collection(Collection::Mailbox)
                            .create_document(document_id)
                            .custom(builder);
                        if !batch.is_empty() {
                            changes.log_update(Collection::Mailbox, document_id);
                            match self.store.write(batch.build()).await {
                                Ok(_) => (),
                                Err(store::Error::AssertValueFailed) => {
                                    ctx.response.not_updated.append(id, SetError::forbidden().with_description(
                                        "Another process modified this mailbox, please try again.",
                                    ));
                                    continue 'update;
                                }
                                Err(err) => {
                                    tracing::error!(
                                        event = "error",
                                        context = "mailbox_set",
                                        account_id = account_id,
                                        error = ?err,
                                        "Failed to update mailbox(es).");
                                    return Err(MethodError::ServerPartialFail);
                                }
                            }
                        }
                        ctx.response.updated.append(id, None);
                    }
                    Err(err) => {
                        ctx.response.not_updated.append(id, err);
                        continue 'update;
                    }
                }
            } else {
                ctx.response.not_updated.append(id, SetError::not_found());
            }
        }

        // Process deletions
        let mut did_remove_emails = false;
        'destroy: for id in ctx.will_destroy {
            let document_id = id.document_id();
            // Internal folders cannot be deleted
            if (document_id == INBOX_ID || document_id == TRASH_ID)
                && !acl_token.is_member(SUPERUSER_ID)
            {
                ctx.response.not_destroyed.append(
                    id,
                    SetError::forbidden()
                        .with_description("You are not allowed to delete Inbox or Trash folders."),
                );
                continue;
            }

            // Verify that this mailbox does not have sub-mailboxes
            if !self
                .filter(
                    account_id,
                    Collection::Mailbox,
                    vec![Filter::eq(Property::ParentId, document_id + 1)],
                )
                .await?
                .results
                .is_empty()
            {
                ctx.response.not_destroyed.append(
                    id,
                    SetError::new(SetErrorType::MailboxHasChild)
                        .with_description("Mailbox has at least one children."),
                );
                continue;
            }

            // Verify that the mailbox is empty
            if let Some(message_ids) = self
                .get_tag(
                    account_id,
                    Collection::Email,
                    Property::MailboxIds,
                    document_id,
                )
                .await?
            {
                if on_destroy_remove_emails {
                    // Flag removal for state change notification
                    did_remove_emails = true;

                    // If the message is in multiple mailboxes, untag it from the current mailbox,
                    // otherwise delete it.
                    for message_id in message_ids {
                        // Obtain mailboxIds
                        if let Some(mailbox_ids) = self
                            .get_property::<HashedValue<Vec<u32>>>(
                                account_id,
                                Collection::Email,
                                message_id,
                                Property::MailboxIds,
                            )
                            .await?
                            .and_then(|mut ids| {
                                let idx = ids.inner.iter().position(|&id| id == document_id)?;
                                ids.inner.swap_remove(idx);
                                Some(ids)
                            })
                        {
                            if !mailbox_ids.inner.is_empty() {
                                // Obtain threadId
                                if let Some(thread_id) = self
                                    .get_property::<u32>(
                                        account_id,
                                        Collection::Email,
                                        message_id,
                                        Property::ThreadId,
                                    )
                                    .await?
                                {
                                    // Untag message from mailbox
                                    let mut batch = BatchBuilder::new();
                                    batch
                                        .with_account_id(account_id)
                                        .with_collection(Collection::Email)
                                        .update_document(message_id)
                                        .assert_value(Property::MailboxIds, &mailbox_ids)
                                        .value(Property::MailboxIds, mailbox_ids.inner, F_VALUE)
                                        .value(
                                            Property::MailboxIds,
                                            document_id,
                                            F_BITMAP | F_CLEAR,
                                        );
                                    match self.store.write(batch.build()).await {
                                        Ok(_) => changes.log_update(
                                            Collection::Email,
                                            Id::from_parts(thread_id, message_id),
                                        ),
                                        Err(store::Error::AssertValueFailed) => {
                                            ctx.response.not_destroyed.append(
                                                id,
                                                SetError::forbidden().with_description(
                                                    concat!("Another process modified a message in this mailbox ",
                                                    "while deleting it, please try again.")
                                                ),
                                            );
                                            continue 'destroy;
                                        }
                                        Err(err) => {
                                            tracing::error!(
                                                    event = "error",
                                                    context = "mailbox_set",
                                                    account_id = account_id,
                                                    mailbox_id = document_id,
                                                    message_id = message_id,
                                                    error = ?err,
                                                    "Failed to update message while deleting mailbox.");
                                            return Err(MethodError::ServerPartialFail);
                                        }
                                    }
                                } else {
                                    tracing::debug!(
                                        event = "error",
                                        context = "mailbox_set",
                                        account_id = account_id,
                                        mailbox_id = document_id,
                                        message_id = message_id,
                                        "Message does not have a threadId, skipping."
                                    );
                                }
                            } else {
                                // Delete message
                                if let Ok(mut change) =
                                    self.email_delete(account_id, message_id).await?
                                {
                                    change.changes.remove(&(Collection::Mailbox as u8));
                                    changes.merge(change);
                                }
                            }
                        } else {
                            tracing::debug!(
                                event = "error",
                                context = "mailbox_set",
                                account_id = account_id,
                                mailbox_id = document_id,
                                message_id = message_id,
                                "Message is not in the mailbox, skipping."
                            );
                        }
                    }
                } else {
                    ctx.response.not_destroyed.append(
                        id,
                        SetError::new(SetErrorType::MailboxHasEmail)
                            .with_description("Mailbox is not empty."),
                    );
                    continue;
                }
            }

            // Obtain mailbox
            if let Some(mailbox) = self
                .get_property::<HashedValue<Object<Value>>>(
                    account_id,
                    Collection::Mailbox,
                    document_id,
                    Property::Value,
                )
                .await?
            {
                // Validate ACLs
                if ctx.is_shared {
                    let acl = mailbox.inner.effective_acl(acl_token);
                    if !acl.contains(Acl::Administer) {
                        if !acl.contains(Acl::Delete) {
                            ctx.response.not_destroyed.append(
                                id,
                                SetError::forbidden().with_description(
                                    "You are not allowed to delete this mailbox.",
                                ),
                            );
                            continue 'destroy;
                        } else if on_destroy_remove_emails && !acl.contains(Acl::RemoveItems) {
                            ctx.response.not_destroyed.append(
                                id,
                                SetError::forbidden().with_description(
                                    "You are not allowed to delete emails from this mailbox.",
                                ),
                            );
                            continue 'destroy;
                        }
                    }
                }

                let mut batch = BatchBuilder::new();
                batch
                    .with_account_id(account_id)
                    .with_collection(Collection::Mailbox)
                    .delete_document(document_id)
                    .custom(ObjectIndexBuilder::new(SCHEMA).with_current(mailbox));

                match self.store.write(batch.build()).await {
                    Ok(_) => {
                        changes.log_delete(Collection::Mailbox, document_id);
                        ctx.response.destroyed.push(id);
                    }
                    Err(store::Error::AssertValueFailed) => {
                        ctx.response.not_destroyed.append(
                            id,
                            SetError::forbidden().with_description(concat!(
                                "Another process modified this mailbox ",
                                "while deleting it, please try again."
                            )),
                        );
                    }
                    Err(err) => {
                        tracing::error!(
                                    event = "error",
                                    context = "mailbox_set",
                                    account_id = account_id,
                                    document_id = document_id,
                                    error = ?err,
                                    "Failed to delete mailbox.");
                        return Err(MethodError::ServerPartialFail);
                    }
                }
            } else {
                ctx.response.not_destroyed.append(id, SetError::not_found());
            }
        }

        // Write changes
        if !changes.is_empty() {
            let state_change =
                StateChange::new(account_id).with_change(TypeState::Mailbox, changes.change_id);
            ctx.response.state_change = if did_remove_emails {
                state_change
                    .with_change(TypeState::Email, changes.change_id)
                    .with_change(TypeState::Thread, changes.change_id)
            } else {
                state_change
            }
            .into();
            ctx.response.new_state = self.commit_changes(account_id, changes).await?.into();
        }

        Ok(ctx.response)
    }

    #[allow(clippy::blocks_in_if_conditions)]
    async fn mailbox_set_item(
        &self,
        changes_: Object<SetValue>,
        update: Option<(u32, HashedValue<Object<Value>>)>,
        ctx: &SetContext<'_>,
    ) -> Result<Result<ObjectIndexBuilder, SetError>, MethodError> {
        // Parse properties
        let mut changes = Object::with_capacity(changes_.properties.len());
        for (property, value) in changes_.properties {
            let value = match ctx.response.eval_object_references(value) {
                Ok(value) => value,
                Err(err) => {
                    return Ok(Err(err));
                }
            };
            let value = match (&property, value) {
                (Property::Name, MaybePatchValue::Value(Value::Text(value))) => {
                    let value = value.trim();
                    if !value.is_empty() && value.len() < self.config.mailbox_name_max_len {
                        Value::Text(value.to_string())
                    } else {
                        return Ok(Err(SetError::invalid_properties()
                            .with_property(Property::Name)
                            .with_description(
                                if !value.is_empty() {
                                    "Mailbox name is too long."
                                } else {
                                    "Mailbox name cannot be empty."
                                }
                                .to_string(),
                            )));
                    }
                }
                (Property::ParentId, MaybePatchValue::Value(Value::Id(value))) => {
                    let parent_id = value.document_id();
                    if ctx.will_destroy.contains(&value) {
                        return Ok(Err(SetError::will_destroy()
                            .with_description("Parent ID will be destroyed.")));
                    } else if !ctx.mailbox_ids.contains(parent_id) {
                        return Ok(Err(SetError::invalid_properties()
                            .with_description("Parent ID does not exist.")));
                    }

                    Value::Id((parent_id + 1).into())
                }
                (Property::ParentId, MaybePatchValue::Value(Value::Null)) => Value::Id(0u64.into()),
                (Property::IsSubscribed, MaybePatchValue::Value(Value::Bool(subscribe))) => {
                    let account_id = Value::Id(ctx.acl_token.primary_id().into());
                    let mut new_value = None;
                    if let Some((_, current_fields)) = update.as_ref() {
                        if let Value::List(subscriptions) =
                            current_fields.inner.get(&Property::IsSubscribed)
                        {
                            if subscribe {
                                if !subscriptions.contains(&account_id) {
                                    let mut current_subscriptions = subscriptions.clone();
                                    current_subscriptions.push(account_id.clone());
                                    new_value = Value::List(current_subscriptions).into();
                                } else {
                                    continue;
                                }
                            } else if subscriptions.contains(&account_id) {
                                if subscriptions.len() > 1 {
                                    new_value = Value::List(
                                        subscriptions
                                            .iter()
                                            .filter(|id| *id != &account_id)
                                            .cloned()
                                            .collect(),
                                    )
                                    .into();
                                } else {
                                    new_value = Value::Null.into();
                                }
                            } else {
                                continue;
                            }
                        }
                    }

                    if let Some(new_value) = new_value {
                        new_value
                    } else if subscribe {
                        Value::List(vec![account_id])
                    } else {
                        continue;
                    }
                }
                (Property::Role, MaybePatchValue::Value(Value::Text(value))) => {
                    let role = value.trim().to_lowercase();
                    if [
                        "inbox", "trash", "spam", "junk", "drafts", "archive", "sent",
                    ]
                    .contains(&role.as_str())
                    {
                        Value::Text(role)
                    } else {
                        return Ok(Err(SetError::invalid_properties()
                            .with_property(Property::Role)
                            .with_description(format!("Invalid role {role:?}."))));
                    }
                }
                (Property::Role, MaybePatchValue::Value(Value::Null)) => Value::Null,
                (Property::SortOrder, MaybePatchValue::Value(Value::UnsignedInt(value))) => {
                    Value::UnsignedInt(value)
                }
                (Property::Acl, value) => {
                    match self
                        .acl_set(&mut changes, update.as_ref().map(|(_, obj)| obj), value)
                        .await
                    {
                        Ok(_) => continue,
                        Err(err) => {
                            return Ok(Err(err));
                        }
                    }
                }

                _ => {
                    return Ok(Err(SetError::invalid_properties()
                        .with_property(property)
                        .with_description("Invalid property or value.".to_string())))
                }
            };

            changes.append(property, value);
        }

        // Validate depth and circular parent-child relationship
        if let Value::Id(mailbox_parent_id) = changes.get(&Property::ParentId) {
            let current_mailbox_id = update
                .as_ref()
                .map_or(u32::MAX, |(mailbox_id, _)| *mailbox_id + 1);
            let mut mailbox_parent_id = mailbox_parent_id.document_id();
            let mut success = false;
            for depth in 0..self.config.mailbox_max_depth {
                if mailbox_parent_id == current_mailbox_id {
                    return Ok(Err(SetError::invalid_properties()
                        .with_property(Property::ParentId)
                        .with_description("Mailbox cannot be a parent of itself.")));
                } else if mailbox_parent_id == 0 {
                    if depth == 0 && ctx.is_shared {
                        return Ok(Err(SetError::forbidden()
                            .with_description("You are not allowed to create root folders.")));
                    }
                    success = true;
                    break;
                }
                let parent_document_id = mailbox_parent_id - 1;

                if let Some(mut fields) = self
                    .get_property::<Object<Value>>(
                        ctx.account_id,
                        Collection::Mailbox,
                        parent_document_id,
                        Property::Value,
                    )
                    .await?
                {
                    if depth == 0
                        && ctx.is_shared
                        && !fields
                            .effective_acl(ctx.acl_token)
                            .contains_any([Acl::CreateChild, Acl::Administer].into_iter())
                    {
                        return Ok(Err(SetError::forbidden().with_description(
                            "You are not allowed to create sub mailboxes under this mailbox.",
                        )));
                    }

                    mailbox_parent_id = fields
                        .properties
                        .remove(&Property::ParentId)
                        .and_then(|v| v.try_unwrap_id().map(|id| id.document_id()))
                        .unwrap_or(0);
                } else if ctx.mailbox_ids.contains(parent_document_id) {
                    // Parent mailbox is probably created within the same request
                    success = true;
                    break;
                } else {
                    return Ok(Err(SetError::invalid_properties()
                        .with_property(Property::ParentId)
                        .with_description("Mailbox parent does not exist.")));
                }
            }

            if !success {
                return Ok(Err(SetError::invalid_properties()
                    .with_property(Property::ParentId)
                    .with_description(
                        "Mailbox parent-child relationship is too deep.",
                    )));
            }
        } else if update.is_none() {
            // Set parentId if the field is missing
            changes.append(Property::ParentId, Value::Id(0u64.into()));
        }

        // Verify that the mailbox role is unique.
        if let Value::Text(mailbox_role) = changes.get(&Property::Role) {
            if update
                .as_ref()
                .map(|(_, update)| update.inner.get(&Property::Role))
                .and_then(|v| v.as_string())
                .unwrap_or_default()
                != mailbox_role
            {
                if !self
                    .filter(
                        ctx.account_id,
                        Collection::Mailbox,
                        vec![Filter::eq(Property::Role, mailbox_role.as_str())],
                    )
                    .await?
                    .results
                    .is_empty()
                {
                    return Ok(Err(SetError::invalid_properties()
                        .with_property(Property::Role)
                        .with_description(format!(
                            "A mailbox with role '{}' already exists.",
                            mailbox_role
                        ))));
                }

                // Role of internal folders cannot be modified
                if update.as_ref().map_or(false, |(document_id, _)| {
                    *document_id == INBOX_ID || *document_id == TRASH_ID
                }) {
                    return Ok(Err(SetError::invalid_properties()
                        .with_property(Property::Role)
                        .with_description(
                            "You are not allowed to change the role of Inbox or Trash folders.",
                        )));
                }
            }
        }

        // Verify that the mailbox name is unique.
        if let Value::Text(mailbox_name) = changes.get(&Property::Name) {
            // Obtain parent mailbox id
            if let Some(parent_mailbox_id) = if let Some(mailbox_parent_id) = &changes
                .properties
                .get(&Property::ParentId)
                .and_then(|id| id.as_id().map(|id| id.document_id()))
            {
                (*mailbox_parent_id).into()
            } else if let Some((_, current_fields)) = &update {
                if current_fields
                    .inner
                    .properties
                    .get(&Property::Name)
                    .and_then(|n| n.as_string())
                    != Some(mailbox_name)
                {
                    current_fields
                        .inner
                        .properties
                        .get(&Property::ParentId)
                        .and_then(|id| id.as_id().map(|id| id.document_id()))
                        .unwrap_or_default()
                        .into()
                } else {
                    None
                }
            } else {
                0.into()
            } {
                if !self
                    .filter(
                        ctx.account_id,
                        Collection::Mailbox,
                        vec![
                            Filter::eq(Property::Name, mailbox_name.as_str()),
                            Filter::eq(Property::ParentId, parent_mailbox_id),
                        ],
                    )
                    .await?
                    .results
                    .is_empty()
                {
                    return Ok(Err(SetError::invalid_properties()
                        .with_property(Property::Name)
                        .with_description(format!(
                            "A mailbox with name '{}' already exists.",
                            mailbox_name
                        ))));
                }
            }
        }

        // Refresh ACLs
        let current = update.map(|(_, current)| current);
        if changes.properties.contains_key(&Property::Acl) {
            self.refresh_acls(&changes, &current);
        }

        // Validate
        Ok(ObjectIndexBuilder::new(SCHEMA)
            .with_changes(changes)
            .with_current_opt(current)
            .validate())
    }

    pub async fn mailbox_get_or_create(
        &self,
        account_id: u32,
    ) -> Result<RoaringBitmap, MethodError> {
        let mut mailbox_ids = self
            .get_document_ids(account_id, Collection::Mailbox)
            .await?
            .unwrap_or_default();
        if !mailbox_ids.is_empty() {
            return Ok(mailbox_ids);
        }

        let mut batch = BatchBuilder::new();
        batch
            .with_account_id(account_id)
            .with_collection(Collection::Mailbox);

        // Create mailboxes
        for (name, role) in [
            ("Inbox", "inbox"),
            ("Deleted Items", "trash"),
            ("Drafts", "drafts"),
            ("Sent Items", "sent"),
            ("Junk Mail", "junk"),
        ] {
            let mailbox_id = self
                .assign_document_id(account_id, Collection::Mailbox)
                .await?;
            batch.create_document(mailbox_id).custom(
                ObjectIndexBuilder::new(SCHEMA).with_changes(
                    Object::with_capacity(3)
                        .with_property(Property::Name, name)
                        .with_property(Property::Role, role)
                        .with_property(Property::ParentId, 0u32),
                ),
            );
            mailbox_ids.insert(mailbox_id);
        }
        self.store.write(batch.build()).await.map_err(|err| {
            tracing::error!(
                event = "error",
                context = "mailbox_get_or_create",
                error = ?err,
                "Failed to create mailboxes.");
            MethodError::ServerPartialFail
        })?;

        Ok(mailbox_ids)
    }

    pub async fn mailbox_create_path(
        &self,
        account_id: u32,
        path: &str,
    ) -> Result<Option<(u32, Option<u64>)>, MethodError> {
        let expanded_path =
            if let Some(expand_path) = self.mailbox_expand_path(account_id, path, false).await? {
                expand_path
            } else {
                return Ok(None);
            };

        let mut next_parent_id = 0;
        let mut path = expanded_path.path.into_iter().peekable();
        'outer: while let Some(name) = path.peek() {
            for (part, parent_id, document_id) in &expanded_path.found_names {
                if part.eq(name) && *parent_id == next_parent_id {
                    next_parent_id = *document_id;
                    path.next();
                    continue 'outer;
                }
            }
            break;
        }

        // Create missing folders
        if path.peek().is_some() {
            let mut batch = BatchBuilder::new();
            let mut changes = self.begin_changes(account_id).await?;
            batch
                .with_account_id(account_id)
                .with_collection(Collection::Mailbox);

            for name in path {
                if name.len() > self.config.mailbox_name_max_len {
                    return Ok(None);
                }

                let document_id = self
                    .assign_document_id(account_id, Collection::Mailbox)
                    .await?;
                batch.create_document(document_id).custom(
                    ObjectIndexBuilder::new(SCHEMA).with_changes(
                        Object::with_capacity(2)
                            .with_property(Property::Name, name)
                            .with_property(Property::ParentId, Value::Id(Id::from(next_parent_id))),
                    ),
                );
                changes.log_insert(Collection::Mailbox, document_id);
                next_parent_id = document_id + 1;
            }
            let change_id = changes.change_id;
            batch.custom(changes);
            self.write_batch(batch).await?;

            Ok(Some((next_parent_id - 1, Some(change_id))))
        } else {
            Ok(Some((next_parent_id - 1, None)))
        }
    }
}