1use std::collections::HashMap;
2
3use google_gmail1::{
4 Gmail,
5 api::{Label, ListLabelsResponse},
6 hyper_rustls::{HttpsConnector, HttpsConnectorBuilder},
7 hyper_util::{
8 client::legacy::{Client, connect::HttpConnector},
9 rt::TokioExecutor,
10 },
11 yup_oauth2::{ApplicationSecret, InstalledFlowAuthenticator, InstalledFlowReturnMethod},
12};
13
14use crate::{Credential, Error};
15
16pub const DEFAULT_MAX_RESULTS: &str = "10";
18
19pub struct Labels {
21 hub: Gmail<HttpsConnector<HttpConnector>>,
22 label_list: Vec<Label>,
23 label_map: HashMap<String, String>,
24}
25
26impl std::fmt::Debug for Labels {
27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28 f.debug_struct("Labels")
29 .field("label_list", &self.label_list)
30 .field("label_map", &self.label_map)
31 .finish()
32 }
33}
34
35impl Labels {
36 pub async fn new(credential: &str) -> Result<Self, Error> {
38 let (config_dir, secret) = {
39 let config_dir = crate::utils::assure_config_dir_exists("~/.cull-gmail")?;
40
41 let secret: ApplicationSecret = Credential::load_json_file(credential).into();
42 (config_dir, secret)
43 };
44
45 let executor = TokioExecutor::new();
46 let connector = HttpsConnectorBuilder::new()
47 .with_native_roots()
48 .unwrap()
49 .https_or_http()
50 .enable_http1()
51 .build();
52
53 let client = Client::builder(executor.clone()).build(connector.clone());
54
55 let auth = InstalledFlowAuthenticator::with_client(
56 secret,
57 InstalledFlowReturnMethod::HTTPRedirect,
58 Client::builder(executor).build(connector),
59 )
60 .persist_tokens_to_disk(format!("{config_dir}/gmail1"))
61 .build()
62 .await
63 .unwrap();
64
65 Ok(Labels {
66 hub: Gmail::new(client, auth),
67 label_list: Vec::new(),
68 label_map: HashMap::new(),
69 })
70 }
71
72 pub async fn get_labels(&mut self) -> Result<(), Error> {
74 let call = self.hub.users().labels_list("me");
75 let (_response, list) = call.doit().await.map_err(Box::new)?;
76
77 self.log_label_names(&list).await?;
78
79 if let Some(labels) = list.labels {
80 let mut label_map = HashMap::new();
81 for label in &labels {
82 if label.id.is_some() && label.name.is_some() {
83 let label = label.clone();
84 label_map.insert(label.name.unwrap(), label.id.unwrap());
85 }
86 }
87 self.label_list = labels;
88 self.label_map = label_map;
89 }
90
91 Ok(())
92 }
93
94 async fn log_label_names(&self, list: &ListLabelsResponse) -> Result<(), Error> {
95 if let Some(labels) = &list.labels {
96 for label in labels {
97 if let Some(name) = &label.name {
98 log::info!("{name}");
99 } else {
100 log::warn!("No name for label {:?}", label.id);
101 }
102 }
103 }
104
105 Ok(())
106 }
107
108 pub fn get_label_id(&self, name: &str) -> Option<String> {
111 self.label_map.get(name).cloned()
112 }
113}