1+ use std:: str:: FromStr ;
2+
13use actix_web:: { web, HttpResponse } ;
24use serde:: { Deserialize , Serialize } ;
35use 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 ) ]
613struct 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" ) ]
2345pub 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 } ) )
0 commit comments