Cotton code
71 lines · 2.3 KB · RustRaw
//! The rows a thread's fold says (`email_thread`, `email_participant`). The//! email row itself, its ids and a Gmail message's state patch are//! cotton-rules' `mail::map`, shared with the Worker's own-domain mail.use serde_json::{json, Value};pub use cotton_rules::mail::map::*;use super::tally::{Participant, Thread};/// The `email_thread` row a thread's fold says, whole.pub fn thread_data(thread: &Thread) -> Value {    json!({        "external_key": thread.key,        "subject": thread.subject,        "first_at": thread.first_at,        "last_at": thread.last_at,        "email_count": thread.email_count,    })}pub fn participant_data(thread_id: &str, participant: &Participant) -> Value {    json!({        "thread_id": thread_id,        "email": participant.email,        "display_name": participant.display_name,        "role": participant.role,    })}#[cfg(test)]mod tests {    use super::super::tally::Owned;    use super::*;    use cotton_store::protocol::derived_id::derived_id;    const DB: &str = "00000000-0000-4000-8000-000000000001";    #[test]    fn thread_and_participant_rows() {        let thread = Thread {            key: "imap:root@example.org".into(),            subject: Some("Lunch".into()),            first_at: Some("2026-10-04T10:00:00.000Z".into()),            last_at: Some("2026-10-05T09:30:00.000Z".into()),            email_count: 2,            owned: Owned::Mine,        };        assert_eq!(            thread_data(&thread),            json!({                "external_key": "imap:root@example.org",                "subject": "Lunch",                "first_at": "2026-10-04T10:00:00.000Z",                "last_at": "2026-10-05T09:30:00.000Z",                "email_count": 2            })        );        let ids = Ids::new(DB);        let thread_id = ids.thread(&thread.key);        let person = Participant { email: "ada@example.org".into(), display_name: Some("Ada".into()), role: "from".into() };        assert_eq!(            participant_data(&thread_id, &person),            json!({ "thread_id": thread_id, "email": "ada@example.org", "display_name": "Ada", "role": "from" })        );        assert_eq!(            ids.participant(&thread_id, "ada@example.org"),            derived_id("cotton_email_participants", &[&thread_id, "ada@example.org"])        );    }}