Skip to content

Commit

Permalink
Adds try_post for Discord
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed May 26, 2024
1 parent 7a1192a commit 74960df
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 38 deletions.
21 changes: 17 additions & 4 deletions src/integrations/discord/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use crate::integrations::webhook::Webhook;
/// # Example
/// ```rust
///
/// use busser::integrations::{webhook::Webhook, discord::post::post};
/// use busser::integrations::{webhook::Webhook, discord::post::post_message};
///
/// pub async fn post_to_discord(){
/// let w = Webhook::new("https://discord.com/api/webhooks/xxx/yyy".to_string());
/// post(&w, "this is some plaintext".to_string());
/// post_message(&w, &"this is some plaintext".to_string());
/// }
/// ```
///
Expand All @@ -34,9 +34,9 @@ use crate::integrations::webhook::Webhook;
/// {"content": "this is some plaintext"}
/// ```
pub async fn post(w: &Webhook, msg: String) -> Result<String, reqwest::Error>
/// Post a text message to a discord Webhook
pub async fn post_message(w: &Webhook, msg: &String) -> Result<String, reqwest::Error>
{

crate::debug(format!("Posting to Discord {:?}", msg), None);
let client = reqwest::Client::new();

Expand All @@ -51,5 +51,18 @@ pub async fn post(w: &Webhook, msg: String) -> Result<String, reqwest::Error>
Ok(r) => Ok(format!("OK\nGot response:\n\n{:#?}", r)),
Err(e) => Err(e)
}
}

/// Attempt to post a message to the discord webhook
pub async fn try_post(webhook: Option<Webhook>, msg: &String)
{
match webhook
{
Some(w) => match post_message(&w, msg).await
{
Ok(_s) => (),
Err(e) => {crate::debug(format!("Error posting to discord\n{}", e), None);}
},
None => {crate::debug(format!("Discord webhook is None"), None);}
}
}
19 changes: 7 additions & 12 deletions src/server/api/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use reqwest::StatusCode;
use serde::Deserialize;
use tokio::sync::Mutex;

use crate::{config::{read_config, CONFIG_PATH}, server::stats::{digest::{digest_message, process_hits}, hits::HitStats}, integrations::{discord::post::post, is_authentic}};
use crate::{config::{read_config, CONFIG_PATH}, integrations::{discord::post::try_post, is_authentic}, server::stats::{digest::{digest_message, process_hits}, hits::HitStats}};

use super::ApiRequest;

Expand Down Expand Up @@ -145,20 +145,15 @@ impl ApiRequest for StatsDigest
None => None
};

let digest = process_hits(config.stats.path, from,to,config.stats.top_n_digest,stats);
let msg = digest_message(digest, from, to);
let msg = digest_message(process_hits(config.stats.path, from,to,config.stats.top_n_digest,stats), from, to);

if self.payload.post_discord
{
match config.notification_endpoint
{
Some(endpoint) => match post(&endpoint, msg.clone()).await
{
Ok(_s) => (),
Err(e) => {crate::debug(format!("Error posting to discord\n{}", e), None);}
},
None => ()
}
try_post
(
config.notification_endpoint,
&msg
).await;
}

(Some(msg), StatusCode::OK)
Expand Down
26 changes: 7 additions & 19 deletions src/server/stats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use chrono::{DateTime, Utc};
use cron::Schedule;
use tokio::sync::Mutex;

use crate::{config::{read_config, Config, CONFIG_PATH}, filesystem::file::File, integrations::discord::post::post, task::{next_job_time, schedule_from_option, Task}};
use crate::{config::{Config, CONFIG_PATH}, filesystem::file::File, integrations::discord::post::try_post, task::{next_job_time, schedule_from_option, Task}};

use self::{digest::{digest_message, process_hits}, file::StatsFile, hits::HitStats};

Expand Down Expand Up @@ -132,14 +132,7 @@ impl Task for StatsDigestTask
{
let mut stats = self.state.lock().await;

let config = match read_config(CONFIG_PATH)
{
Some(c) => c,
None =>
{
Config::default()
}
};
let config = Config::load_or_default(CONFIG_PATH);

stats.summary = process_hits
(
Expand All @@ -150,16 +143,11 @@ impl Task for StatsDigestTask
Some(stats.to_owned())
);

let msg = digest_message(stats.summary.clone(), Some(self.last_run), None);
match config.notification_endpoint
{
Some(endpoint) => match post(&endpoint, msg).await
{
Ok(_s) => (),
Err(e) => {crate::debug(format!("Error posting to discord\n{}", e), None);}
},
None => ()
}
try_post
(
config.notification_endpoint,
&digest_message(stats.summary.clone(), Some(self.last_run), None)
).await;
}

let config = Config::load_or_default(CONFIG_PATH);
Expand Down
6 changes: 3 additions & 3 deletions tests/test_discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod common;
#[cfg(test)]
mod discord
{
use busser::integrations::{discord::post::post, webhook::Webhook};
use busser::integrations::{discord::post::post_message, webhook::Webhook};

#[tokio::test]
async fn test_webhook()
Expand All @@ -12,13 +12,13 @@ mod discord

assert_eq!(w.get_addr(), "https://discord.com/api/webhooks/xxx/yyy");

assert!(post(&w, "400".to_string()).await.is_ok());
assert!(post_message(&w, &"400".to_string()).await.is_ok());
}

#[tokio::test]
async fn test_err_webhook()
{
let w = Webhook::new("not_a_domain".to_string());
assert!(post(&w, "400".to_string()).await.is_err());
assert!(post_message(&w, &"400".to_string()).await.is_err());
}
}

0 comments on commit 74960df

Please sign in to comment.