From 9070d77f4118a9ebaa8b0d1af47c0cab638c2f57 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 17 Dec 2024 20:30:31 -0800 Subject: [PATCH] validation: set status on failure --- validation/api/src/internal.rs | 1 + validation/src/message_handler.rs | 11 ++++++++--- validation/src/publish_fix.rs | 6 +----- validation/src/publish_new.rs | 6 +----- validation/src/validator.rs | 32 +++++++++++++++++++++---------- 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/validation/api/src/internal.rs b/validation/api/src/internal.rs index dc36912..bd636dc 100644 --- a/validation/api/src/internal.rs +++ b/validation/api/src/internal.rs @@ -173,5 +173,6 @@ impl Context{ Ok(()) } action!(action_submission_validated,"validator-validated"); + action!(action_submission_accepted,"validator-failed"); action!(action_submission_released,"releaser-released"); } diff --git a/validation/src/message_handler.rs b/validation/src/message_handler.rs index 6843495..1c0cfe6 100644 --- a/validation/src/message_handler.rs +++ b/validation/src/message_handler.rs @@ -3,6 +3,7 @@ pub enum HandleMessageError{ Messages(async_nats::jetstream::consumer::pull::MessagesError), DoubleAck(async_nats::Error), + Json(serde_json::Error), UnknownSubject(String), PublishNew(crate::publish_new::PublishError), PublishFix(crate::publish_fix::PublishError), @@ -17,6 +18,10 @@ impl std::error::Error for HandleMessageError{} pub type MessageResult=Result; +fn from_slice<'a,T:serde::de::Deserialize<'a>>(slice:&'a [u8])->Result{ + serde_json::from_slice(slice).map_err(HandleMessageError::Json) +} + pub struct MessageHandler{ publish_new:crate::publish_new::Publisher, publish_fix:crate::publish_fix::Publisher, @@ -39,9 +44,9 @@ impl MessageHandler{ let message=message_result.map_err(HandleMessageError::Messages)?; message.double_ack().await.map_err(HandleMessageError::DoubleAck)?; match message.subject.as_str(){ - "maptest.submissions.publishnew"=>self.publish_new.publish(message).await.map_err(HandleMessageError::PublishNew), - "maptest.submissions.publishfix"=>self.publish_fix.publish(message).await.map_err(HandleMessageError::PublishFix), - "maptest.submissions.validate"=>self.validator.validate(message).await.map_err(HandleMessageError::Validation), + "maptest.submissions.publishnew"=>self.publish_new.publish(from_slice(&message.payload)?).await.map_err(HandleMessageError::PublishNew), + "maptest.submissions.publishfix"=>self.publish_fix.publish(from_slice(&message.payload)?).await.map_err(HandleMessageError::PublishFix), + "maptest.submissions.validate"=>self.validator.validate(from_slice(&message.payload)?).await.map_err(HandleMessageError::Validation), other=>Err(HandleMessageError::UnknownSubject(other.to_owned())) } } diff --git a/validation/src/publish_fix.rs b/validation/src/publish_fix.rs index 2602618..d91bf14 100644 --- a/validation/src/publish_fix.rs +++ b/validation/src/publish_fix.rs @@ -32,11 +32,7 @@ impl Publisher{ api_internal, } } - pub async fn publish(&self,message:async_nats::jetstream::Message)->Result<(),PublishError>{ - println!("publish_fix {:?}",message.message.payload); - // decode json - let publish_info:PublishFixRequest=serde_json::from_slice(&message.payload).map_err(PublishError::Json)?; - + pub async fn publish(&self,publish_info:PublishFixRequest)->Result<(),PublishError>{ // download the map model version let model_data=self.roblox_cookie.get_asset(rbx_asset::cookie::GetAssetRequest{ asset_id:publish_info.ModelID, diff --git a/validation/src/publish_new.rs b/validation/src/publish_new.rs index e9c71da..8cc9ac8 100644 --- a/validation/src/publish_new.rs +++ b/validation/src/publish_new.rs @@ -33,11 +33,7 @@ impl Publisher{ api, } } - pub async fn publish(&self,message:async_nats::jetstream::Message)->Result<(),PublishError>{ - println!("publish_new {:?}",message.message.payload); - // decode json - let publish_info:PublishNewRequest=serde_json::from_slice(&message.payload).map_err(PublishError::Json)?; - + pub async fn publish(&self,publish_info:PublishNewRequest)->Result<(),PublishError>{ // download the map model version let model_data=self.roblox_cookie.get_asset(rbx_asset::cookie::GetAssetRequest{ asset_id:publish_info.ModelID, diff --git a/validation/src/validator.rs b/validation/src/validator.rs index 8193298..b277346 100644 --- a/validation/src/validator.rs +++ b/validation/src/validator.rs @@ -41,7 +41,6 @@ pub enum ValidateError{ Blocked, NotAllowed, Get(rbx_asset::cookie::GetError), - Json(serde_json::Error), ReadDom(ReadDomError), ApiGetScriptPolicy(submissions_api::Error), ApiGetScript(submissions_api::Error), @@ -75,11 +74,29 @@ impl Validator{ api, } } - pub async fn validate(&self,message:async_nats::jetstream::Message)->Result<(),ValidateError>{ - println!("validate {:?}",message.message.payload); - // decode json - let validate_info:ValidateRequest=serde_json::from_slice(&message.payload).map_err(ValidateError::Json)?; + pub async fn validate(&self,validate_info:ValidateRequest)->Result<(),ValidateError>{ + let submission_id=validate_info.SubmissionID; + let validate_result=self.validate_inner(validate_info).await; + // update the submission depending on the result + match &validate_result{ + Ok(())=>{ + // update the submission model status to validated + self.api.action_submission_validated( + submissions_api::internal::SubmissionID(submission_id) + ).await.map_err(ValidateError::ApiActionSubmissionValidate)?; + }, + Err(_)=>{ + // update the submission model status to accepted + self.api.action_submission_accepted( + submissions_api::internal::SubmissionID(submission_id) + ).await.map_err(ValidateError::ApiActionSubmissionValidate)?; + }, + } + + validate_result + } + pub async fn validate_inner(&self,validate_info:ValidateRequest)->Result<(),ValidateError>{ // download map let data=self.roblox_cookie.get_asset(rbx_asset::cookie::GetAssetRequest{ asset_id:validate_info.ModelID, @@ -219,11 +236,6 @@ impl Validator{ }).await.map_err(ValidateError::ApiUpdateSubmissionModel)?; }; - // update the submission model status to validated - self.api.action_submission_validated( - submissions_api::internal::SubmissionID(validate_info.SubmissionID) - ).await.map_err(ValidateError::ApiActionSubmissionValidate)?; - Ok(()) } }