✨ feat(message_list): add label support
- add labels to the message list - add label ids to the message list - add labels from command line 🐛 fix(message_list): fix error handling - use Result type alias instead of Error - remove unused Error import ♻️ refactor(message_list): improve code readability - rename add_labels to add_labels_ids - move add_labels to Labels struct
This commit is contained in:
committed by
Jeremiah Russell
parent
172f0ca1bc
commit
a114023ec8
@@ -1,5 +1,7 @@
|
|||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
|
use crate::{Labels, Result};
|
||||||
|
|
||||||
use google_gmail1::{
|
use google_gmail1::{
|
||||||
Gmail,
|
Gmail,
|
||||||
api::ListMessagesResponse,
|
api::ListMessagesResponse,
|
||||||
@@ -15,7 +17,7 @@ mod message_summary;
|
|||||||
|
|
||||||
use message_summary::MessageSummary;
|
use message_summary::MessageSummary;
|
||||||
|
|
||||||
use crate::{Credential, Error, utils::Elide};
|
use crate::{Credential, utils::Elide};
|
||||||
|
|
||||||
/// Default for the maximum number of results to return on a page
|
/// Default for the maximum number of results to return on a page
|
||||||
pub const DEFAULT_MAX_RESULTS: &str = "10";
|
pub const DEFAULT_MAX_RESULTS: &str = "10";
|
||||||
@@ -42,7 +44,7 @@ impl Debug for MessageList {
|
|||||||
|
|
||||||
impl MessageList {
|
impl MessageList {
|
||||||
/// Create a new List struct and add the Gmail api connection.
|
/// Create a new List struct and add the Gmail api connection.
|
||||||
pub async fn new(credential: &str) -> Result<Self, Error> {
|
pub async fn new(credential: &str) -> Result<Self> {
|
||||||
let (config_dir, secret) = {
|
let (config_dir, secret) = {
|
||||||
let config_dir = crate::utils::assure_config_dir_exists("~/.cull-gmail")?;
|
let config_dir = crate::utils::assure_config_dir_exists("~/.cull-gmail")?;
|
||||||
|
|
||||||
@@ -90,7 +92,24 @@ impl MessageList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Add label to the labels collection
|
/// Add label to the labels collection
|
||||||
pub fn add_labels(&mut self, label_ids: &[String]) {
|
pub async fn add_labels(&mut self, credential_file: &str, labels: &[String]) -> Result<()> {
|
||||||
|
// add labels if any specified
|
||||||
|
let label_list = Labels::new(credential_file, false).await?;
|
||||||
|
|
||||||
|
log::trace!("labels found and setup {label_list:#?}");
|
||||||
|
log::debug!("labels from command line: {labels:?}");
|
||||||
|
let mut label_ids = Vec::new();
|
||||||
|
for label in labels {
|
||||||
|
if let Some(id) = label_list.get_label_id(label) {
|
||||||
|
label_ids.push(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.add_labels_ids(label_ids.as_slice());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add label to the labels collection
|
||||||
|
fn add_labels_ids(&mut self, label_ids: &[String]) {
|
||||||
if !label_ids.is_empty() {
|
if !label_ids.is_empty() {
|
||||||
for id in label_ids {
|
for id in label_ids {
|
||||||
self.label_ids.push(id.to_string())
|
self.label_ids.push(id.to_string())
|
||||||
@@ -128,7 +147,7 @@ impl MessageList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Run the Gmail api as configured
|
/// Run the Gmail api as configured
|
||||||
pub async fn run(&mut self, pages: u32) -> Result<(), Error> {
|
pub async fn run(&mut self, pages: u32) -> Result<()> {
|
||||||
let list = self.messages_list(None).await?;
|
let list = self.messages_list(None).await?;
|
||||||
match pages {
|
match pages {
|
||||||
1 => {}
|
1 => {}
|
||||||
@@ -168,7 +187,7 @@ impl MessageList {
|
|||||||
async fn messages_list(
|
async fn messages_list(
|
||||||
&mut self,
|
&mut self,
|
||||||
next_page_token: Option<String>,
|
next_page_token: Option<String>,
|
||||||
) -> Result<ListMessagesResponse, Error> {
|
) -> Result<ListMessagesResponse> {
|
||||||
let mut call = self
|
let mut call = self
|
||||||
.hub
|
.hub
|
||||||
.users()
|
.users()
|
||||||
@@ -215,7 +234,7 @@ impl MessageList {
|
|||||||
Ok(list)
|
Ok(list)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn log_message_subjects(&mut self) -> Result<(), Error> {
|
async fn log_message_subjects(&mut self) -> Result<()> {
|
||||||
for message in &mut self.messages {
|
for message in &mut self.messages {
|
||||||
log::trace!("{}", message.id());
|
log::trace!("{}", message.id());
|
||||||
let (_res, m) = self
|
let (_res, m) = self
|
||||||
|
|||||||
Reference in New Issue
Block a user