Skip to main content

plexus_comms/
builder.rs

1use crate::activations::*;
2use crate::config::CommsConfig;
3use plexus_core::DynamicHub;
4use std::sync::Arc;
5
6/// Build the communications hub with all enabled activations
7pub async fn build_comms_hub(config: CommsConfig) -> Result<Arc<DynamicHub>, String> {
8    let mut hub = DynamicHub::new("comms");
9
10    // Register email activation (always available, uses runtime account registration)
11    let email = Email::new()
12        .await
13        .map_err(|e| format!("Failed to initialize email activation: {}", e))?;
14    hub = hub.register(email);
15    tracing::info!("Email activation registered (multi-account mode)");
16
17    // Register Discord activation (always available, uses runtime account registration)
18    let discord = Discord::new()
19        .await
20        .map_err(|e| format!("Failed to initialize Discord activation: {}", e))?;
21    hub = hub.register(discord);
22    tracing::info!("Discord activation registered (multi-account mode)");
23
24    // Register SMS activation if configured
25    if let Some(sms_config) = config.sms {
26        let sms = Sms::new(sms_config)
27            .await
28            .map_err(|e| format!("Failed to initialize SMS activation: {}", e))?;
29        hub = hub.register(sms);
30        tracing::info!("SMS activation registered");
31    }
32
33    // Register push notifications if configured
34    if let Some(push_config) = config.push {
35        let push = Push::new(push_config)
36            .await
37            .map_err(|e| format!("Failed to initialize push activation: {}", e))?;
38        hub = hub.register(push);
39        tracing::info!("Push activation registered");
40    }
41
42    // Register Telegram if configured
43    if let Some(telegram_config) = config.telegram {
44        let telegram = Telegram::new(telegram_config)
45            .await
46            .map_err(|e| format!("Failed to initialize Telegram activation: {}", e))?;
47        hub = hub.register(telegram);
48        tracing::info!("Telegram activation registered");
49    }
50
51    // Register WhatsApp if configured
52    if let Some(whatsapp_config) = config.whatsapp {
53        let whatsapp = Whatsapp::new(whatsapp_config)
54            .await
55            .map_err(|e| format!("Failed to initialize WhatsApp activation: {}", e))?;
56        hub = hub.register(whatsapp);
57        tracing::info!("WhatsApp activation registered");
58    }
59
60    // Register Slack if configured
61    if let Some(slack_config) = config.slack {
62        let slack = Slack::new(slack_config)
63            .await
64            .map_err(|e| format!("Failed to initialize Slack activation: {}", e))?;
65        hub = hub.register(slack);
66        tracing::info!("Slack activation registered");
67    }
68
69    Ok(Arc::new(hub))
70}
71
72/// Build a default hub with minimal configuration (SMTP email only)
73pub async fn build_default_hub() -> Result<Arc<DynamicHub>, String> {
74    build_comms_hub(CommsConfig::default()).await
75}
76
77/// Load configuration from a TOML file and build the hub
78pub async fn build_from_config_file(path: &str) -> Result<Arc<DynamicHub>, String> {
79    let config_str = std::fs::read_to_string(path)
80        .map_err(|e| format!("Failed to read config file: {}", e))?;
81
82    let config: CommsConfig = toml::from_str(&config_str)
83        .map_err(|e| format!("Failed to parse config file: {}", e))?;
84
85    build_comms_hub(config).await
86}