Add WIP webhook functionality
This commit is contained in:
parent
d14f7f45ce
commit
11fb6d617c
5 changed files with 64 additions and 6 deletions
|
@ -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
Add a link
Reference in a new issue