Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tuta-sudipg authored and jomapp committed Oct 29, 2024
1 parent ca6fede commit 0fa0e0f
Show file tree
Hide file tree
Showing 26 changed files with 1,496 additions and 785 deletions.
7 changes: 5 additions & 2 deletions ipc-schema/facades/MailImportFacade.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@
"unencryptedTutaCredentials": "UnencryptedCredentials"
},
{
"filePaths": "List<string>"
"targetOwnerGroup": "string"
},
{
"targetFolder": "List<string>"
},
{
"targetFolderId": "string"
"filePaths": "List<string>"
}
],
"ret": "string"
Expand Down
2 changes: 1 addition & 1 deletion ipc-schema/types/TutaCredentials.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "TutaCredentials",
"type": "typeref",
"location": {
"typescript": "../packages/node-mimimi/dist/binding.js"
"typescript": "../packages/node-mimimi/dist/binding.cjs"
}
}
71 changes: 31 additions & 40 deletions packages/node-mimimi/src/importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ use crate::importer::imap_reader::ImapImportConfig;
use crate::importer::importable_mail::ImportableMail;
use std::sync::Arc;
use tutasdk::crypto::aes::Iv;
use tutasdk::crypto::key::{GenericAesKey, VersionedAesKey};
use tutasdk::crypto::key::GenericAesKey;
use tutasdk::crypto::randomizer_facade::RandomizerFacade;
use tutasdk::entities::tutanota::{ImportMailData, ImportMailPostIn, ImportMailPostOut};
use tutasdk::generated_id::GeneratedId;
use tutasdk::services::tutanota::ImportMailService;
use tutasdk::services::ExtraServiceParams;
use tutasdk::{ApiCallError, IdTuple, LoggedInSdk};
use tutasdk::{IdTuple, LoggedInSdk};

pub type NapiTokioMutex<T> = napi::tokio::sync::Mutex<T>;

Expand Down Expand Up @@ -55,7 +56,7 @@ pub struct ImportStatus {
struct ImporterInner {
status: ImportStatus,
logged_in_sdk: Arc<LoggedInSdk>,
target_address: String,
target_owner_group: GeneratedId,
target_mail_folder: IdTuple,
import_source: ImportSource,
randomizer_facade: RandomizerFacade,
Expand Down Expand Up @@ -109,7 +110,7 @@ impl ImporterInner {
};

match import_res {
// this import have been success,
// this import has been success,
Ok(_imported_mail_response) => success_import_count += 1,

Err(_) => {
Expand Down Expand Up @@ -150,23 +151,17 @@ impl ImporterInner {
)
.unwrap();
let mail_group_key = self
.get_mail_group_key(self.target_address.as_str())
.await
.unwrap();
let owner_enc_session_key =
mail_group_key.encrypt_key(&new_aes_256_key, Iv::generate(&self.randomizer_facade));

let owner_group = self
.logged_in_sdk
.mail_facade()
.get_group_id_for_mail_address(self.target_address.as_str())
.get_current_sym_group_key(&self.target_owner_group)
.await
.map_err(|e| ())?;
let owner_enc_session_key =
mail_group_key.encrypt_key(&new_aes_256_key, Iv::generate(&self.randomizer_facade));

let import_mail_data = ImportMailData::from(importable_mail);
let import_mail_post_in = ImportMailPostIn {
ownerEncSessionKey: owner_enc_session_key.object,
ownerGroup: owner_group,
ownerGroup: self.target_owner_group.clone(),
ownerKeyVersion: owner_enc_session_key.version,
mailData: import_mail_data,
targetMailFolder: self.target_mail_folder.clone(),
Expand All @@ -189,34 +184,18 @@ impl ImporterInner {

Ok(import_mail_post_out)
}

async fn get_mail_group_key(
&self,
mail_address: &str,
) -> Result<VersionedAesKey, ApiCallError> {
let sender_mail_group_id = self
.logged_in_sdk
.mail_facade()
.get_group_id_for_mail_address(mail_address)
.await?;
let mail_group_key = self
.logged_in_sdk
.get_current_sym_group_key(&sender_mail_group_id)
.await?;
Ok(mail_group_key)
}
}

impl Importer {
pub fn new(
logged_in_sdk: Arc<LoggedInSdk>,
target_address: String,
target_owner_group: GeneratedId,
target_mail_folder: IdTuple,
import_source: ImportSource,
) -> Self {
let import_inner = ImporterInner {
logged_in_sdk,
target_address,
target_owner_group,
target_mail_folder,
import_source,
status: ImportStatus::default(),
Expand Down Expand Up @@ -330,13 +309,19 @@ mod tests {
imap_import_client: ImapImport::new(imap_import_config),
};
let randomizer_facade = RandomizerFacade::from_core(rand::rngs::OsRng);
let target_mail_folder_id = get_test_import_folder_id(&logged_in_sdk, MailSetKind::Archive)
let target_mail_folder = get_test_import_folder_id(&logged_in_sdk, MailSetKind::Archive)
.await
._id;

let target_owner_group = logged_in_sdk
.mail_facade()
.get_group_id_for_mail_address(importer_mail_address.as_str())
.await
.unwrap();

let importer = ImporterInner {
target_address: importer_mail_address,
target_mail_folder: target_mail_folder_id,
target_owner_group,
target_mail_folder,
logged_in_sdk,
import_source,
randomizer_facade,
Expand All @@ -356,17 +341,23 @@ mod tests {
.unwrap();

let import_source = ImportSource::LocalFile {
fs_email_client: FileImport::new(source_paths),
fs_email_client: FileImport::new(source_paths).unwrap(),
};
let randomizer_facade = RandomizerFacade::from_core(rand::rngs::OsRng);
let target_mail_folder_id = get_test_import_folder_id(&logged_in_sdk, MailSetKind::Archive)
let target_mail_folder = get_test_import_folder_id(&logged_in_sdk, MailSetKind::Archive)
.await
._id;

let target_owner_group = logged_in_sdk
.mail_facade()
.get_group_id_for_mail_address("map-free@tutanota.de")
.await
.unwrap();

ImporterInner {
status: ImportStatus::default(),
target_address: "map-free@tutanota.de".to_string(),
target_mail_folder: target_mail_folder_id,
target_owner_group,
target_mail_folder,
logged_in_sdk,
import_source,
randomizer_facade,
Expand Down Expand Up @@ -411,7 +402,7 @@ mod tests {

#[tokio::test]
async fn can_import_single_eml_file() {
let mut importer = init_file_importer("./test/sample.eml".to_string()).await;
let mut importer = init_file_importer(vec!["./test/sample.eml".to_string()]).await;

let import_res = importer.continue_import().await.map_err(|_| ());
assert_eq!(
Expand Down
Loading

0 comments on commit 0fa0e0f

Please sign in to comment.