From 061048797c21c471b6479d9d4b9c139bc78fe744 Mon Sep 17 00:00:00 2001 From: Jeremiah Russell Date: Thu, 9 Oct 2025 17:47:39 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(delete):=20implement=20batch?= =?UTF-8?q?=20delete=20functionality?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add `delete.rs` module for batch deleting messages in gmail - create `Delete` struct with methods for preparing and executing batch deletes - implement `batch_delete` method to delete messages using gmail api --- src/delete.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/delete.rs diff --git a/src/delete.rs b/src/delete.rs new file mode 100644 index 0000000..0af8bca --- /dev/null +++ b/src/delete.rs @@ -0,0 +1,67 @@ +use google_gmail1::api::BatchDeleteMessagesRequest; + +use crate::{MessageList, Result}; + +/// Struct for trashing messages +#[derive(Debug)] +pub struct Delete { + message_list: MessageList, +} + +impl Delete { + /// Create a new Delete struct + pub async fn new(credential: &str) -> Result { + let message_list = MessageList::new(credential).await?; + Ok(Delete { message_list }) + } + + /// Set the maximum results + pub fn set_max_results(&mut self, value: u32) { + self.message_list.set_max_results(value); + } + + /// Report the maximum results value + pub fn max_results(&self) -> u32 { + self.message_list.max_results() + } + + /// Add label to the labels collection + pub async fn add_labels(&mut self, credential: &str, labels: &[String]) -> Result<()> { + self.message_list.add_labels(credential, labels).await + } + + /// Set the query string + pub fn set_query(&mut self, query: &str) { + self.message_list.set_query(query) + } + + /// Run the trash cli + pub async fn prepare(&mut self, pages: u32) -> Result<()> { + self.message_list.run(pages).await + } + + /// Run the batch delete on the selected messages + pub async fn batch_delete(&self) -> Result<()> { + let ids = Some(self.message_list.message_ids()); + + let batch_request = BatchDeleteMessagesRequest { ids }; + + log::trace!("{batch_request:#?}"); + + let _res = self + .message_list + .hub() + .users() + .messages_batch_delete(batch_request, "me") + .add_scope("https://mail.google.com/") + .doit() + .await + .map_err(Box::new)?; + + for m in self.message_list.messages() { + log::info!("Message with subject `{}` deleted.", m.subject()); + } + + Ok(()) + } +}