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
/*
 * Copyright Stalwart Labs Ltd. See the COPYING
 * file at the top-level directory of this distribution.
 *
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
 * option. This file may not be copied, modified, or distributed
 * except according to those terms.
 */

use chrono::{DateTime, Utc};
use serde::Serialize;

use crate::{
    core::{
        query::{self, QueryObject},
        set::from_timestamp,
    },
    Set,
};

use super::{EmailSubmission, UndoStatus};

#[derive(Serialize, Clone, Debug)]
#[serde(untagged)]
pub enum Filter {
    IdentityIds {
        #[serde(rename = "identityIds")]
        value: Vec<String>,
    },
    EmailIds {
        #[serde(rename = "emailIds")]
        value: Vec<String>,
    },
    ThreadIds {
        #[serde(rename = "threadIds")]
        value: Vec<String>,
    },
    UndoStatus {
        #[serde(rename = "undoStatus")]
        value: UndoStatus,
    },
    Before {
        #[serde(rename = "before")]
        value: DateTime<Utc>,
    },
    After {
        #[serde(rename = "after")]
        value: DateTime<Utc>,
    },
}

#[derive(Serialize, Debug, Clone)]
#[serde(tag = "property")]
pub enum Comparator {
    #[serde(rename = "emailId")]
    EmailId,
    #[serde(rename = "threadId")]
    ThreadId,
    #[serde(rename = "sentAt")]
    SentAt,
}

impl Filter {
    pub fn identity_ids<U, V>(value: U) -> Self
    where
        U: IntoIterator<Item = V>,
        V: Into<String>,
    {
        Filter::IdentityIds {
            value: value.into_iter().map(|v| v.into()).collect(),
        }
    }

    pub fn email_ids<U, V>(value: U) -> Self
    where
        U: IntoIterator<Item = V>,
        V: Into<String>,
    {
        Filter::EmailIds {
            value: value.into_iter().map(|v| v.into()).collect(),
        }
    }

    pub fn thread_ids<U, V>(value: U) -> Self
    where
        U: IntoIterator<Item = V>,
        V: Into<String>,
    {
        Filter::ThreadIds {
            value: value.into_iter().map(|v| v.into()).collect(),
        }
    }

    pub fn undo_status(value: UndoStatus) -> Self {
        Filter::UndoStatus { value }
    }

    pub fn before(value: i64) -> Self {
        Filter::Before {
            value: from_timestamp(value),
        }
    }

    pub fn after(value: i64) -> Self {
        Filter::After {
            value: from_timestamp(value),
        }
    }
}

impl Comparator {
    pub fn email_id() -> query::Comparator<Comparator> {
        query::Comparator::new(Comparator::EmailId)
    }

    pub fn thread_id() -> query::Comparator<Comparator> {
        query::Comparator::new(Comparator::ThreadId)
    }

    pub fn sent_at() -> query::Comparator<Comparator> {
        query::Comparator::new(Comparator::SentAt)
    }
}

impl QueryObject for EmailSubmission<Set> {
    type QueryArguments = ();

    type Filter = Filter;

    type Sort = Comparator;
}