Improve webhook embeds

main
Elnu 2 years ago
parent 3758a16f8d
commit 072c7c4b8b

@ -130,31 +130,8 @@ async fn _post_comment(
} }
None => return Err(Error::InvalidUrl), // e.g. 404 None => return Err(Error::InvalidUrl), // e.g. 404
}; };
// TODO: Clean up webhook integration // TODO: Use web::block
{ // Create comment in database
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)?; let database = data.get_db(&origin)?;
if comment.author.is_none() && database.settings.name_required { if comment.author.is_none() && database.settings.name_required {
return Err(Error::NameRequired); return Err(Error::NameRequired);
@ -177,8 +154,46 @@ async fn _post_comment(
} }
} }
database.create_comment(&comment)?; database.create_comment(&comment)?;
Ok(()) // Send notification webhook
}).await?.unwrap(); 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)
.field("Link", &format!("{}#{}",
&url // Remove any trailing hash from URL
.split("#")
.next()
.unwrap(),
&database // Get ID of just created comment
.get_comments(&page_data.content_id)
.unwrap()
.first() // returned in reverse chronological order
.unwrap()
.id
.unwrap()
), true)
.field("Content ID", &page_data.content_id, true)
.field("Email", match &comment.email {
Some(email) => email,
None => "None",
}, false)
)
}
).await.unwrap();
}
Ok(()) Ok(())
} }

Loading…
Cancel
Save