♻️ refactor(processor): consolidate message operations in GmailClient

- move label adding and message deletion logic to GmailClient
- reduce code duplication and improve code structure
- simplify processor methods by reusing GmailClient functions
This commit is contained in:
Jeremiah Russell
2025-10-13 15:51:49 +01:00
committed by Jeremiah Russell
parent 8094e46f0f
commit 33cf154791

View File

@@ -2,7 +2,10 @@
use std::fmt; use std::fmt;
use crate::{Delete, EolAction, Error, GmailClient, Result, Trash, config::EolRule}; use crate::{
EolAction, Error, GmailClient, Result, Trash, config::EolRule, delete::Delete,
message_list::MessageList,
};
/// Rule processor /// Rule processor
#[derive()] #[derive()]
@@ -75,7 +78,7 @@ impl<'a> Processor<'a> {
let mut messages_to_trash = Trash::new(&self.client).await?; let mut messages_to_trash = Trash::new(&self.client).await?;
messages_to_trash messages_to_trash
.message_list() .message_list()
.add_labels(&self.client, &[label.to_string()]) .add_labels(&[label.to_string()])
.await?; .await?;
if messages_to_trash.message_list().label_ids().is_empty() { if messages_to_trash.message_list().label_ids().is_empty() {
@@ -100,29 +103,24 @@ impl<'a> Processor<'a> {
} }
/// Delete the messages /// Delete the messages
pub async fn delete_messages(&self, label: &str) -> Result<()> { pub async fn delete_messages(&mut self, label: &str) -> Result<()> {
let mut messages_to_delete = Delete::new(&self.client).await?; self.client.add_labels(&[label.to_string()]).await?;
messages_to_delete if self.client.label_ids().is_empty() {
.message_list()
.add_labels(&self.client, &[label.to_string()])
.await?;
if messages_to_delete.message_list().label_ids().is_empty() {
return Err(Error::LabelNotFoundInMailbox(label.to_string())); return Err(Error::LabelNotFoundInMailbox(label.to_string()));
} }
let Some(query) = self.rule.eol_query() else { let Some(query) = self.rule.eol_query() else {
return Err(Error::NoQueryStringCalculated(self.rule.id())); return Err(Error::NoQueryStringCalculated(self.rule.id()));
}; };
messages_to_delete.message_list().set_query(&query); self.client.set_query(&query);
log::info!("{messages_to_delete:?}"); log::info!("{:?}", self.client.messages());
log::info!("Ready to run"); log::info!("Ready to run");
messages_to_delete.prepare(0).await?; self.client.prepare(0).await?;
if self.execute { if self.execute {
log::info!("***executing final delete messages***"); log::info!("***executing final delete messages***");
messages_to_delete.batch_delete().await self.client.batch_delete().await
} else { } else {
log::warn!("Execution stopped for dry run"); log::warn!("Execution stopped for dry run");