refactor: drop webhook system
This commit is contained in:
parent
5e97f4d12a
commit
b45018d0de
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -3202,7 +3202,6 @@ dependencies = [
|
||||
"nostr",
|
||||
"nostr-cursor",
|
||||
"pretty_env_logger",
|
||||
"rand",
|
||||
"regex",
|
||||
"reqwest",
|
||||
"rocket",
|
||||
@ -3213,7 +3212,6 @@ dependencies = [
|
||||
"sqlx-postgres",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"url",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
|
@ -47,7 +47,6 @@ sha2 = "0.10.8"
|
||||
sqlx = { version = "0.8.1", features = ["mysql", "runtime-tokio", "chrono", "uuid"] }
|
||||
config = { version = "0.14.0", features = ["yaml"] }
|
||||
chrono = { version = "0.4.38", features = ["serde"] }
|
||||
url = "2.5.0"
|
||||
serde_with = { version = "3.8.1", features = ["hex"] }
|
||||
reqwest = { version = "0.12.8", features = ["stream"] }
|
||||
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 }
|
||||
nostr-cursor = { git = "https://git.v0l.io/Kieran/nostr_backup_proc.git", branch = "main", optional = true }
|
||||
regex = { version = "1.11.1", optional = true }
|
||||
rand = "0.8.5"
|
||||
|
||||
|
||||
|
@ -21,9 +21,6 @@ vit_model:
|
||||
model: "/home/kieran/Downloads/falcon_nsfw.safetensors"
|
||||
config: "/home/kieran/Downloads/falcon_nsfw.json"
|
||||
|
||||
# Webhook api endpoint
|
||||
# webhook_url: "https://api.snort.social/api/v1/media/webhook"
|
||||
|
||||
# Analytics support
|
||||
# plausible_url: "https://plausible.com/"
|
||||
|
||||
|
@ -18,7 +18,6 @@ use route96::filesystem::FileStore;
|
||||
use route96::routes;
|
||||
use route96::routes::{get_blob, head_blob, root};
|
||||
use route96::settings::Settings;
|
||||
use route96::webhook::Webhook;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about)]
|
||||
@ -68,12 +67,6 @@ async fn main() -> Result<(), Error> {
|
||||
.manage(FileStore::new(settings.clone()))
|
||||
.manage(settings.clone())
|
||||
.manage(db.clone())
|
||||
.manage(
|
||||
settings
|
||||
.webhook_url
|
||||
.as_ref()
|
||||
.map(|w| Webhook::new(w.clone())),
|
||||
)
|
||||
.attach(CORS)
|
||||
.attach(Shield::new()) // disable
|
||||
.mount(
|
||||
|
@ -11,4 +11,3 @@ pub mod settings;
|
||||
#[cfg(any(feature = "void-cat-redirects", feature = "bin-void-cat-migrate"))]
|
||||
pub mod void_db;
|
||||
pub mod void_file;
|
||||
pub mod webhook;
|
||||
|
@ -3,7 +3,6 @@ use crate::db::{Database, FileUpload};
|
||||
use crate::filesystem::{FileStore, FileSystemResult};
|
||||
use crate::routes::{delete_file, Nip94Event};
|
||||
use crate::settings::Settings;
|
||||
use crate::webhook::Webhook;
|
||||
use log::error;
|
||||
use nostr::prelude::hex;
|
||||
use nostr::{Alphabet, SingleLetterTag, TagKind};
|
||||
@ -15,7 +14,6 @@ use rocket::serde::json::Json;
|
||||
use rocket::{routes, Data, Request, Response, Route, State};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use tokio::io::AsyncRead;
|
||||
use tokio_util::io::StreamReader;
|
||||
|
||||
@ -213,10 +211,9 @@ async fn upload(
|
||||
fs: &State<FileStore>,
|
||||
db: &State<Database>,
|
||||
settings: &State<Settings>,
|
||||
webhook: &State<Option<Webhook>>,
|
||||
data: Data<'_>,
|
||||
) -> 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")]
|
||||
@ -225,7 +222,6 @@ async fn mirror(
|
||||
fs: &State<FileStore>,
|
||||
db: &State<Database>,
|
||||
settings: &State<Settings>,
|
||||
webhook: &State<Option<Webhook>>,
|
||||
req: Json<MirrorRequest>,
|
||||
) -> BlossomResponse {
|
||||
if !check_method(&auth.event, "mirror") {
|
||||
@ -263,7 +259,6 @@ async fn mirror(
|
||||
fs,
|
||||
db,
|
||||
settings,
|
||||
webhook,
|
||||
)
|
||||
.await
|
||||
}
|
||||
@ -281,10 +276,9 @@ async fn upload_media(
|
||||
fs: &State<FileStore>,
|
||||
db: &State<Database>,
|
||||
settings: &State<Settings>,
|
||||
webhook: &State<Option<Webhook>>,
|
||||
data: Data<'_>,
|
||||
) -> 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 {
|
||||
@ -337,7 +331,6 @@ async fn process_upload(
|
||||
fs: &State<FileStore>,
|
||||
db: &State<Database>,
|
||||
settings: &State<Settings>,
|
||||
webhook: &State<Option<Webhook>>,
|
||||
data: Data<'_>,
|
||||
) -> BlossomResponse {
|
||||
if !check_method(&auth.event, method) {
|
||||
@ -380,7 +373,6 @@ async fn process_upload(
|
||||
fs,
|
||||
db,
|
||||
settings,
|
||||
webhook,
|
||||
)
|
||||
.await
|
||||
}
|
||||
@ -394,30 +386,12 @@ async fn process_stream<'p, S>(
|
||||
fs: &State<FileStore>,
|
||||
db: &State<Database>,
|
||||
settings: &State<Settings>,
|
||||
webhook: &State<Option<Webhook>>,
|
||||
) -> BlossomResponse
|
||||
where
|
||||
S: AsyncRead + Unpin + 'p,
|
||||
{
|
||||
let upload = match fs.put(stream, mime_type, compress).await {
|
||||
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();
|
||||
|
||||
// update file data before inserting
|
||||
|
@ -1,5 +1,4 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::ops::Sub;
|
||||
use std::time::Duration;
|
||||
|
||||
@ -17,7 +16,6 @@ use crate::db::{Database, FileUpload};
|
||||
use crate::filesystem::{FileStore, FileSystemResult};
|
||||
use crate::routes::{delete_file, Nip94Event, PagedResult};
|
||||
use crate::settings::Settings;
|
||||
use crate::webhook::Webhook;
|
||||
|
||||
#[derive(Serialize, Default)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
@ -174,7 +172,6 @@ async fn upload(
|
||||
fs: &State<FileStore>,
|
||||
db: &State<Database>,
|
||||
settings: &State<Settings>,
|
||||
webhook: &State<Option<Webhook>>,
|
||||
form: Form<Nip96Form<'_>>,
|
||||
) -> Nip96Response {
|
||||
if let Some(size) = auth.content_length {
|
||||
@ -215,24 +212,6 @@ async fn upload(
|
||||
.await
|
||||
{
|
||||
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();
|
||||
upload.name = form.caption.map(|cap| cap.to_string());
|
||||
upload.alt = form.alt.as_ref().map(|s| s.to_string());
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user