♻️ 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:
committed by
Jeremiah Russell
parent
8094e46f0f
commit
33cf154791
@@ -2,7 +2,10 @@
|
||||
|
||||
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
|
||||
#[derive()]
|
||||
@@ -75,7 +78,7 @@ impl<'a> Processor<'a> {
|
||||
let mut messages_to_trash = Trash::new(&self.client).await?;
|
||||
messages_to_trash
|
||||
.message_list()
|
||||
.add_labels(&self.client, &[label.to_string()])
|
||||
.add_labels(&[label.to_string()])
|
||||
.await?;
|
||||
|
||||
if messages_to_trash.message_list().label_ids().is_empty() {
|
||||
@@ -100,29 +103,24 @@ impl<'a> Processor<'a> {
|
||||
}
|
||||
|
||||
/// Delete the messages
|
||||
pub async fn delete_messages(&self, label: &str) -> Result<()> {
|
||||
let mut messages_to_delete = Delete::new(&self.client).await?;
|
||||
pub async fn delete_messages(&mut self, label: &str) -> Result<()> {
|
||||
self.client.add_labels(&[label.to_string()]).await?;
|
||||
|
||||
messages_to_delete
|
||||
.message_list()
|
||||
.add_labels(&self.client, &[label.to_string()])
|
||||
.await?;
|
||||
|
||||
if messages_to_delete.message_list().label_ids().is_empty() {
|
||||
if self.client.label_ids().is_empty() {
|
||||
return Err(Error::LabelNotFoundInMailbox(label.to_string()));
|
||||
}
|
||||
|
||||
let Some(query) = self.rule.eol_query() else {
|
||||
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");
|
||||
messages_to_delete.prepare(0).await?;
|
||||
self.client.prepare(0).await?;
|
||||
if self.execute {
|
||||
log::info!("***executing final delete messages***");
|
||||
messages_to_delete.batch_delete().await
|
||||
self.client.batch_delete().await
|
||||
} else {
|
||||
log::warn!("Execution stopped for dry run");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user