jmap_client/email_submission/
query.rs

1/*
2 * Copyright Stalwart Labs LLC See the COPYING
3 * file at the top-level directory of this distribution.
4 *
5 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8 * option. This file may not be copied, modified, or distributed
9 * except according to those terms.
10 */
11
12use chrono::{DateTime, Utc};
13use serde::Serialize;
14
15use crate::{
16    core::{
17        query::{self, QueryObject},
18        set::from_timestamp,
19    },
20    Set,
21};
22
23use super::{EmailSubmission, UndoStatus};
24
25#[derive(Serialize, Clone, Debug)]
26#[serde(untagged)]
27pub enum Filter {
28    IdentityIds {
29        #[serde(rename = "identityIds")]
30        value: Vec<String>,
31    },
32    EmailIds {
33        #[serde(rename = "emailIds")]
34        value: Vec<String>,
35    },
36    ThreadIds {
37        #[serde(rename = "threadIds")]
38        value: Vec<String>,
39    },
40    UndoStatus {
41        #[serde(rename = "undoStatus")]
42        value: UndoStatus,
43    },
44    Before {
45        #[serde(rename = "before")]
46        value: DateTime<Utc>,
47    },
48    After {
49        #[serde(rename = "after")]
50        value: DateTime<Utc>,
51    },
52}
53
54#[derive(Serialize, Debug, Clone)]
55#[serde(tag = "property")]
56pub enum Comparator {
57    #[serde(rename = "emailId")]
58    EmailId,
59    #[serde(rename = "threadId")]
60    ThreadId,
61    #[serde(rename = "sentAt")]
62    SentAt,
63}
64
65impl Filter {
66    pub fn identity_ids<U, V>(value: U) -> Self
67    where
68        U: IntoIterator<Item = V>,
69        V: Into<String>,
70    {
71        Filter::IdentityIds {
72            value: value.into_iter().map(|v| v.into()).collect(),
73        }
74    }
75
76    pub fn email_ids<U, V>(value: U) -> Self
77    where
78        U: IntoIterator<Item = V>,
79        V: Into<String>,
80    {
81        Filter::EmailIds {
82            value: value.into_iter().map(|v| v.into()).collect(),
83        }
84    }
85
86    pub fn thread_ids<U, V>(value: U) -> Self
87    where
88        U: IntoIterator<Item = V>,
89        V: Into<String>,
90    {
91        Filter::ThreadIds {
92            value: value.into_iter().map(|v| v.into()).collect(),
93        }
94    }
95
96    pub fn undo_status(value: UndoStatus) -> Self {
97        Filter::UndoStatus { value }
98    }
99
100    pub fn before(value: i64) -> Self {
101        Filter::Before {
102            value: from_timestamp(value),
103        }
104    }
105
106    pub fn after(value: i64) -> Self {
107        Filter::After {
108            value: from_timestamp(value),
109        }
110    }
111}
112
113impl Comparator {
114    pub fn email_id() -> query::Comparator<Comparator> {
115        query::Comparator::new(Comparator::EmailId)
116    }
117
118    pub fn thread_id() -> query::Comparator<Comparator> {
119        query::Comparator::new(Comparator::ThreadId)
120    }
121
122    pub fn sent_at() -> query::Comparator<Comparator> {
123        query::Comparator::new(Comparator::SentAt)
124    }
125}
126
127impl QueryObject for EmailSubmission<Set> {
128    type QueryArguments = ();
129
130    type Filter = Filter;
131
132    type Sort = Comparator;
133}