aboutsummaryrefslogtreecommitdiff
path: root/src/notify.rs
blob: 19915db737fce6299b88c3ac8ce84c50f1dd29b1 (plain) (blame)
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
use crate::database::FeedConf;
use crate::rss_utils;

use log::{info, error};
use feed_rs::model::Feed;
use chrono::prelude::{Utc,DateTime};

/**
 * Push feed entry to gotify
 */
async fn gotify(title: String, message: String, link: Option<String>, feed_conf: &FeedConf) -> Result<(), reqwest::Error>  {
    let uri = format!("{}/message", &feed_conf.push_url);

    // Build json string that will be sent as payload to gotify
    let mut req = "{".to_owned();

    req.push_str(format!("\"title\":\"{}\"", title).as_str());
    req.push_str(format!(",\"message\":\"{}\"", message).as_str());
    req.push_str(",\"priority\":1");
    
    req.push_str(",\"extras\": {");
    req.push_str("\"client::display\": { \"contentType\": \"text/markdown\" }");
    if link.is_some() {
        req.push_str(",\"client::notification\": { \"click\": { \"url\": \"");
        req.push_str(link.unwrap().as_str());
        req.push_str("\"}}")
    }
    req.push_str("}}");

    // Send request to gotify
    let client = reqwest::Client::new();
    let res = client.post(uri)
                    .query(&[("token",&feed_conf.push_token)])
                    .body(req.to_owned())
                    .header("Content-Type", "application/json")
                    .send()
                    .await?;
    if res.status().is_success() {
        info!("Sent notification with title \"{}\"", title);
    } else {
        error!("payload: {}", req);
        error!("Could not send notification... {:#?}", res);
    }
    Ok(())
}

/**
 * Push all new entries in the feed as per the configuration
 */
pub async fn all(feed: &Feed, feed_conf: &FeedConf, last_fetch_time: DateTime<Utc>) -> bool {
    let mut all_notifs_successfull = true;

    // Skip sending notification if the publish time is before the
    // last_fetch_time
    for entry in &feed.entries {
        if let Some(x) = entry.published {
            if last_fetch_time > x {
                info!("Skipping entry that was published at {}", x);
                continue;
            }
        }

        // Get the fields we want to send to gotify
        let title = rss_utils::fill_template(&feed_conf.title, &entry, &feed);
        let message = rss_utils::fill_template(&feed_conf.message, &entry, &feed);
        let mut link: Option<String> = None;
        if entry.links.len() > 0 {
            link = Some(rss_utils::escape(entry.links[0].href.to_owned()));
        }

        if let Err(e) = gotify(title, message, link, &feed_conf).await {
            error!("Could not send push notification ({:#?})", e);
            all_notifs_successfull = false;
        }
    }

    return all_notifs_successfull;
}