2024-12-03 06:09:01 +00:00
|
|
|
use futures::StreamExt;
|
2024-12-11 02:04:02 +00:00
|
|
|
use crate::types::{MessageResult,NatsStartupError};
|
2024-12-12 00:44:56 +00:00
|
|
|
use crate::nats_types::PublishFixRequest;
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2024-12-03 06:52:01 +00:00
|
|
|
#[derive(Debug)]
|
2024-12-03 06:09:01 +00:00
|
|
|
enum PublishError{
|
2024-12-11 02:04:02 +00:00
|
|
|
Messages(async_nats::jetstream::consumer::pull::MessagesError),
|
|
|
|
DoubleAck(async_nats::Error),
|
2024-12-12 00:44:56 +00:00
|
|
|
Get(rbx_asset::cookie::GetError),
|
|
|
|
Json(serde_json::Error),
|
|
|
|
Upload(rbx_asset::cookie::UploadError),
|
|
|
|
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{
|
2024-12-11 02:04:02 +00:00
|
|
|
messages:async_nats::jetstream::consumer::pull::Stream,
|
2024-12-03 06:52:01 +00:00
|
|
|
roblox_cookie:rbx_asset::cookie::CookieContext,
|
2024-12-12 00:44:56 +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(
|
2024-12-12 05:07:51 +00:00
|
|
|
stream:async_nats::jetstream::stream::Stream,
|
2024-12-03 06:52:01 +00:00
|
|
|
roblox_cookie:rbx_asset::cookie::CookieContext,
|
2024-12-12 00:44:56 +00:00
|
|
|
api:api::Context,
|
2024-12-11 02:04:02 +00:00
|
|
|
)->Result<Self,NatsStartupError>{
|
2024-12-03 06:09:01 +00:00
|
|
|
Ok(Self{
|
2024-12-12 05:07:51 +00:00
|
|
|
messages:stream.create_consumer_strict(async_nats::jetstream::consumer::pull::Config{
|
|
|
|
filter_subject:"maptest.submissions.publish.fix".to_owned(),
|
2024-12-11 02:04:02 +00:00
|
|
|
..Default::default()
|
|
|
|
}).await.map_err(NatsStartupError::ConsumerCreateStrict)?
|
|
|
|
.messages().await.map_err(NatsStartupError::Stream)?,
|
2024-12-03 06:52:01 +00:00
|
|
|
roblox_cookie,
|
2024-12-12 00:44:56 +00:00
|
|
|
api,
|
2024-12-03 06:09:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
pub async fn run(mut self){
|
2024-12-11 02:04:02 +00:00
|
|
|
while let Some(message_result)=self.messages.next().await{
|
|
|
|
self.publish_supress_error(message_result).await
|
2024-12-12 00:44:56 +00:00
|
|
|
}
|
|
|
|
}
|
2024-12-11 02:04:02 +00:00
|
|
|
async fn publish_supress_error(&self,message_result:MessageResult){
|
|
|
|
match self.publish(message_result).await{
|
2024-12-12 00:44:56 +00:00
|
|
|
Ok(())=>println!("[PublishFix] Published, hooray!"),
|
|
|
|
Err(e)=>println!("[PublishFix] There was an error, oopsie! {e}"),
|
2024-12-03 06:09:01 +00:00
|
|
|
}
|
|
|
|
}
|
2024-12-11 02:04:02 +00:00
|
|
|
async fn publish(&self,message_result:MessageResult)->Result<(),PublishError>{
|
|
|
|
println!("publish_fix {:?}",message_result);
|
|
|
|
let message=message_result.map_err(PublishError::Messages)?;
|
|
|
|
message.double_ack().await.map_err(PublishError::DoubleAck)?;
|
2024-12-12 00:44:56 +00:00
|
|
|
// decode json
|
|
|
|
let publish_info:PublishFixRequest=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.ModelID,
|
|
|
|
version:Some(publish_info.ModelVersion),
|
|
|
|
}).await.map_err(PublishError::Get)?;
|
|
|
|
|
|
|
|
// upload the map to the strafesnet group
|
|
|
|
let _upload_response=self.roblox_cookie.upload(rbx_asset::cookie::UploadRequest{
|
|
|
|
assetid:publish_info.TargetAssetID,
|
|
|
|
groupId:Some(crate::GROUP_STRAFESNET),
|
|
|
|
name:None,
|
|
|
|
description:None,
|
|
|
|
ispublic:None,
|
|
|
|
allowComments:None,
|
|
|
|
},model_data).await.map_err(PublishError::Upload)?;
|
|
|
|
|
|
|
|
// that's it, the database entry does not need to be changed.
|
|
|
|
|
|
|
|
// mark submission as published
|
|
|
|
self.api.action_submission_publish(
|
|
|
|
api::SubmissionID(publish_info.SubmissionID)
|
|
|
|
).await.map_err(PublishError::ApiActionSubmissionPublish)?;
|
|
|
|
|
|
|
|
Ok(())
|
2024-12-03 06:09:01 +00:00
|
|
|
}
|
|
|
|
}
|