Skip to main content

notify_core/provider/
mod.rs

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