Add WIP webhook functionality

main
Elnu 2 years ago
parent d14f7f45ce
commit 11fb6d617c

27
Cargo.lock generated

@ -360,9 +360,9 @@ dependencies = [
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.73" version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
dependencies = [ dependencies = [
"jobserver", "jobserver",
] ]
@ -1311,6 +1311,15 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-src"
version = "111.25.2+1.1.1t"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320708a054ad9b3bf314688b5db87cf4d6683d64cfc835e2337924ae62bf4431"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "openssl-sys" name = "openssl-sys"
version = "0.9.75" version = "0.9.75"
@ -1320,6 +1329,7 @@ dependencies = [
"autocfg", "autocfg",
"cc", "cc",
"libc", "libc",
"openssl-src",
"pkg-config", "pkg-config",
"vcpkg", "vcpkg",
] ]
@ -1995,6 +2005,7 @@ dependencies = [
"serde_json", "serde_json",
"serde_yaml", "serde_yaml",
"validator", "validator",
"webhook",
] ]
[[package]] [[package]]
@ -2428,6 +2439,18 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "webhook"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09d801ea0225da29d32c85b21d0cb12ed628783f5fa1fbe226e586a3ef6ca96f"
dependencies = [
"hyper",
"hyper-tls",
"serde",
"serde_json",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

@ -30,3 +30,4 @@ sanitize_html = "0.7"
clap = { version = "4.2", features = ["derive"] } clap = { version = "4.2", features = ["derive"] }
serde_yaml = "0.9" serde_yaml = "0.9"
derive_more = "0.99" derive_more = "0.99"
webhook = "2.1"

@ -30,12 +30,19 @@ pub struct Comment {
pub replies: Vec<Comment>, pub replies: Vec<Comment>,
} }
pub fn get_gravatar(email: &Option<String>) -> String {
match email {
Some(email) => format!("{:x}", md5::compute(email.to_lowercase())),
None => String::from(""),
}
}
fn serialize_gravatar<S>(email: &Option<String>, s: S) -> Result<S::Ok, S::Error> fn serialize_gravatar<S>(email: &Option<String>, s: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
{ {
match email { match email {
Some(email) => s.serialize_some(&format!("{:x}", md5::compute(email.to_lowercase()))), Some(_) => s.serialize_some(&get_gravatar(email)),
None => s.serialize_none(), None => s.serialize_none(),
} }
} }

@ -17,6 +17,7 @@ pub struct DatabaseSettings {
pub name_required: bool, pub name_required: bool,
pub email_required: bool, pub email_required: bool,
pub file: Option<String>, pub file: Option<String>,
pub webhook: Option<String>,
} }
#[derive(From, Debug)] #[derive(From, Debug)]

@ -17,6 +17,7 @@ use std::fs::File;
use std::sync::Mutex; use std::sync::Mutex;
use std::{collections::HashMap, sync::MutexGuard}; use std::{collections::HashMap, sync::MutexGuard};
use validator::Validate; use validator::Validate;
use webhook::client::WebhookClient;
struct AppState { struct AppState {
databases: HashMap<String, Mutex<Database>>, databases: HashMap<String, Mutex<Database>>,
@ -120,14 +121,39 @@ async fn _post_comment(
} }
return Err(Error::InvalidUrl); return Err(Error::InvalidUrl);
} }
match get_page_data(&url).await? { let page_data = match get_page_data(&url).await? {
Some(page_data) => { Some(page_data) => {
if page_data.content_id != comment.content_id { if page_data.content_id != comment.content_id {
return Err(Error::InvalidContentId); return Err(Error::InvalidContentId);
} }
page_data
} }
None => return Err(Error::InvalidUrl), // e.g. 404 None => return Err(Error::InvalidUrl), // e.g. 404
}; };
// TODO: Clean up webhook integration
{
let database = data.get_db(&origin)?;
if let Some(webhook) = &database.settings.webhook {
let client = WebhookClient::new(&webhook);
client.send(|message| {
let author = match &comment.author {
Some(author) => &author,
None => "Annonymous",
};
message
.username(&author)
.avatar_url(&format!(
"https://www.gravatar.com/avatar/{}?d=mp",
get_gravatar(&comment.email)
))
.embed(|embed| embed
.title(&format!("New comment on {}", page_data.content_id))
.description(&comment.text)
)
}
).await.unwrap();
}
}
web::block(move || { web::block(move || {
let database = data.get_db(&origin)?; let database = data.get_db(&origin)?;
if comment.author.is_none() && database.settings.name_required { if comment.author.is_none() && database.settings.name_required {
@ -152,8 +178,8 @@ async fn _post_comment(
} }
database.create_comment(&comment)?; database.create_comment(&comment)?;
Ok(()) Ok(())
}) }).await?.unwrap();
.await? Ok(())
} }
#[post("/")] #[post("/")]

Loading…
Cancel
Save