Skip to main content

nexus_common/config/
watcher.rs

1use super::file::ConfigLoader;
2use super::{default_stack, DaemonConfig, StackConfig};
3use async_trait::async_trait;
4use pubky_app_specs::PubkyId;
5use serde::{Deserialize, Serialize};
6use std::fmt::Debug;
7
8pub const NAME: &str = "nexus.watcher";
9
10pub const TESTNET: bool = false;
11// Testnet homeserver key
12pub const HOMESERVER_PUBKY: &str = "8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo";
13// Maximum number of events to fetch at once from a homeserver
14pub const EVENTS_LIMIT: u32 = 1000;
15// Sleep between checks to homeserver
16pub const WATCHER_SLEEP: u64 = 5000;
17// Moderation service key
18pub const MODERATION_ID: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
19// Moderation service key
20pub const MODERATED_TAGS: [&str; 6] = [
21    "hatespeech",
22    "harassement",
23    "terrorism",
24    "violence",
25    "illegal_activities",
26    "il_adult_nu_sex_act",
27];
28
29/// Configuration settings for the Nexus Watcher service
30#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
31pub struct WatcherConfig {
32    pub name: String,
33    pub testnet: bool,
34    pub homeserver: PubkyId,
35    pub events_limit: u32,
36    pub watcher_sleep: u64,
37    #[serde(default = "default_stack")]
38    pub stack: StackConfig,
39    // Moderation
40    pub moderation_id: PubkyId,
41    pub moderated_tags: Vec<String>,
42}
43
44impl Default for WatcherConfig {
45    /// The default values are derived from predefined constants
46    /// This implementation is not secure as it may panic if the homeserver
47    /// identifier fails to parse, but it ensures a valid initial state
48    fn default() -> Self {
49        let homeserver = PubkyId::try_from(HOMESERVER_PUBKY)
50            .expect("Hardcoded homeserver should be a valid pubky id");
51        let moderation_id = PubkyId::try_from(MODERATION_ID)
52            .expect("Hardcoded moderation should be a valid pubky id");
53        Self {
54            name: String::from(NAME),
55            stack: StackConfig::default(),
56            testnet: TESTNET,
57            homeserver,
58            events_limit: EVENTS_LIMIT,
59            watcher_sleep: WATCHER_SLEEP,
60            moderation_id,
61            moderated_tags: MODERATED_TAGS.iter().map(|s| s.to_string()).collect(),
62        }
63    }
64}
65
66/// Converts a [`DaemonConfig`] into an [`WatcherConfig`], extracting only the Watcher-related settings
67/// and the shared application stack
68impl From<DaemonConfig> for WatcherConfig {
69    fn from(daemon_config: DaemonConfig) -> Self {
70        WatcherConfig {
71            name: daemon_config.watcher.name,
72            testnet: daemon_config.watcher.testnet,
73            homeserver: daemon_config.watcher.homeserver,
74            events_limit: daemon_config.watcher.events_limit,
75            watcher_sleep: daemon_config.watcher.watcher_sleep,
76            stack: daemon_config.stack,
77            moderation_id: daemon_config.watcher.moderation_id,
78            moderated_tags: daemon_config.watcher.moderated_tags,
79        }
80    }
81}
82
83#[async_trait]
84impl ConfigLoader<WatcherConfig> for WatcherConfig {}