From adaf2b1e3020b8a40bea454473616c4e3077a59c Mon Sep 17 00:00:00 2001 From: Jeremiah Russell Date: Mon, 6 Oct 2025 17:02:53 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(message=5Flist):=20improve=20m?= =?UTF-8?q?essage=20handling=20and=20logging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - create MessageSummary struct to hold message id and subject - update MessageList to use MessageSummary instead of just message ids - implement log_message_subjects to fetch and log message subjects - add elide function to truncate long subjects for better logging --- src/message_list.rs | 48 +++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/message_list.rs b/src/message_list.rs index e326283..b2042a9 100644 --- a/src/message_list.rs +++ b/src/message_list.rs @@ -11,7 +11,11 @@ use google_gmail1::{ yup_oauth2::{ApplicationSecret, InstalledFlowAuthenticator, InstalledFlowReturnMethod}, }; -use crate::{Credential, Error}; +mod message_summary; + +use message_summary::MessageSummary; + +use crate::{Credential, Error, utils::Elide}; /// Default for the maximum number of results to return on a page pub const DEFAULT_MAX_RESULTS: &str = "10"; @@ -21,7 +25,7 @@ pub struct MessageList { hub: Gmail>, max_results: u32, label_ids: Vec, - message_ids: Vec, + messages: Vec, query: String, } @@ -30,7 +34,7 @@ impl Debug for MessageList { f.debug_struct("MessageList") .field("max_results", &self.max_results) .field("label_ids", &self.label_ids) - .field("message_ids", &self.message_ids) + .field("messages", &self.messages) .field("query", &self.query) .finish() } @@ -70,7 +74,7 @@ impl MessageList { hub: Gmail::new(client, auth), max_results: DEFAULT_MAX_RESULTS.parse::().unwrap(), label_ids: Vec::new(), - message_ids: Vec::new(), + messages: Vec::new(), query: String::new(), }) } @@ -99,9 +103,18 @@ impl MessageList { self.query = query.to_string() } + /// Get the summary of the messages + pub(crate) fn messages(&self) -> &Vec { + &self.messages + } + /// Get a reference to the message_ids pub fn message_ids(&self) -> Vec { - self.message_ids.clone() + self.messages + .iter() + .map(|m| m.id().to_string()) + .collect::>() + .clone() } /// Get a reference to the message_ids @@ -180,26 +193,35 @@ impl MessageList { } let (_response, list) = call.doit().await.map_err(Box::new)?; + log::trace!( + "Estimated {} messages.", + list.result_size_estimate.unwrap_or(0) + ); - let mut list_ids: Vec = list + if list.result_size_estimate.unwrap_or(0) == 0 { + log::warn!("Search returned no messages."); + return Ok(list); + } + + let mut list_ids = list .clone() .messages .unwrap() .iter() - .map(|item| item.id.clone().unwrap()) + .flat_map(|item| item.id.as_ref().map(|id| MessageSummary::new(id))) .collect(); - self.message_ids.append(&mut list_ids); + self.messages.append(&mut list_ids); Ok(list) } - async fn log_message_subjects(&self) -> Result<(), Error> { - for id in &self.message_ids { - log::trace!("{id}"); + async fn log_message_subjects(&mut self) -> Result<(), Error> { + for message in &mut self.messages { + log::trace!("{}", message.id()); let (_res, m) = self .hub .users() - .messages_get("me", id) + .messages_get("me", message.id()) .add_scope("https://www.googleapis.com/auth/gmail.metadata") .format("metadata") .add_metadata_headers("subject") @@ -228,6 +250,8 @@ impl MessageList { if subject.is_empty() { log::info!("***Email with no subject***"); } else { + subject.elide(24); + message.set_subject(&subject); log::info!("{subject:?}"); } }