Compare commits

...

3 Commits

Author SHA1 Message Date
483ba390e9 Bump version
All checks were successful
Build and push container image / build (push) Successful in 12m49s
2024-08-02 13:36:42 +10:00
246cb301c4 Allow GPortal RSS to update message 2024-08-02 13:33:46 +10:00
dd008c24b6 Pack logs into one message efficiently 2024-08-02 12:19:53 +10:00
4 changed files with 69 additions and 57 deletions

6
Cargo.lock generated
View File

@ -182,9 +182,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "bytes"
version = "1.7.0"
version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fca2be1d5c43812bae364ee3f30b3afcb7877cf59f4aeb94c66f313a41d2fac9"
checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50"
[[package]]
name = "camino"
@ -1020,7 +1020,7 @@ dependencies = [
[[package]]
name = "kon"
version = "0.3.7"
version = "0.3.8"
dependencies = [
"bb8",
"bb8-postgres",

View File

@ -1,6 +1,6 @@
[package]
name = "kon"
version = "0.3.7"
version = "0.3.8"
edition = "2021"
[dependencies]
@ -15,8 +15,8 @@ poise = "0.6.1"
regex = "1.10.5"
reqwest = { version = "0.12.5", features = ["json"] }
serde = "1.0.204"
serde_json = "1.0.120"
sysinfo = "0.31.0"
serde_json = "1.0.122"
sysinfo = "0.31.2"
tokenservice-client = { version = "0.3.3", registry = "gitea" }
tokio = { version = "1.39.2", features = ["macros", "signal", "rt-multi-thread"] }
tokio-postgres = "0.7.11"

View File

@ -1,6 +1,5 @@
use crate::internals::utils::token_path;
use poise::serenity_prelude::prelude::TypeMapKey;
use bb8_redis::{
bb8::Pool,
redis::cmd,
@ -19,10 +18,6 @@ pub struct RedisController {
pool: Pool<RedisConnectionManager>
}
impl TypeMapKey for RedisController {
type Value = RedisController;
}
impl RedisController {
pub async fn new() -> Result<Self, RedisError> {
let manager = RedisConnectionManager::new(token_path().await.redis_uri.as_str())?;

View File

@ -22,6 +22,7 @@ use std::{
use poise::serenity_prelude::{
Context,
ChannelId,
EditMessage,
CreateMessage,
CreateEmbed,
CreateEmbedAuthor,
@ -47,6 +48,12 @@ async fn get_redis() -> Arc<RedisController> {
REDIS_SERVICE.get().unwrap().clone()
}
// Moved up here as a copy-paste
// This is for building up the embed with the feed data
// std::fs::File::create("rss_name.log").unwrap();
// std::fs::write("rss_name.log", format!("{:#?}", feed))?;
fn format_href_to_discord(input: &str) -> String {
let re = Regex::new(r#"<a href="([^"]+)">([^<]+)</a>"#).unwrap();
re.replace_all(input, r"[$2]($1)").to_string()
@ -126,10 +133,6 @@ async fn esxi_embed() -> Result<Option<CreateEmbed>, Error> {
}
}
// This is for building up the embed with the feed data
// std::fs::File::create("esxi_atom.log").unwrap();
// std::fs::write("esxi_atom.log", format!("{:#?}", feed))?;
let cached_patch = redis.get(&rkey).await.unwrap().unwrap_or_default();
if cached_patch.is_empty() {
@ -169,6 +172,7 @@ async fn esxi_embed() -> Result<Option<CreateEmbed>, Error> {
async fn gportal_embed() -> Result<Option<CreateEmbed>, Error> {
let redis = get_redis().await;
let rkey = "RSS_GPortal";
let rkey_content = format!("{}_Content", rkey);
let url = "https://status.g-portal.com/history.atom";
let res = fetch_feed(url).await?;
@ -189,14 +193,12 @@ async fn gportal_embed() -> Result<Option<CreateEmbed>, Error> {
}
}
// This is for building up the embed with the feed data
// std::fs::File::create("gportal.log").unwrap();
// std::fs::write("gportal.log", format!("{:#?}", feed))?;
let cached_incident = redis.get(&rkey).await.unwrap().unwrap_or_default();
let new_content = format_html_to_discord(article.content.unwrap().body.unwrap());
if cached_incident.is_empty() {
redis.set(&rkey, get_incident_id(&article.links[0].href).unwrap().as_str()).await.unwrap();
redis.set(&rkey, &get_incident_id(&article.links[0].href).unwrap()).await.unwrap();
redis.set(&rkey_content, &new_content).await.unwrap();
if let Err(y) = redis.expire(&rkey, REDIS_EXPIRY_SECS).await {
task_err("RSS", format!("[RedisExpiry]: {}", y).as_str());
}
@ -205,17 +207,30 @@ async fn gportal_embed() -> Result<Option<CreateEmbed>, Error> {
if let Some(incident) = get_incident_id(&article.links[0].href) {
if incident == cached_incident {
return Ok(None);
let cached_content: String = redis.get(&format!("{}_content", rkey)).await.unwrap().unwrap_or_default();
if cached_content == new_content {
return Ok(None);
} else {
redis.set(&rkey_content, &new_content).await.unwrap();
redis.expire(&rkey_content, 21600).await.unwrap();
return Ok(Some(CreateEmbed::new()
.color(0xC23EE8)
.title(article.title.unwrap().content)
.url(incident_page)
.description(new_content)
.timestamp(Timestamp::from(article.updated.unwrap()))
));
}
} else {
save_to_redis(&rkey, &incident).await?;
Ok(Some(CreateEmbed::new()
redis.set(&rkey_content, &new_content).await.unwrap();
return Ok(Some(CreateEmbed::new()
.color(0xC23EE8)
.title(article.title.unwrap().content)
.url(incident_page)
.description(format!("{}", format_html_to_discord(article.content.unwrap().body.unwrap())
))
.timestamp(Timestamp::from(article.updated.unwrap())))
)
.description(new_content)
.timestamp(Timestamp::from(article.updated.unwrap()))
));
}
} else {
task_err("RSS:GPortal", &format!("Incident ID does not match the expected RegEx pattern! ({})", &article.links[0].href));
@ -241,10 +256,6 @@ async fn rust_message() -> Result<Option<String>, Error> {
re.captures(input.as_str()).and_then(|caps| caps.get(1).map(|m| m.as_str().to_string()))
}
// This is for building up the message with the feed data
// std::fs::File::create("rustblog.log").unwrap();
// std::fs::write("rustblog.log", format!("{:#?}", feed))?;
let cached_blog = redis.get(&rkey).await.unwrap().unwrap_or_default();
if cached_blog.is_empty() {
@ -275,43 +286,49 @@ pub async fn rss(ctx: Arc<Context>) -> Result<(), Error> {
loop {
interval.tick().await;
let mut log_msgs: Vec<String> = Vec::new();
match esxi_embed().await {
Ok(Some(embed)) => {
ChannelId::new(BINARY_PROPERTIES.rss_channel).send_message(&ctx.http, CreateMessage::new().add_embed(embed)).await.unwrap();
},
Ok(None) => {
ChannelId::new(BINARY_PROPERTIES.kon_logs).send_message(
&ctx.http, CreateMessage::new()
.content("**[RSS:ESXi]:** Article returned no new content.")
).await.unwrap();
log_msgs.push("**[RSS:ESXi]:** Article returned no new content.".to_string());
},
Err(y) => {
ChannelId::new(BINARY_PROPERTIES.kon_logs).send_message(
&ctx.http, CreateMessage::new()
.content(format!("**[RSS:ESXi:Error]:** Feed failed with the following error:```\n{}\n```", y))
).await.unwrap();
log_msgs.push(format!("**[RSS:ESXi:Error]:** Feed failed with the following error:```\n{}\n```", y));
task_err(&task_name, &y.to_string())
}
}
match gportal_embed().await {
Ok(Some(embed)) => {
ChannelId::new(BINARY_PROPERTIES.rss_channel).send_message(&ctx.http, CreateMessage::new()
.content("*Uh-oh! G-Portal is having issues!*").add_embed(embed)
).await.unwrap();
let redis = get_redis().await;
let rkey = "RSS_GPortal_MsgID";
let channel = ChannelId::new(BINARY_PROPERTIES.rss_channel);
// Check if the message ID is in Redis
if let Ok(Some(msg_id_key)) = redis.get(&rkey).await {
if let Ok(msg_id) = msg_id_key.parse::<u64>() {
// Attempt to edit the message
if let Ok(mut message) = channel.message(&ctx.http, msg_id).await {
message.edit(&ctx.http, EditMessage::new().embed(embed)).await.unwrap();
}
} else {
// If the message is not found or invalid ID, send a new message instead
let message = channel.send_message(&ctx.http, CreateMessage::new()
.content("*Uh-oh! G-Portal is having issues!*").add_embed(embed)
).await.unwrap();
redis.set(&rkey, &message.id.to_string()).await.unwrap();
redis.expire(&rkey, 36000).await.unwrap();
}
}
},
Ok(None) => {
ChannelId::new(BINARY_PROPERTIES.kon_logs).send_message(
&ctx.http, CreateMessage::new()
.content("**[RSS:GPortal]:** Article returned no new content.")
).await.unwrap();
log_msgs.push("**[RSS:GPortal]:** Article returned no new content.".to_string());
},
Err(y) => {
ChannelId::new(BINARY_PROPERTIES.kon_logs).send_message(
&ctx.http, CreateMessage::new()
.content(format!("**[RSS:GPortal:Error]:** Feed failed with the following error:```\n{}\n```", y))
).await.unwrap();
log_msgs.push(format!("**[RSS:GPortal:Error]:** Feed failed with the following error:```\n{}\n```", y));
task_err(&task_name, &y.to_string())
}
}
@ -321,18 +338,18 @@ pub async fn rss(ctx: Arc<Context>) -> Result<(), Error> {
ChannelId::new(BINARY_PROPERTIES.rss_channel).send_message(&ctx.http, CreateMessage::new().content(content)).await.unwrap();
},
Ok(None) => {
ChannelId::new(BINARY_PROPERTIES.kon_logs).send_message(
&ctx.http, CreateMessage::new()
.content("**[RSS:RustBlog]:** Article returned no new content.")
).await.unwrap();
log_msgs.push("**[RSS:RustBlog]:** Article returned no new content.".to_string());
},
Err(y) => {
ChannelId::new(BINARY_PROPERTIES.kon_logs).send_message(
&ctx.http, CreateMessage::new()
.content(format!("**[RSS:RustBlog:Error]:** Feed failed with the following error:```\n{}\n```", y))
).await.unwrap();
log_msgs.push(format!("**[RSS:RustBlog:Error]:** Feed failed with the following error:```\n{}\n```", y));
task_err(&task_name, &y.to_string())
}
}
if !log_msgs.is_empty() {
ChannelId::new(BINARY_PROPERTIES.kon_logs).send_message(
&ctx.http, CreateMessage::new().content(log_msgs.join("\n"))
).await.unwrap();
}
}
}