refactor: drop webhook system

This commit is contained in:
kieran 2025-01-15 20:31:05 +00:00
parent 5e97f4d12a
commit b45018d0de
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
8 changed files with 3 additions and 118 deletions

2
Cargo.lock generated
View File

@ -3202,7 +3202,6 @@ dependencies = [
"nostr", "nostr",
"nostr-cursor", "nostr-cursor",
"pretty_env_logger", "pretty_env_logger",
"rand",
"regex", "regex",
"reqwest", "reqwest",
"rocket", "rocket",
@ -3213,7 +3212,6 @@ dependencies = [
"sqlx-postgres", "sqlx-postgres",
"tokio", "tokio",
"tokio-util", "tokio-util",
"url",
"uuid", "uuid",
] ]

View File

@ -47,7 +47,6 @@ sha2 = "0.10.8"
sqlx = { version = "0.8.1", features = ["mysql", "runtime-tokio", "chrono", "uuid"] } sqlx = { version = "0.8.1", features = ["mysql", "runtime-tokio", "chrono", "uuid"] }
config = { version = "0.14.0", features = ["yaml"] } config = { version = "0.14.0", features = ["yaml"] }
chrono = { version = "0.4.38", features = ["serde"] } chrono = { version = "0.4.38", features = ["serde"] }
url = "2.5.0"
serde_with = { version = "3.8.1", features = ["hex"] } serde_with = { version = "3.8.1", features = ["hex"] }
reqwest = { version = "0.12.8", features = ["stream"] } reqwest = { version = "0.12.8", features = ["stream"] }
clap = { version = "4.5.18", features = ["derive"] } clap = { version = "4.5.18", features = ["derive"] }
@ -64,6 +63,3 @@ sqlx-postgres = { version = "0.8.2", optional = true, features = ["chrono", "uui
http-range-header = { version = "0.4.2", optional = true } http-range-header = { version = "0.4.2", optional = true }
nostr-cursor = { git = "https://git.v0l.io/Kieran/nostr_backup_proc.git", branch = "main", optional = true } nostr-cursor = { git = "https://git.v0l.io/Kieran/nostr_backup_proc.git", branch = "main", optional = true }
regex = { version = "1.11.1", optional = true } regex = { version = "1.11.1", optional = true }
rand = "0.8.5"

View File

@ -21,9 +21,6 @@ vit_model:
model: "/home/kieran/Downloads/falcon_nsfw.safetensors" model: "/home/kieran/Downloads/falcon_nsfw.safetensors"
config: "/home/kieran/Downloads/falcon_nsfw.json" config: "/home/kieran/Downloads/falcon_nsfw.json"
# Webhook api endpoint
# webhook_url: "https://api.snort.social/api/v1/media/webhook"
# Analytics support # Analytics support
# plausible_url: "https://plausible.com/" # plausible_url: "https://plausible.com/"

View File

@ -18,7 +18,6 @@ use route96::filesystem::FileStore;
use route96::routes; use route96::routes;
use route96::routes::{get_blob, head_blob, root}; use route96::routes::{get_blob, head_blob, root};
use route96::settings::Settings; use route96::settings::Settings;
use route96::webhook::Webhook;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about)] #[command(version, about)]
@ -68,12 +67,6 @@ async fn main() -> Result<(), Error> {
.manage(FileStore::new(settings.clone())) .manage(FileStore::new(settings.clone()))
.manage(settings.clone()) .manage(settings.clone())
.manage(db.clone()) .manage(db.clone())
.manage(
settings
.webhook_url
.as_ref()
.map(|w| Webhook::new(w.clone())),
)
.attach(CORS) .attach(CORS)
.attach(Shield::new()) // disable .attach(Shield::new()) // disable
.mount( .mount(

View File

@ -11,4 +11,3 @@ pub mod settings;
#[cfg(any(feature = "void-cat-redirects", feature = "bin-void-cat-migrate"))] #[cfg(any(feature = "void-cat-redirects", feature = "bin-void-cat-migrate"))]
pub mod void_db; pub mod void_db;
pub mod void_file; pub mod void_file;
pub mod webhook;

View File

@ -3,7 +3,6 @@ use crate::db::{Database, FileUpload};
use crate::filesystem::{FileStore, FileSystemResult}; use crate::filesystem::{FileStore, FileSystemResult};
use crate::routes::{delete_file, Nip94Event}; use crate::routes::{delete_file, Nip94Event};
use crate::settings::Settings; use crate::settings::Settings;
use crate::webhook::Webhook;
use log::error; use log::error;
use nostr::prelude::hex; use nostr::prelude::hex;
use nostr::{Alphabet, SingleLetterTag, TagKind}; use nostr::{Alphabet, SingleLetterTag, TagKind};
@ -15,7 +14,6 @@ use rocket::serde::json::Json;
use rocket::{routes, Data, Request, Response, Route, State}; use rocket::{routes, Data, Request, Response, Route, State};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::fs;
use tokio::io::AsyncRead; use tokio::io::AsyncRead;
use tokio_util::io::StreamReader; use tokio_util::io::StreamReader;
@ -213,10 +211,9 @@ async fn upload(
fs: &State<FileStore>, fs: &State<FileStore>,
db: &State<Database>, db: &State<Database>,
settings: &State<Settings>, settings: &State<Settings>,
webhook: &State<Option<Webhook>>,
data: Data<'_>, data: Data<'_>,
) -> BlossomResponse { ) -> BlossomResponse {
process_upload("upload", false, auth, fs, db, settings, webhook, data).await process_upload("upload", false, auth, fs, db, settings, data).await
} }
#[rocket::put("/mirror", data = "<req>", format = "json")] #[rocket::put("/mirror", data = "<req>", format = "json")]
@ -225,7 +222,6 @@ async fn mirror(
fs: &State<FileStore>, fs: &State<FileStore>,
db: &State<Database>, db: &State<Database>,
settings: &State<Settings>, settings: &State<Settings>,
webhook: &State<Option<Webhook>>,
req: Json<MirrorRequest>, req: Json<MirrorRequest>,
) -> BlossomResponse { ) -> BlossomResponse {
if !check_method(&auth.event, "mirror") { if !check_method(&auth.event, "mirror") {
@ -263,7 +259,6 @@ async fn mirror(
fs, fs,
db, db,
settings, settings,
webhook,
) )
.await .await
} }
@ -281,10 +276,9 @@ async fn upload_media(
fs: &State<FileStore>, fs: &State<FileStore>,
db: &State<Database>, db: &State<Database>,
settings: &State<Settings>, settings: &State<Settings>,
webhook: &State<Option<Webhook>>,
data: Data<'_>, data: Data<'_>,
) -> BlossomResponse { ) -> BlossomResponse {
process_upload("media", true, auth, fs, db, settings, webhook, data).await process_upload("media", true, auth, fs, db, settings, data).await
} }
fn check_head(auth: BlossomAuth, settings: &State<Settings>) -> BlossomHead { fn check_head(auth: BlossomAuth, settings: &State<Settings>) -> BlossomHead {
@ -337,7 +331,6 @@ async fn process_upload(
fs: &State<FileStore>, fs: &State<FileStore>,
db: &State<Database>, db: &State<Database>,
settings: &State<Settings>, settings: &State<Settings>,
webhook: &State<Option<Webhook>>,
data: Data<'_>, data: Data<'_>,
) -> BlossomResponse { ) -> BlossomResponse {
if !check_method(&auth.event, method) { if !check_method(&auth.event, method) {
@ -380,7 +373,6 @@ async fn process_upload(
fs, fs,
db, db,
settings, settings,
webhook,
) )
.await .await
} }
@ -394,30 +386,12 @@ async fn process_stream<'p, S>(
fs: &State<FileStore>, fs: &State<FileStore>,
db: &State<Database>, db: &State<Database>,
settings: &State<Settings>, settings: &State<Settings>,
webhook: &State<Option<Webhook>>,
) -> BlossomResponse ) -> BlossomResponse
where where
S: AsyncRead + Unpin + 'p, S: AsyncRead + Unpin + 'p,
{ {
let upload = match fs.put(stream, mime_type, compress).await { let upload = match fs.put(stream, mime_type, compress).await {
Ok(FileSystemResult::NewFile(blob)) => { Ok(FileSystemResult::NewFile(blob)) => {
if let Some(wh) = webhook.as_ref() {
match wh.store_file(pubkey, blob.clone()).await {
Ok(store) => {
if !store {
let _ = fs::remove_file(blob.path);
return BlossomResponse::error("Upload rejected");
}
}
Err(e) => {
let _ = fs::remove_file(blob.path);
return BlossomResponse::error(format!(
"Internal error, failed to call webhook: {}",
e
));
}
}
}
let mut ret: FileUpload = (&blob).into(); let mut ret: FileUpload = (&blob).into();
// update file data before inserting // update file data before inserting

View File

@ -1,5 +1,4 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::fs;
use std::ops::Sub; use std::ops::Sub;
use std::time::Duration; use std::time::Duration;
@ -17,7 +16,6 @@ use crate::db::{Database, FileUpload};
use crate::filesystem::{FileStore, FileSystemResult}; use crate::filesystem::{FileStore, FileSystemResult};
use crate::routes::{delete_file, Nip94Event, PagedResult}; use crate::routes::{delete_file, Nip94Event, PagedResult};
use crate::settings::Settings; use crate::settings::Settings;
use crate::webhook::Webhook;
#[derive(Serialize, Default)] #[derive(Serialize, Default)]
#[serde(crate = "rocket::serde")] #[serde(crate = "rocket::serde")]
@ -174,7 +172,6 @@ async fn upload(
fs: &State<FileStore>, fs: &State<FileStore>,
db: &State<Database>, db: &State<Database>,
settings: &State<Settings>, settings: &State<Settings>,
webhook: &State<Option<Webhook>>,
form: Form<Nip96Form<'_>>, form: Form<Nip96Form<'_>>,
) -> Nip96Response { ) -> Nip96Response {
if let Some(size) = auth.content_length { if let Some(size) = auth.content_length {
@ -215,24 +212,6 @@ async fn upload(
.await .await
{ {
Ok(FileSystemResult::NewFile(blob)) => { Ok(FileSystemResult::NewFile(blob)) => {
if let Some(wh) = webhook.as_ref() {
match wh.store_file(&pubkey_vec, blob.clone()).await {
Ok(store) => {
if !store {
let _ = fs::remove_file(blob.path);
return Nip96Response::error("Upload rejected");
}
}
Err(e) => {
let _ = fs::remove_file(blob.path);
return Nip96Response::error(&format!(
"Internal error, failed to call webhook: {}",
e
));
}
}
}
let mut upload: FileUpload = (&blob).into(); let mut upload: FileUpload = (&blob).into();
upload.name = form.caption.map(|cap| cap.to_string()); upload.name = form.caption.map(|cap| cap.to_string());
upload.alt = form.alt.as_ref().map(|s| s.to_string()); upload.alt = form.alt.as_ref().map(|s| s.to_string());

View File

@ -1,51 +0,0 @@
use anyhow::Error;
use nostr::serde_json;
use reqwest::{Client, ClientBuilder};
use serde::{Deserialize, Serialize};
use crate::filesystem::NewFileResult;
pub struct Webhook {
url: String,
client: Client,
}
#[derive(Serialize, Deserialize)]
struct WebhookRequest<T> {
pub action: String,
pub subject: Option<String>,
pub payload: T,
}
impl Webhook {
pub fn new(url: String) -> Self {
Self {
url,
client: ClientBuilder::new().build().unwrap(),
}
}
/// Ask webhook api if this file can be accepted
pub async fn store_file(&self, pubkey: &Vec<u8>, fs: NewFileResult) -> Result<bool, Error> {
let body = WebhookRequest {
action: "store_file".to_string(),
subject: Some(hex::encode(pubkey)),
payload: fs,
};
let body = serde_json::to_string(&body)?;
let req = self
.client
.post(&self.url)
.header("accept", "application/json")
.header("content-type", "application/json")
.body(body)
.send()
.await?;
if req.status() == 200 {
Ok(true)
} else {
Ok(false)
}
}
}