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
//! Docket's primitives. A received message carries its own state, folder,
//! and assignees; a thread is only a grouping, and owns nothing but its
//! comments.

use std::collections::BTreeSet;
use std::fmt;

use jiff::civil::DateTime;

pub type MessageId = String;
pub type ThreadId = String;
pub type CommentId = String;

/// The reverse-hex alphabet jj's change ids use: nibble `0` renders as
/// `z`, so the ids carry no order and no meaning.
pub(crate) const REVERSE_HEX: [char; 16] = [
    'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k',
];

pub(crate) fn reverse_hex(bits: u64) -> String {
    reverse_hex_bytes(&bits.to_be_bytes())
}

pub(crate) fn reverse_hex_bytes(bytes: &[u8]) -> String {
    bytes
        .iter()
        .flat_map(|b| [b >> 4, b & 0xf])
        .filter_map(|nibble| REVERSE_HEX.get(usize::from(nibble)).copied())
        .collect()
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum State {
    Inbox,
    Do,
    Wait,
    Watch,
    Done,
}

impl State {
    pub const ALL: [State; 5] = [
        State::Inbox,
        State::Do,
        State::Wait,
        State::Watch,
        State::Done,
    ];

    /// The states that have a lane. Done is reached through search only.
    pub const LANES: [State; 4] = [State::Inbox, State::Do, State::Wait, State::Watch];

    pub fn name(self) -> &'static str {
        match self {
            State::Inbox => "Inbox",
            State::Do => "Do",
            State::Wait => "Wait",
            State::Watch => "Watch",
            State::Done => "Done",
        }
    }

    pub fn slug(self) -> &'static str {
        match self {
            State::Inbox => "inbox",
            State::Do => "do",
            State::Wait => "wait",
            State::Watch => "watch",
            State::Done => "done",
        }
    }

    pub fn from_slug(slug: &str) -> Option<State> {
        State::ALL.into_iter().find(|s| s.slug() == slug)
    }
}

impl fmt::Display for State {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.name())
    }
}

/// Someone Tailscale let in. Tailscale is the whole of authentication, so
/// anyone who reaches Docket is a user.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct User {
    /// The `Remote-User` value that identifies this user.
    pub login: String,
    /// The short name shown for them. Not unique: two logins can share one.
    pub slug: String,
}

impl User {
    pub fn new(login: &str, slug: &str) -> Self {
        Self {
            login: login.to_owned(),
            slug: slug.trim().to_owned(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Account {
    pub slug: String,
    pub name: String,
    pub address: String,
    /// From the session (see `jmap`): no filing or replying when true.
    pub read_only: bool,
}

#[derive(Debug, Clone)]
pub struct Thread {
    pub id: ThreadId,
    pub account: String,
    pub subject: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Values {
    pub state: State,
    pub folder: Option<String>,
    /// User logins.
    pub assignees: BTreeSet<String>,
}

#[derive(Debug, Clone)]
pub enum Kind {
    Received {
        from: String,
        addr: String,
        values: Values,
    },
    /// Sent by one of us (`by` is a login, or `None` when the sender is
    /// the account's shared identity). Sent messages carry no values.
    Sent { by: Option<String>, to: Vec<String> },
}

#[derive(Debug, Clone)]
pub struct Message {
    pub id: MessageId,
    /// The RFC 5322 Message-ID, without angle brackets.
    pub message_id: String,
    pub thread: ThreadId,
    pub at: DateTime,
    pub cc: Vec<String>,
    /// Only known for messages we sent.
    pub bcc: Vec<String>,
    /// The text: what lists, search, and quote folding read.
    pub body: String,
    /// An HTML part is stored too, for the thread view to render.
    pub has_html: bool,
    pub kind: Kind,
}

impl Message {
    pub fn values(&self) -> Option<&Values> {
        match &self.kind {
            Kind::Received { values, .. } => Some(values),
            Kind::Sent { .. } => None,
        }
    }
}

#[derive(Debug, Clone)]
pub struct Comment {
    pub id: CommentId,
    pub thread: ThreadId,
    /// A login.
    pub author: String,
    pub at: DateTime,
    pub text: String,
}

/// A change someone made to a message's values, as its toast put it,
/// or one adopted from another client.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Event {
    pub message: MessageId,
    /// A login, or None for a change made in another client.
    pub user: Option<String>,
    pub at: DateTime,
    pub text: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn slugs_are_trimmed() {
        let named = User::new("alex@example.com", " Al ");
        assert_eq!(named.login, "alex@example.com");
        assert_eq!(named.slug, "Al");
    }

    #[test]
    fn state_slugs_round_trip() {
        for state in State::ALL {
            assert_eq!(State::from_slug(state.slug()), Some(state));
            assert_eq!(state.to_string(), state.name());
        }
        assert_eq!(State::from_slug("read"), None);
    }
}