maps-service/validation/src/publish_new.rs

78 lines
2.3 KiB
Rust
Raw Normal View History

2024-12-03 06:09:01 +00:00
use futures::StreamExt;
2024-12-07 05:00:19 +00:00
use crate::nats_types::PublishNewRequest;
2024-12-03 06:52:01 +00:00
#[derive(Debug)]
2024-12-03 06:09:01 +00:00
enum PublishError{
2024-12-07 05:00:19 +00:00
Get(rbx_asset::cookie::GetError),
Json(serde_json::Error),
Create(rbx_asset::cookie::CreateError),
ApiActionSubmissionPublish(api::Error),
2024-12-03 06:09:01 +00:00
}
2024-12-03 06:52:01 +00:00
impl std::fmt::Display for PublishError{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
impl std::error::Error for PublishError{}
2024-12-03 06:09:01 +00:00
pub struct Publisher{
subscriber:async_nats::Subscriber,
2024-12-03 06:52:01 +00:00
roblox_cookie:rbx_asset::cookie::CookieContext,
2024-12-07 05:00:19 +00:00
api:api::Context,
2024-12-03 06:09:01 +00:00
}
impl Publisher{
2024-12-03 06:52:01 +00:00
pub async fn new(
nats:async_nats::Client,
roblox_cookie:rbx_asset::cookie::CookieContext,
2024-12-07 05:00:19 +00:00
api:api::Context,
2024-12-03 06:52:01 +00:00
)->Result<Self,async_nats::SubscribeError>{
2024-12-03 06:09:01 +00:00
Ok(Self{
subscriber:nats.subscribe("publish_new").await?,
2024-12-03 06:52:01 +00:00
roblox_cookie,
2024-12-07 05:00:19 +00:00
api,
2024-12-03 06:09:01 +00:00
})
}
pub async fn run(mut self){
while let Some(message)=self.subscriber.next().await{
2024-12-07 05:00:19 +00:00
self.publish_supress_error(message).await
}
}
async fn publish_supress_error(&self,message:async_nats::Message){
match self.publish(message).await{
Ok(())=>println!("Published, hooray!"),
Err(e)=>println!("[PublishNew] There was an error, oopsie! {e}"),
2024-12-03 06:09:01 +00:00
}
}
2024-12-07 05:00:19 +00:00
async fn publish(&self,message:async_nats::Message)->Result<(),PublishError>{
2024-12-03 06:09:01 +00:00
println!("publish {:?}",message);
2024-12-07 05:00:19 +00:00
// decode json
let publish_info:PublishNewRequest=serde_json::from_slice(&message.payload).map_err(PublishError::Json)?;
// download the map model version
let model_data=self.roblox_cookie.get_asset(rbx_asset::cookie::GetAssetRequest{
asset_id:publish_info.model_id,
version:Some(publish_info.model_version),
}).await.map_err(PublishError::Get)?;
// upload the map to the strafesnet group
let upload_response=self.roblox_cookie.create(rbx_asset::cookie::CreateRequest{
name:publish_info.display_name,
description:"".to_owned(),
ispublic:false,
allowComments:false,
groupId:Some(crate::GROUP_STRAFESNET),
},model_data).await.map_err(PublishError::Create)?;
// create the map entry in the game database, including release date
// game_rpc.maps.create(upload_response.AssetId)
// mark submission as published
self.api.action_submission_publish(
api::SubmissionID(publish_info.submission_id)
).await.map_err(PublishError::ApiActionSubmissionPublish)?;
Ok(())
2024-12-03 06:09:01 +00:00
}
}