feat(list): add label filtering capability

- allow users to filter messages by label
- implement add_label method to add label IDs to the filter
- integrate label filtering into the messages_list call
This commit is contained in:
Jeremiah Russell
2025-10-03 14:34:19 +01:00
committed by Jeremiah Russell
parent 2af35e7588
commit f1ae06bf74

View File

@@ -18,6 +18,7 @@ pub const DEFAULT_MAX_RESULTS: &str = "10";
pub struct List {
hub: Gmail<HttpsConnector<HttpConnector>>,
max_results: u32,
label_ids: Vec<String>,
}
impl List {
@@ -53,6 +54,7 @@ impl List {
Ok(List {
hub: Gmail::new(client, auth),
max_results: DEFAULT_MAX_RESULTS.parse::<u32>().unwrap(),
label_ids: Vec::new(),
})
}
@@ -66,6 +68,11 @@ impl List {
self.max_results
}
/// Add label to the labels collection
pub fn add_label(&mut self, label_id: &str) {
self.label_ids.push(label_id.to_string())
}
/// Run the Gmail api as configured
pub async fn run(&self, pages: u32) -> Result<(), Error> {
let log_estimate = |est: &Option<u32>| {
@@ -116,16 +123,24 @@ impl List {
&self,
next_page_token: Option<String>,
) -> Result<ListMessagesResponse, Error> {
let call = self
let mut call = self
.hub
.users()
.messages_list("me")
.max_results(self.max_results);
let call = if let Some(page_token) = next_page_token {
call.page_token(&page_token)
} else {
call
};
// Add any labels specified
if !self.label_ids.is_empty() {
log::debug!("Setting labels for list: {:#?}", self.label_ids);
for id in self.label_ids.as_slice() {
call = call.add_label_ids(id);
}
}
// Add a page token if it exists
if let Some(page_token) = next_page_token {
log::debug!("Setting token for next page.");
call = call.page_token(&page_token);
}
let (_response, list) = call.doit().await.map_err(Box::new)?;
Ok(list)