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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use super::{EventType, WebhookBatch};
use crate::utils::{get_timestamp::get_timestamp, sify::sify};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Clone)]
#[doc(hidden)]
pub struct DiscordEmbedField {
    pub name: String,
    pub value: String,
}

#[derive(Serialize, Clone)]
#[doc(hidden)]
pub struct DiscordEmbed {
    pub color: i32,
    pub timestamp: String,
    pub fields: Vec<DiscordEmbedField>,
    pub title: String,
}

#[derive(Serialize, Clone)]
#[doc(hidden)]
pub struct DiscordEmbedContent {
    pub username: String,
    pub avatar_url: String,
    pub embeds: Vec<DiscordEmbed>,
}

#[derive(Deserialize, Clone)]
pub struct DiscordWebhook {
    /// Webhook URL
    pub url: String,
    /// Optional avatar URL (default [assets/logo.webp](https://raw.githubusercontent.com/dan-online/autopulse/main/assets/logo.webp))
    pub avatar_url: Option<String>,
    /// Optional username (default: autopulse)
    pub username: Option<String>,
}

impl DiscordWebhook {
    fn get_client(&self) -> reqwest::Client {
        reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(10))
            .build()
            .expect("failed to build reqwest client")
    }

    fn generate_json(&self, batch: &WebhookBatch) -> DiscordEmbedContent {
        let mut content = DiscordEmbedContent {
            username: self
                .username
                .clone()
                .unwrap_or_else(|| "autopulse".to_string()),
            avatar_url: self.avatar_url.clone().unwrap_or_else(|| {
                "https://raw.githubusercontent.com/dan-online/autopulse/main/assets/logo.webp"
                    .to_string()
            }),
            embeds: vec![],
        };

        for (event, trigger, files) in batch {
            let color = match event {
                EventType::New => 6_061_450,    // grey
                EventType::Found => 52084,      // green
                EventType::Error => 16_711_680, // red
                EventType::Processed => 39129,  // blue
                EventType::Retrying | EventType::HashMismatch => 16_776_960,
            };

            let title = trigger.clone().map_or_else(
                || {
                    format!(
                        "[{}] - {} file{} {}",
                        event,
                        files.len(),
                        sify(files),
                        event.action()
                    )
                },
                |trigger| {
                    format!(
                        "[{}] - [{}] - {} file{} {}",
                        event,
                        trigger,
                        files.len(),
                        sify(files),
                        event.action()
                    )
                },
            );

            let fields = vec![
                DiscordEmbedField {
                    name: "Timestamp".to_string(),
                    value: get_timestamp(),
                },
                DiscordEmbedField {
                    name: "Files".to_string(),
                    value: files.join("\n"),
                },
            ];

            let embed = DiscordEmbed {
                color,
                timestamp: chrono::Utc::now().to_rfc3339(),
                fields,
                title,
            };

            content.embeds.push(embed);
        }

        content
    }

    pub async fn send(&self, batch: &WebhookBatch) -> anyhow::Result<()> {
        let mut message_queue = vec![];

        for chunk in batch.chunks(10) {
            let content = self.generate_json(&chunk.to_vec());
            message_queue.push(content);
        }

        for message in message_queue {
            let res = self
                .get_client()
                .post(&self.url)
                .json(&message)
                .send()
                .await
                .map_err(|e| anyhow::anyhow!(e))?;

            if !res.status().is_success() {
                let body = res.text().await.unwrap_or_else(|_| "no body".to_string());
                return Err(anyhow::anyhow!("failed to send webhook: {}", body));
            }
        }

        Ok(())
    }
}