mirror of
https://github.com/rustmailer/bichon.git
synced 2026-08-03 07:48:34 +02:00
chore: remove bb8 pool for IMAP; create a new session per operation to avoid stale connections
This commit is contained in:
+22
-37
@@ -18,12 +18,14 @@
|
||||
|
||||
use crate::modules::account::migration::{AccountModel, AccountType};
|
||||
use crate::modules::cache::imap::mailbox::{Attribute, AttributeEnum, MailBox};
|
||||
use crate::modules::context::executors::MAIL_CONTEXT;
|
||||
use crate::modules::error::code::ErrorCode;
|
||||
use crate::modules::error::{BichonError, BichonResult};
|
||||
use crate::modules::error::BichonResult;
|
||||
use crate::modules::imap::executor::ImapExecutor;
|
||||
use crate::modules::imap::session::SessionStream;
|
||||
use crate::modules::utils::create_hash;
|
||||
use crate::raise_error;
|
||||
use async_imap::types::Name;
|
||||
use async_imap::Session;
|
||||
|
||||
pub async fn get_account_mailboxes(account_id: u64, remote: bool) -> BichonResult<Vec<MailBox>> {
|
||||
let account = AccountModel::check_account_exists(account_id).await?;
|
||||
@@ -42,9 +44,11 @@ pub async fn get_account_mailboxes(account_id: u64, remote: bool) -> BichonResul
|
||||
}
|
||||
|
||||
pub async fn request_imap_all_mailbox_list(account_id: u64) -> BichonResult<Vec<MailBox>> {
|
||||
let executor = MAIL_CONTEXT.imap(account_id).await?;
|
||||
let names = executor.list_all_mailboxes().await?;
|
||||
convert_names_to_mailboxes(account_id, names.iter()).await
|
||||
let mut session = ImapExecutor::create_connection(account_id).await?;
|
||||
let names = ImapExecutor::list_all_mailboxes(&mut session).await?;
|
||||
let result = convert_names_to_mailboxes(account_id, &mut session, names.iter()).await?;
|
||||
session.logout().await.ok();
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn contains_no_select(attributes: &[Attribute]) -> bool {
|
||||
@@ -55,50 +59,31 @@ fn contains_no_select(attributes: &[Attribute]) -> bool {
|
||||
|
||||
pub async fn convert_names_to_mailboxes(
|
||||
account_id: u64,
|
||||
session: &mut Session<Box<dyn SessionStream>>,
|
||||
names: impl IntoIterator<Item = &Name>,
|
||||
) -> BichonResult<Vec<MailBox>> {
|
||||
// Preallocate enough space in the vector to avoid multiple reallocations
|
||||
let mut tasks = Vec::new();
|
||||
let mut mailboxes = Vec::new();
|
||||
|
||||
for name in names.into_iter() {
|
||||
// Convert the name into a MailBox structure
|
||||
for name in names {
|
||||
let mailbox_name = name.name().to_string();
|
||||
|
||||
let mut mailbox: MailBox = name.into();
|
||||
|
||||
tracing::debug!(
|
||||
raw = &mailbox_name,
|
||||
decoded = &mailbox.name,
|
||||
"mailbox name comparison"
|
||||
);
|
||||
|
||||
if contains_no_select(&mailbox.attributes) {
|
||||
continue;
|
||||
}
|
||||
|
||||
mailbox.account_id = account_id;
|
||||
mailbox.id = create_hash(account_id, &mailbox.name);
|
||||
let task: tokio::task::JoinHandle<Result<MailBox, BichonError>> =
|
||||
tokio::spawn(async move {
|
||||
let executor = MAIL_CONTEXT.imap(account_id).await?;
|
||||
let mx = executor.examine_mailbox(mailbox_name.as_str()).await?;
|
||||
// Update the mailbox status information
|
||||
mailbox.exists = mx.exists; // Number of messages in the mailbox
|
||||
mailbox.unseen = mx.unseen; // Number of unseen messages
|
||||
mailbox.uid_next = mx.uid_next; // Next unique identifier to be assigned
|
||||
mailbox.uid_validity = mx.uid_validity; // Validity of the UIDs
|
||||
Ok(mailbox)
|
||||
});
|
||||
tasks.push(task);
|
||||
}
|
||||
let mx = session
|
||||
.examine(mailbox_name.as_str())
|
||||
.await
|
||||
.map_err(|e| raise_error!(format!("{:#?}", e), ErrorCode::ImapCommandFailed))?;
|
||||
mailbox.exists = mx.exists;
|
||||
mailbox.unseen = mx.unseen;
|
||||
mailbox.uid_next = mx.uid_next;
|
||||
mailbox.uid_validity = mx.uid_validity;
|
||||
|
||||
let mut mailboxes = Vec::new();
|
||||
|
||||
for task in tasks {
|
||||
match task.await {
|
||||
Ok(Ok(mailbox)) => mailboxes.push(mailbox),
|
||||
Ok(Err(err)) => return Err(err), // Handle mailbox-level errors
|
||||
Err(e) => return Err(raise_error!(format!("{:#?}", e), ErrorCode::InternalError)), // Handle task-level panics or errors
|
||||
}
|
||||
mailboxes.push(mailbox);
|
||||
}
|
||||
|
||||
Ok(mailboxes)
|
||||
|
||||
Reference in New Issue
Block a user