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();
let poll_handle = tokio::spawn(async move {
let mut session = session_cloned;
loop {
interval.tick().await; interval.tick().await;
let request = CAuthentication_PollAuthSessionStatus_Request { let request = CAuthentication_PollAuthSessionStatus_Request {
client_id: response.body.client_id, client_id: response.body.client_id,
request_id: response.body.request_id.clone(), request_id: response.body.request_id.clone(),
..Default::default() ..Default::default()
}; };
let response: CMProtoBufMessage<CAuthentication_PollAuthSessionStatus_Response> = session let response: CMProtoBufMessage<CAuthentication_PollAuthSessionStatus_Response> =
session
.call_service_method( .call_service_method(
"Authentication.PollAuthSessionStatus#1".to_string(), "Authentication.PollAuthSessionStatus#1".to_string(),
request, request,
) )
.await?; .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 { if let Some(new_url) = response.body.new_challenge_url {
let code = qrcode::QrCode::new(new_url)?; let code = qrcode::QrCode::new(new_url).expect("Failed to create QR Code");
log::info!( log::info!(
"Got new QR code:\n{}", "Got new QR code:\n{}",
code.render::<qrcode::render::unicode::Dense1x2>().build() code.render::<qrcode::render::unicode::Dense1x2>().build()
) )
}; };
}; }
});
let (refresh_token, account_name) = poll_handle.await?;
log::debug!( log::debug!(
"Got account name {}, access token {}", "Got account name {}, access token {}",