✨ 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:
committed by
Jeremiah Russell
parent
2af35e7588
commit
f1ae06bf74
27
src/list.rs
27
src/list.rs
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user