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
pub mod config;

use async_trait::async_trait;
use log::info;
use std::sync::Arc;

#[cfg(feature = "message-send")]
use crate::message::send::{sendmail::SendSendmailMessage, SendMessage};
use crate::{
    account::config::AccountConfig,
    backend::{BackendContext, BackendContextBuilder, BackendFeatureBuilder},
    Result,
};

use self::config::SendmailConfig;

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct SendmailContext {
    pub account_config: Arc<AccountConfig>,
    pub sendmail_config: Arc<SendmailConfig>,
}

impl SendmailContext {
    pub fn new(account_config: Arc<AccountConfig>, sendmail_config: Arc<SendmailConfig>) -> Self {
        Self {
            account_config,
            sendmail_config,
        }
    }
}

pub type SendmailContextSync = SendmailContext;

impl BackendContext for SendmailContextSync {}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct SendmailContextBuilder {
    /// The sendmail configuration
    pub sendmail_config: Arc<SendmailConfig>,
}

impl SendmailContextBuilder {
    pub fn new(sendmail_config: Arc<SendmailConfig>) -> Self {
        Self { sendmail_config }
    }
}

#[async_trait]
impl BackendContextBuilder for SendmailContextBuilder {
    type Context = SendmailContextSync;

    #[cfg(feature = "message-send")]
    fn send_message(&self) -> BackendFeatureBuilder<Self::Context, dyn SendMessage> {
        Some(Arc::new(SendSendmailMessage::some_new_boxed))
    }

    /// Build an SENDMAIL sync session.
    ///
    /// The SENDMAIL session is created at this moment. If the session
    /// cannot be created using the OAuth 2.0 authentication, the
    /// access token is refreshed first then a new session is created.
    async fn build(self, account_config: Arc<AccountConfig>) -> Result<Self::Context> {
        info!("building new sendmail context");

        Ok(SendmailContextSync {
            account_config,
            sendmail_config: self.sendmail_config,
        })
    }
}