Skip to main content

notify_core/provider/
mod.rs

1mod common;
2mod discord;
3mod file_log;
4mod gotify;
5mod ntfy;
6mod pushover;
7mod slack;
8mod telegram;
9mod webhook;
10
11use std::path::Path;
12
13use reqwest::Client;
14use serde::Serialize;
15
16use crate::{
17    Attachment, NotifyMessage, Result,
18    config::{ChannelConfig, CheckIssue, Config},
19};
20
21use common::path_to_string;
22
23#[derive(Debug, Clone)]
24pub struct SendResult {
25    pub id: String,
26    pub attachments: Vec<StoredAttachment>,
27}
28
29#[derive(Debug, Clone, Serialize)]
30pub struct StoredAttachment {
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub path: Option<String>,
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub field: Option<String>,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub name: Option<String>,
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub original_path: Option<String>,
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub stored_path: Option<String>,
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub mime_type: Option<String>,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub size_bytes: Option<u64>,
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub sha256: Option<String>,
47}
48
49impl StoredAttachment {
50    pub fn dry_run(path: &Path) -> Self {
51        Self {
52            path: Some(path_to_string(path)),
53            field: None,
54            name: None,
55            original_path: None,
56            stored_path: None,
57            mime_type: None,
58            size_bytes: None,
59            sha256: None,
60        }
61    }
62
63    pub(super) fn sent(field: Option<String>, attachment: &Attachment) -> Self {
64        Self {
65            path: None,
66            field,
67            name: Some(attachment.name.clone()),
68            original_path: Some(path_to_string(&attachment.path)),
69            stored_path: None,
70            mime_type: Some(attachment.mime_type.clone()),
71            size_bytes: Some(attachment.size_bytes),
72            sha256: Some(attachment.sha256.clone()),
73        }
74    }
75}
76
77pub async fn send_notification(
78    channel_name: &str,
79    channel: &ChannelConfig,
80    message: &NotifyMessage,
81) -> Result<SendResult> {
82    let client = Client::new();
83    match channel {
84        ChannelConfig::Telegram(config) => {
85            telegram::send(&client, channel_name, config, message).await
86        }
87        ChannelConfig::DiscordWebhook(config) => {
88            discord::send_webhook(&client, channel_name, config, message).await
89        }
90        ChannelConfig::DiscordBot(config) => {
91            discord::send_bot(&client, channel_name, config, message).await
92        }
93        ChannelConfig::Ntfy(config) => ntfy::send(&client, channel_name, config, message).await,
94        ChannelConfig::SlackWebhook(config) => {
95            slack::send_webhook(&client, channel_name, config, message).await
96        }
97        ChannelConfig::Pushover(config) => {
98            pushover::send(&client, channel_name, config, message).await
99        }
100        ChannelConfig::Gotify(config) => gotify::send(&client, channel_name, config, message).await,
101        ChannelConfig::Webhook(config) => {
102            webhook::send(&client, channel_name, config, message).await
103        }
104        ChannelConfig::FileLog(config) => file_log::send(channel_name, config, message),
105    }
106}
107
108pub fn check_file_log_paths(config: &Config) -> Vec<CheckIssue> {
109    file_log::check_paths(config)
110}