Improve webhook embeds
This commit is contained in:
parent
3758a16f8d
commit
072c7c4b8b
1 changed files with 60 additions and 45 deletions
105
src/main.rs
105
src/main.rs
|
@ -130,55 +130,70 @@ async fn _post_comment(
|
|||
}
|
||||
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)
|
||||
)
|
||||
// TODO: Use web::block
|
||||
// Create comment in database
|
||||
let database = data.get_db(&origin)?;
|
||||
if comment.author.is_none() && database.settings.name_required {
|
||||
return Err(Error::NameRequired);
|
||||
}
|
||||
if comment.email.is_none() && database.settings.email_required {
|
||||
return Err(Error::EmailRequired);
|
||||
}
|
||||
if let Some(parent) = comment.parent {
|
||||
'outer2: loop {
|
||||
let comments = database.get_comments(&comment.content_id)?;
|
||||
for other_comment in comments.iter() {
|
||||
if other_comment.id.unwrap() == parent {
|
||||
if other_comment.parent.is_none() {
|
||||
break 'outer2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
).await.unwrap();
|
||||
}
|
||||
return Err(Error::InvalidParent);
|
||||
}
|
||||
}
|
||||
web::block(move || {
|
||||
let database = data.get_db(&origin)?;
|
||||
if comment.author.is_none() && database.settings.name_required {
|
||||
return Err(Error::NameRequired);
|
||||
}
|
||||
if comment.email.is_none() && database.settings.email_required {
|
||||
return Err(Error::EmailRequired);
|
||||
}
|
||||
if let Some(parent) = comment.parent {
|
||||
'outer2: loop {
|
||||
let comments = database.get_comments(&comment.content_id)?;
|
||||
for other_comment in comments.iter() {
|
||||
if other_comment.id.unwrap() == parent {
|
||||
if other_comment.parent.is_none() {
|
||||
break 'outer2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Err(Error::InvalidParent);
|
||||
database.create_comment(&comment)?;
|
||||
// Send notification webhook
|
||||
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)
|
||||
)
|
||||
}
|
||||
}
|
||||
database.create_comment(&comment)?;
|
||||
Ok(())
|
||||
}).await?.unwrap();
|
||||
).await.unwrap();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue