From ceb4548649af9abb6a5394936bf4754b18d7337e Mon Sep 17 00:00:00 2001 From: Jeremiah Russell Date: Mon, 13 Oct 2025 12:48:18 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(message=5Flist):?= =?UTF-8?q?=20use=20gmail=20client=20for=20label=20retrieval?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - removes credential file and Labels struct from add_labels function - uses GmailClient to get label id - simplifies label adding process --- src/message_list.rs | 53 +++++++-------------------------------------- 1 file changed, 8 insertions(+), 45 deletions(-) diff --git a/src/message_list.rs b/src/message_list.rs index 0ba6fd8..9b4b31a 100644 --- a/src/message_list.rs +++ b/src/message_list.rs @@ -1,23 +1,17 @@ use std::fmt::Debug; -use crate::{Labels, Result}; +use crate::{GmailClient, Result}; use google_gmail1::{ - Gmail, - api::ListMessagesResponse, - hyper_rustls::{HttpsConnector, HttpsConnectorBuilder}, - hyper_util::{ - client::legacy::{Client, connect::HttpConnector}, - rt::TokioExecutor, - }, - yup_oauth2::{ApplicationSecret, InstalledFlowAuthenticator, InstalledFlowReturnMethod}, + Gmail, api::ListMessagesResponse, hyper_rustls::HttpsConnector, + hyper_util::client::legacy::connect::HttpConnector, }; mod message_summary; use message_summary::MessageSummary; -use crate::{Credential, utils::Elide}; +use crate::utils::Elide; /// Default for the maximum number of results to return on a page pub const DEFAULT_MAX_RESULTS: &str = "200"; @@ -44,36 +38,9 @@ impl Debug for MessageList { impl MessageList { /// Create a new List struct and add the Gmail api connection. - pub async fn new(credential: &str) -> Result { - let (config_dir, secret) = { - let config_dir = crate::utils::assure_config_dir_exists("~/.cull-gmail")?; - - let secret: ApplicationSecret = Credential::load_json_file(credential).into(); - (config_dir, secret) - }; - - let executor = TokioExecutor::new(); - let connector = HttpsConnectorBuilder::new() - .with_native_roots() - .unwrap() - .https_or_http() - .enable_http1() - .build(); - - let client = Client::builder(executor.clone()).build(connector.clone()); - - let auth = InstalledFlowAuthenticator::with_client( - secret, - InstalledFlowReturnMethod::HTTPRedirect, - Client::builder(executor).build(connector), - ) - .persist_tokens_to_disk(format!("{config_dir}/gmail1")) - .build() - .await - .unwrap(); - + pub async fn new(client: &GmailClient) -> Result { Ok(MessageList { - hub: Gmail::new(client, auth), + hub: client.hub(), max_results: DEFAULT_MAX_RESULTS.parse::().unwrap(), label_ids: Vec::new(), messages: Vec::new(), @@ -92,15 +59,11 @@ impl MessageList { } /// Add label to the labels collection - pub async fn add_labels(&mut self, credential_file: &str, labels: &[String]) -> Result<()> { - // add labels if any specified - let label_list = Labels::new(credential_file, false).await?; - - log::trace!("labels found and setup {label_list:#?}"); + pub async fn add_labels(&mut self, client: GmailClient, labels: &[String]) -> Result<()> { log::debug!("labels from command line: {labels:?}"); let mut label_ids = Vec::new(); for label in labels { - if let Some(id) = label_list.get_label_id(label) { + if let Some(id) = client.get_label_id(label) { label_ids.push(id) } }