improve webmention code, announce webmentions on Discord

This commit is contained in:
Cadey Ratio 2020-11-04 12:31:10 -05:00
parent 9bbfcf74c8
commit 19ab543438
2 changed files with 18 additions and 4 deletions

View File

@ -1,10 +1,10 @@
use super::{Error, Result}; use super::{Error, Result};
use crate::{models, schema, MainDatabase}; use crate::{models, schema, web::discord_webhook::Client as DiscordWebhook, MainDatabase};
use diesel::prelude::*; use diesel::prelude::*;
use rocket::{ use rocket::{
request::Form, request::Form,
response::{self, Responder}, response::{self, Responder},
Request, Response, Request, Response, State,
}; };
use rocket_contrib::json::Json; use rocket_contrib::json::Json;
use rusty_ulid::generate_ulid_string; use rusty_ulid::generate_ulid_string;
@ -71,8 +71,12 @@ impl<'a> Responder<'a> for models::WebMention {
} }
#[post("/webmention/accept", data = "<mention>")] #[post("/webmention/accept", data = "<mention>")]
#[instrument(skip(conn, mention), err)] #[instrument(skip(conn, mention, dw), err)]
pub fn accept(conn: MainDatabase, mention: Form<WebMention>) -> Result<models::WebMention> { pub fn accept(
conn: MainDatabase,
mention: Form<WebMention>,
dw: State<DiscordWebhook>,
) -> Result<models::WebMention> {
use schema::webmentions; use schema::webmentions;
let mention = mention.into_inner(); let mention = mention.into_inner();
@ -90,6 +94,15 @@ pub fn accept(conn: MainDatabase, mention: Form<WebMention>) -> Result<models::W
.execute(&*conn) .execute(&*conn)
.map_err(Error::Database)?; .map_err(Error::Database)?;
dw.send(format!(
"<{}> mentioned <{}> (<https://mi.within.website/api/webmention/{}>)",
wm.source_url, wm.target_url, wm.id
))
.map_err(|why| {
error!("webhook send failed: {}", why);
Error::Web(why)
})?;
Ok(wm) Ok(wm)
} }

View File

@ -43,6 +43,7 @@ impl Client {
}) })
} }
#[instrument(skip(self), err)]
pub fn send(&self, body: String) -> Result<()> { pub fn send(&self, body: String) -> Result<()> {
let resp = ureq::post(&self.webhook_url) let resp = ureq::post(&self.webhook_url)
.set("User-Agent", crate::APPLICATION_NAME) .set("User-Agent", crate::APPLICATION_NAME)