Skip to main content

server_watchdog/infrastructure/config/
event.rs

1use crate::application::config::{EventConfigUseCase, EventSubscribeUseCase};
2use crate::domain::config::{Config, EventConfig, EventSubscribe, EventSubscribeList};
3use crate::domain::file_accessor::FileAccessor;
4use async_trait::async_trait;
5use derive_new::new;
6use std::error::Error;
7use std::sync::Arc;
8
9#[derive(new)]
10pub struct EventConfigAdapter {
11    config_file_accessor: Arc<dyn FileAccessor<Config>>,
12    subscribe_file_accessor: Arc<dyn FileAccessor<EventSubscribeList>>,
13}
14
15#[async_trait]
16impl EventConfigUseCase for EventConfigAdapter {
17    async fn add_event(
18        &self,
19        event_config: EventConfig,
20    ) -> Result<(), Box<dyn Error + Send + Sync>> {
21        let mut config = self.config_file_accessor.read().await?;
22        config.events.push(event_config);
23        self.config_file_accessor.write(&config).await?;
24        Ok(())
25    }
26
27    async fn list_event(&self) -> Result<Vec<EventConfig>, Box<dyn Error + Send + Sync>> {
28        let config = self.config_file_accessor.read().await?;
29        Ok(config.events)
30    }
31
32    async fn remove_event(&self, name: String) -> Result<(), Box<dyn Error + Send + Sync>> {
33        let mut config = self.config_file_accessor.read().await?;
34        config.events.retain(|event| event.name != name);
35        self.config_file_accessor.write(&config).await?;
36        Ok(())
37    }
38}
39
40#[async_trait]
41impl EventSubscribeUseCase for EventConfigAdapter {
42    async fn subscribe(
43        &self,
44        chat_id: String,
45        event_name: String,
46    ) -> Result<(), Box<dyn Error + Send + Sync>> {
47        // Validate that the event exists in the config
48        let config = self.config_file_accessor.read().await?;
49        let event_exists = config.events.iter().any(|e| e.name == event_name);
50        if !event_exists {
51            return Err(format!("Event '{}' does not exist in configuration", event_name).into());
52        }
53
54        let mut subscribe_file: EventSubscribeList = self.subscribe_file_accessor.read().await?;
55
56        if subscribe_file.contains(event_name.as_str(), chat_id.as_str()) {
57            return Ok(());
58        }
59
60        match subscribe_file.find_subscribe_mut(event_name.as_str()) {
61            Some(subscribe) => {
62                subscribe.chat_ids.push(chat_id);
63            }
64            None => {
65                let mut chat_ids = Vec::new();
66                chat_ids.push(chat_id);
67                subscribe_file.subscribes.push(EventSubscribe {
68                    event_name,
69                    chat_ids,
70                })
71            }
72        }
73
74        let _ = self.subscribe_file_accessor.write(&subscribe_file).await?;
75        Ok(())
76    }
77
78    async fn list_subscribed_event(
79        &self,
80        chat_id: String,
81    ) -> Result<Vec<EventConfig>, Box<dyn Error + Send + Sync>> {
82        let subscribe_file: EventSubscribeList = self.subscribe_file_accessor.read().await?;
83        let event_names = subscribe_file.find_subscribed_events(chat_id.as_str());
84        let config = self.config_file_accessor.read().await?;
85
86        let event_configs = config
87            .events
88            .into_iter()
89            .filter(|event_config| event_names.contains(&event_config.name.as_str()))
90            .collect();
91
92        Ok(event_configs)
93    }
94
95    async fn unsubscribe(
96        &self,
97        chat_id: String,
98        event_name: String,
99    ) -> Result<(), Box<dyn Error + Send + Sync>> {
100        let mut subscribe_file: EventSubscribeList = self.subscribe_file_accessor.read().await?;
101        subscribe_file.unsubscribe(event_name.as_str(), chat_id.as_str());
102        let _ = self.subscribe_file_accessor.write(&subscribe_file).await?;
103
104        Ok(())
105    }
106}