Skip to content

Commit

Permalink
resolve folder aliases from backend implems instead #95
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Jul 17, 2023
1 parent 4e43b97 commit 679007b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 84 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Made the code async using the tokio async runtime.
- On Linux, made the kernel keyring the default one (the one based on keyutils).

### Fixed

- Fixed the way folder aliases are resolved. In some case, aliases were resolved CLI side and lib side, which led to alias errors [sourcehut#95].

## [0.8.1] - 2023-06-15

### Added
Expand Down Expand Up @@ -744,3 +748,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[sourcehut#54]: https://todo.sr.ht/~soywod/pimalaya/54
[sourcehut#59]: https://todo.sr.ht/~soywod/pimalaya/59
[sourcehut#60]: https://todo.sr.ht/~soywod/pimalaya/60
[sourcehut#95]: https://todo.sr.ht/~soywod/pimalaya/95
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ indicatif = "0.17"
log = "0.4"
md5 = "0.7.0"
once_cell = "1.16.0"
pimalaya-email = { version = "=0.13.0", default-features = false }
pimalaya-email = { git = "https://git.sr.ht/~soywod/pimalaya", default-features = false }
pimalaya-keyring = "=0.0.5"
pimalaya-oauth2 = "=0.0.3"
pimalaya-process = "=0.0.5"
Expand Down
14 changes: 11 additions & 3 deletions src/cache/id_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use log::{debug, trace};
use pimalaya_email::backend::ImapBackend;
#[cfg(feature = "notmuch-backend")]
use pimalaya_email::backend::NotmuchBackend;
use pimalaya_email::backend::{Backend, MaildirBackend};
use pimalaya_email::{
account::AccountConfig,
backend::{Backend, MaildirBackend},
};
use std::path::{Path, PathBuf};

const ID_MAPPER_DB_FILE_NAME: &str = ".id-mapper.sqlite";
Expand Down Expand Up @@ -36,7 +39,11 @@ impl IdMapper {
db_path
}

pub fn new(backend: &dyn Backend, account: &str, folder: &str) -> Result<Self> {
pub fn new(
backend: &dyn Backend,
account_config: &AccountConfig,
folder: &str,
) -> Result<Self> {
#[cfg(feature = "imap-backend")]
if backend.as_any().is::<ImapBackend>() {
return Ok(IdMapper::Dummy);
Expand All @@ -53,7 +60,8 @@ impl IdMapper {
db_path = Self::find_closest_db_path(backend.path())
}

let digest = md5::compute(account.to_string() + folder);
let folder = account_config.get_folder_alias(folder)?;
let digest = md5::compute(account_config.name.clone() + &folder);
let table = format!("id_mapper_{digest:x}");
debug!("creating id mapper table {table} at {db_path:?}…");

Expand Down
19 changes: 0 additions & 19 deletions src/domain/email/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub async fn attachments<P: Printer>(
folder: &str,
ids: Vec<&str>,
) -> Result<()> {
let folder = config.folder_alias(folder)?;
let ids = id_mapper.get_ids(ids)?;
let ids = ids.iter().map(String::as_str).collect::<Vec<_>>();
let emails = backend.get_emails(&folder, ids.clone()).await?;
Expand Down Expand Up @@ -78,31 +77,26 @@ pub async fn attachments<P: Printer>(
}

pub async fn copy<P: Printer>(
config: &AccountConfig,
printer: &mut P,
id_mapper: &IdMapper,
backend: &mut dyn Backend,
from_folder: &str,
to_folder: &str,
ids: Vec<&str>,
) -> Result<()> {
let from_folder = config.folder_alias(from_folder)?;
let to_folder = config.folder_alias(to_folder)?;
let ids = id_mapper.get_ids(ids)?;
let ids = ids.iter().map(String::as_str).collect::<Vec<_>>();
backend.copy_emails(&from_folder, &to_folder, ids).await?;
printer.print("Email(s) successfully copied!")
}

pub async fn delete<P: Printer>(
config: &AccountConfig,
printer: &mut P,
id_mapper: &IdMapper,
backend: &mut dyn Backend,
folder: &str,
ids: Vec<&str>,
) -> Result<()> {
let folder = config.folder_alias(folder)?;
let ids = id_mapper.get_ids(ids)?;
let ids = ids.iter().map(String::as_str).collect::<Vec<_>>();
backend.delete_emails(&folder, ids).await?;
Expand All @@ -120,8 +114,6 @@ pub async fn forward<P: Printer>(
headers: Option<Vec<(&str, &str)>>,
body: Option<&str>,
) -> Result<()> {
let folder = config.folder_alias(folder)?;

let ids = id_mapper.get_ids([id])?;
let ids = ids.iter().map(String::as_str).collect::<Vec<_>>();

Expand Down Expand Up @@ -150,7 +142,6 @@ pub async fn list<P: Printer>(
page_size: Option<usize>,
page: usize,
) -> Result<()> {
let folder = config.folder_alias(folder)?;
let page_size = page_size.unwrap_or(config.email_listing_page_size());
debug!("page size: {}", page_size);

Expand Down Expand Up @@ -202,16 +193,13 @@ pub async fn mailto<P: Printer>(
}

pub async fn move_<P: Printer>(
config: &AccountConfig,
printer: &mut P,
id_mapper: &IdMapper,
backend: &mut dyn Backend,
from_folder: &str,
to_folder: &str,
ids: Vec<&str>,
) -> Result<()> {
let from_folder = config.folder_alias(from_folder)?;
let to_folder = config.folder_alias(to_folder)?;
let ids = id_mapper.get_ids(ids)?;
let ids = ids.iter().map(String::as_str).collect::<Vec<_>>();
backend.move_emails(&from_folder, &to_folder, ids).await?;
Expand All @@ -229,7 +217,6 @@ pub async fn read<P: Printer>(
raw: bool,
headers: Vec<&str>,
) -> Result<()> {
let folder = config.folder_alias(folder)?;
let ids = id_mapper.get_ids(ids)?;
let ids = ids.iter().map(String::as_str).collect::<Vec<_>>();
let emails = backend.get_emails(&folder, ids).await?;
Expand Down Expand Up @@ -275,8 +262,6 @@ pub async fn reply<P: Printer>(
headers: Option<Vec<(&str, &str)>>,
body: Option<&str>,
) -> Result<()> {
let folder = config.folder_alias(folder)?;

let ids = id_mapper.get_ids([id])?;
let ids = ids.iter().map(String::as_str).collect::<Vec<_>>();

Expand All @@ -300,14 +285,12 @@ pub async fn reply<P: Printer>(
}

pub async fn save<P: Printer>(
config: &AccountConfig,
printer: &mut P,
id_mapper: &IdMapper,
backend: &mut dyn Backend,
folder: &str,
raw_email: String,
) -> Result<()> {
let folder = config.folder_alias(folder)?;
let is_tty = atty::is(Stream::Stdin);
let is_json = printer.is_json();
let raw_email = if is_tty || is_json {
Expand Down Expand Up @@ -340,7 +323,6 @@ pub async fn search<P: Printer>(
page_size: Option<usize>,
page: usize,
) -> Result<()> {
let folder = config.folder_alias(folder)?;
let page_size = page_size.unwrap_or(config.email_listing_page_size());
let envelopes = Envelopes::from_backend(
config,
Expand Down Expand Up @@ -369,7 +351,6 @@ pub async fn sort<P: Printer>(
page_size: Option<usize>,
page: usize,
) -> Result<()> {
let folder = config.folder_alias(folder)?;
let page_size = page_size.unwrap_or(config.email_listing_page_size());
let envelopes = Envelopes::from_backend(
config,
Expand Down
Loading

0 comments on commit 679007b

Please sign in to comment.