Add WIP webhook functionality
This commit is contained in:
parent
d14f7f45ce
commit
11fb6d617c
5 changed files with 64 additions and 6 deletions
27
Cargo.lock
generated
27
Cargo.lock
generated
|
@ -360,9 +360,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.73"
|
||||
version = "1.0.79"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
|
||||
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
|
||||
dependencies = [
|
||||
"jobserver",
|
||||
]
|
||||
|
@ -1311,6 +1311,15 @@ version = "0.1.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "openssl-sys"
|
||||
version = "0.9.75"
|
||||
|
@ -1320,6 +1329,7 @@ dependencies = [
|
|||
"autocfg",
|
||||
"cc",
|
||||
"libc",
|
||||
"openssl-src",
|
||||
"pkg-config",
|
||||
"vcpkg",
|
||||
]
|
||||
|
@ -1995,6 +2005,7 @@ dependencies = [
|
|||
"serde_json",
|
||||
"serde_yaml",
|
||||
"validator",
|
||||
"webhook",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -2428,6 +2439,18 @@ dependencies = [
|
|||
"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]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
|
|
|
@ -30,3 +30,4 @@ sanitize_html = "0.7"
|
|||
clap = { version = "4.2", features = ["derive"] }
|
||||
serde_yaml = "0.9"
|
||||
derive_more = "0.99"
|
||||
webhook = "2.1"
|
||||
|
|
|
@ -30,12 +30,19 @@ pub struct 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>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
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(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ pub struct DatabaseSettings {
|
|||
pub name_required: bool,
|
||||
pub email_required: bool,
|
||||
pub file: Option<String>,
|
||||
pub webhook: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(From, Debug)]
|
||||
|
|
32
src/main.rs
32
src/main.rs
|
@ -17,6 +17,7 @@ use std::fs::File;
|
|||
use std::sync::Mutex;
|
||||
use std::{collections::HashMap, sync::MutexGuard};
|
||||
use validator::Validate;
|
||||
use webhook::client::WebhookClient;
|
||||
|
||||
struct AppState {
|
||||
databases: HashMap<String, Mutex<Database>>,
|
||||
|
@ -120,14 +121,39 @@ async fn _post_comment(
|
|||
}
|
||||
return Err(Error::InvalidUrl);
|
||||
}
|
||||
match get_page_data(&url).await? {
|
||||
let page_data = match get_page_data(&url).await? {
|
||||
Some(page_data) => {
|
||||
if page_data.content_id != comment.content_id {
|
||||
return Err(Error::InvalidContentId);
|
||||
}
|
||||
page_data
|
||||
}
|
||||
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 || {
|
||||
let database = data.get_db(&origin)?;
|
||||
if comment.author.is_none() && database.settings.name_required {
|
||||
|
@ -152,8 +178,8 @@ async fn _post_comment(
|
|||
}
|
||||
database.create_comment(&comment)?;
|
||||
Ok(())
|
||||
})
|
||||
.await?
|
||||
}).await?.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[post("/")]
|
||||
|
|
Loading…
Add table
Reference in a new issue