Skip to content

Commit 3f472a4

Browse files
drew-harrisskeptrunedev
authored andcommitted
feat: create operator and impl system for content
1 parent 82035db commit 3f472a4

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

server/src/handlers/webhook_handler.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
use std::str::FromStr;
2+
13
use actix_web::{web, HttpResponse};
24
use serde::{Deserialize, Serialize};
35
use serde_json::Value;
6+
use uuid::Uuid;
7+
8+
use crate::{errors::ServiceError, operators::webhook_operator::publish_content};
9+
10+
use super::chunk_handler::ChunkReqPayload;
411

512
#[derive(Serialize, Deserialize)]
613
struct WebhookRespose {
@@ -18,12 +25,28 @@ pub enum Operation {
1825
ScheduledEnd,
1926
}
2027

28+
#[derive(Serialize, Deserialize, Debug)]
29+
#[serde(rename_all = "camelCase")]
30+
pub struct ContentValue {
31+
id: String,
32+
name: String,
33+
model_id: String,
34+
data: Value,
35+
}
36+
37+
impl Into<ChunkReqPayload> for ContentValue {
38+
fn into(self) -> ChunkReqPayload {
39+
todo!();
40+
}
41+
}
42+
2143
#[derive(Serialize, Deserialize, Debug)]
2244
#[serde(rename_all = "camelCase")]
2345
pub struct WebhookRequest {
2446
model_name: String,
25-
new_value: Value,
26-
previous_value: Value,
47+
new_value: ContentValue,
48+
// This is usually always there just being safe
49+
previous_value: Option<ContentValue>,
2750
operation: Operation,
2851
}
2952

@@ -39,12 +62,35 @@ pub async fn builder_webhook(
3962
payload: web::Json<WebhookRequest>,
4063
query: web::Query<WebhookQueryParams>,
4164
) -> Result<HttpResponse, actix_web::Error> {
65+
// Ensure that the trieve_key and trieve_dataset are valid
66+
if query.trieve_key.is_empty() || query.trieve_dataset.is_empty() {
67+
return Err(ServiceError::BadRequest(
68+
"trieve_key and trieve_dataset are required".to_string(),
69+
)
70+
.into());
71+
}
72+
4273
let payload = payload.into_inner();
4374
let query = query.into_inner();
75+
let dataset_id = uuid::Uuid::from_str(query.trieve_dataset.as_str()).map_err(|_| {
76+
ServiceError::BadRequest(format!("Invalid dataset id: {}", query.trieve_dataset))
77+
})?;
78+
79+
// TODO: Ensure user has proper perms
4480

4581
log::info!("Webhook received: {:?}", payload);
4682
log::info!("Query params: {:?}", query);
4783

84+
match payload.operation {
85+
Operation::Publish => {
86+
publish_content(dataset_id, payload.new_value).await?;
87+
}
88+
89+
_ => {
90+
return Err(ServiceError::BadRequest("Operation not supported".to_string()).into());
91+
}
92+
}
93+
4894
Ok(HttpResponse::Ok().json(WebhookRespose {
4995
message: "Webhook received".to_string(),
5096
}))

server/src/operators/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ pub mod stripe_operator;
1919
pub mod topic_operator;
2020
pub mod typo_operator;
2121
pub mod user_operator;
22+
pub mod webhook_operator;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use crate::{
2+
errors::ServiceError,
3+
handlers::{chunk_handler::ChunkReqPayload, webhook_handler::ContentValue},
4+
};
5+
6+
pub async fn publish_content<T: Into<ChunkReqPayload>>(
7+
dataset: uuid::Uuid,
8+
value: T,
9+
) -> Result<(), ServiceError> {
10+
todo!();
11+
}

0 commit comments

Comments
 (0)