Move waiting for QR to task

This commit is contained in:
Artemis Tosini 2024-09-04 21:29:49 +00:00
parent 4207832545
commit 277967fae9
Signed by: artemist
GPG key ID: ADFFE553DCBB831E

View file

@ -95,35 +95,43 @@ pub async fn main() -> eyre::Result<()> {
response.body.interval(), response.body.interval(),
)); ));
let (refresh_token, account_name) = loop { let session_cloned = session.clone();
interval.tick().await; let poll_handle = tokio::spawn(async move {
let request = CAuthentication_PollAuthSessionStatus_Request { let mut session = session_cloned;
client_id: response.body.client_id, loop {
request_id: response.body.request_id.clone(), interval.tick().await;
..Default::default() let request = CAuthentication_PollAuthSessionStatus_Request {
}; client_id: response.body.client_id,
let response: CMProtoBufMessage<CAuthentication_PollAuthSessionStatus_Response> = session request_id: response.body.request_id.clone(),
.call_service_method( ..Default::default()
"Authentication.PollAuthSessionStatus#1".to_string(), };
request, let response: CMProtoBufMessage<CAuthentication_PollAuthSessionStatus_Response> =
) session
.await?; .call_service_method(
"Authentication.PollAuthSessionStatus#1".to_string(),
request,
)
.await
.expect("Failed to call PollAuthSessionStatus");
log::debug!("Got auth poll status {:#?}", response.body); log::debug!("Got auth poll status {:#?}", response.body);
if let Some(access_token) = response.body.refresh_token { if let Some(refresh_token) = response.body.refresh_token {
let account_name = response.body.account_name.unwrap_or_default(); let account_name = response.body.account_name.unwrap_or_default();
break (access_token, account_name); break (refresh_token, account_name);
}
if let Some(new_url) = response.body.new_challenge_url {
let code = qrcode::QrCode::new(new_url).expect("Failed to create QR Code");
log::info!(
"Got new QR code:\n{}",
code.render::<qrcode::render::unicode::Dense1x2>().build()
)
};
} }
});
if let Some(new_url) = response.body.new_challenge_url { let (refresh_token, account_name) = poll_handle.await?;
let code = qrcode::QrCode::new(new_url)?;
log::info!(
"Got new QR code:\n{}",
code.render::<qrcode::render::unicode::Dense1x2>().build()
)
};
};
log::debug!( log::debug!(
"Got account name {}, access token {}", "Got account name {}, access token {}",