aboutsummaryrefslogtreecommitdiff
path: root/src/mail/user.rs
blob: f1d04776ae7d46872faddc812119185cfc2f6256 (plain) (blame)
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
use std::collections::HashMap;
use std::sync::{Arc, Weak};

use anyhow::Result;
use lazy_static::lazy_static;

use k2v_client::K2vClient;
use rusoto_s3::S3Client;

use crate::login::{Credentials, StorageCredentials};
use crate::mail::mailbox::Mailbox;
use crate::mail::unique_ident::UniqueIdent;

pub struct User {
    pub username: String,
    pub creds: Credentials,
    pub s3_client: S3Client,
    pub k2v_client: K2vClient,
}

impl User {
    pub fn new(username: String, creds: Credentials) -> Result<Self> {
        let s3_client = creds.s3_client()?;
        let k2v_client = creds.k2v_client()?;
        Ok(Self {
            username,
            creds,
            s3_client,
            k2v_client,
        })
    }

    /// Lists user's available mailboxes
    pub fn list_mailboxes(&self) -> Result<Vec<String>> {
        unimplemented!()
    }

    /// Opens an existing mailbox given its IMAP name.
    pub async fn open_mailbox(&self, name: &str) -> Result<Option<Arc<Mailbox>>> {
        // TODO: handle mailbox names, mappings, renaming, etc
        let id = match name {
            "INBOX" => UniqueIdent([0u8; 24]),
            _ => panic!("Only INBOX exists for now"),
        };

        let cache_key = (self.creds.storage.clone(), id);

        {
            let cache = MAILBOX_CACHE.cache.lock().unwrap();
            if let Some(mb) = cache.get(&cache_key).and_then(Weak::upgrade) {
                return Ok(Some(mb));
            }
        }

        let mb = Arc::new(Mailbox::open(&self.creds, id).await?);

        let mut cache = MAILBOX_CACHE.cache.lock().unwrap();
        if let Some(concurrent_mb) = cache.get(&cache_key).and_then(Weak::upgrade) {
            drop(mb); // we worked for nothing but at least we didn't starve someone else
            Ok(Some(concurrent_mb))
        } else {
            cache.insert(cache_key, Arc::downgrade(&mb));
            Ok(Some(mb))
        }
    }

    /// Creates a new mailbox in the user's IMAP namespace.
    pub fn create_mailbox(&self, _name: &str) -> Result<()> {
        unimplemented!()
    }

    /// Deletes a mailbox in the user's IMAP namespace.
    pub fn delete_mailbox(&self, _name: &str) -> Result<()> {
        unimplemented!()
    }

    /// Renames a mailbox in the user's IMAP namespace.
    pub fn rename_mailbox(&self, _old_name: &str, _new_name: &str) -> Result<()> {
        unimplemented!()
    }
}

// ---- Mailbox cache ----

struct MailboxCache {
    cache: std::sync::Mutex<HashMap<(StorageCredentials, UniqueIdent), Weak<Mailbox>>>,
}

impl MailboxCache {
    fn new() -> Self {
        Self {
            cache: std::sync::Mutex::new(HashMap::new()),
        }
    }
}

lazy_static! {
    static ref MAILBOX_CACHE: MailboxCache = MailboxCache::new();
}