From 1574ce683c737d32547fb49f789a541fd0afec8c Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 27 Oct 2022 19:44:48 +0200 Subject: [PATCH] Inline native methods The split no longer makes sense now that uniffi is the bridge. --- native/src/lib.rs | 122 +++++++++++++++++----------------------------- 1 file changed, 45 insertions(+), 77 deletions(-) diff --git a/native/src/lib.rs b/native/src/lib.rs index 1b2f9b8..6efd16b 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -3,7 +3,7 @@ mod db; -use grammers_client::types::{Dialog as OgDialog, LoginToken}; +use grammers_client::types::LoginToken; use grammers_client::{Client, Config, InitParams}; use grammers_session::{PackedChat, Session, UpdateState}; use grammers_tl_types as tl; @@ -104,7 +104,22 @@ fn block_on(future: F) -> F::Output { RUNTIME.get().unwrap().block_on(future) } -async fn init_client() -> Result<()> { +pub fn initDatabase(path: String) -> Result<()> { + let mut guard = DATABASE.lock().unwrap(); + if guard.is_some() { + info!("Database is already initialized"); + } + + match db::init_connection(&path) { + Ok(conn) => { + *guard = Some(conn); + Ok(()) + } + Err(_) => Err(NativeError::Database), + } +} + +pub fn initClient() -> Result<()> { android_logger::init_once( android_logger::Config::default() .with_min_level(LOG_MIN_LEVEL) @@ -155,7 +170,7 @@ async fn init_client() -> Result<()> { } } - let client = Client::connect(Config { + let client = block_on(Client::connect(Config { session, api_id: API_ID, api_hash: API_HASH.to_string(), @@ -167,8 +182,7 @@ async fn init_client() -> Result<()> { }, ..Default::default() }, - }) - .await + })) .map_err(|_| NativeError::Network)?; info!("Connected!"); @@ -180,28 +194,23 @@ async fn init_client() -> Result<()> { Ok(()) } -async fn need_login() -> Result { +pub fn needLogin() -> Result { let client = CLIENT.get().ok_or(NativeError::Initialization)?; - client - .is_authorized() - .await + block_on(client.is_authorized()).map_err(|_| NativeError::Network) +} + +pub fn requestLoginCode(phone: String) -> Result { + let client = CLIENT.get().ok_or(NativeError::Initialization)?; + block_on(client.request_login_code(&phone, API_ID, API_HASH)) + .map(|token| Box::into_raw(Box::new(token)) as u64) .map_err(|_| NativeError::Network) } -async fn request_login_code(phone: &str) -> Result { +pub fn signIn(token_ptr: u64, code: String) -> Result<()> { + let token = unsafe { *Box::from_raw(token_ptr as *mut LoginToken) }; let client = CLIENT.get().ok_or(NativeError::Initialization)?; - client - .request_login_code(&phone, API_ID, API_HASH) - .await - .map_err(|_| NativeError::Network) -} -async fn sign_in(token: LoginToken, code: &str) -> Result<()> { - let client = CLIENT.get().ok_or(NativeError::Initialization)?; - client - .sign_in(&token, &code) - .await - .map_err(|_| NativeError::Network)?; + block_on(client.sign_in(&token, &code)).map_err(|_| NativeError::Network)?; let guard = DATABASE.lock().unwrap(); let conn = match guard.as_ref() { @@ -247,61 +256,18 @@ async fn sign_in(token: LoginToken, code: &str) -> Result<()> { Ok(()) } -async fn get_dialogs() -> Result> { - let client = CLIENT.get().ok_or(NativeError::Initialization)?; - - let mut result = Vec::new(); - let mut dialogs = client.iter_dialogs(); - while let Some(dialog) = dialogs.next().await.map_err(|_| NativeError::Network)? { - result.push(dialog); - } - Ok(result) -} - -async fn send_message(chat: PackedChat, text: &str) -> Result<()> { - let client = CLIENT.get().ok_or(NativeError::Initialization)?; - client - .send_message(chat, text) - .await - .map_err(|_| NativeError::Network)?; - Ok(()) -} - -pub fn initDatabase(path: String) -> Result<()> { - let mut guard = DATABASE.lock().unwrap(); - if guard.is_some() { - info!("Database is already initialized"); - } - - match db::init_connection(&path) { - Ok(conn) => { - *guard = Some(conn); - Ok(()) - } - Err(e) => Err(NativeError::Database), - } -} - -pub fn initClient() -> Result<()> { - block_on(init_client()) -} - -pub fn needLogin() -> Result { - block_on(need_login()) -} - -pub fn requestLoginCode(phone: String) -> Result { - block_on(request_login_code(&phone)).map(|token| Box::into_raw(Box::new(token)) as u64) -} - -pub fn signIn(token_ptr: u64, code: String) -> Result<()> { - let token = unsafe { *Box::from_raw(token_ptr as *mut LoginToken) }; - - block_on(sign_in(token, &code)) -} - pub fn getDialogs() -> Result> { - block_on(get_dialogs()).map(|dialogs| { + let client = CLIENT.get().ok_or(NativeError::Initialization)?; + + block_on(async { + let mut result = Vec::new(); + let mut dialogs = client.iter_dialogs(); + while let Some(dialog) = dialogs.next().await.map_err(|_| NativeError::Network)? { + result.push(dialog); + } + Ok(result) + }) + .map(|dialogs| { dialogs .into_iter() .map(|d| Dialog { @@ -340,6 +306,8 @@ pub fn getDialogs() -> Result> { } pub fn sendMessage(packed: String, text: String) -> Result<()> { - let packed = PackedChat::from_hex(&packed).unwrap(); - block_on(send_message(packed, &text)) + let chat = PackedChat::from_hex(&packed).unwrap(); + let client = CLIENT.get().ok_or(NativeError::Initialization)?; + block_on(client.send_message(chat, text)).map_err(|_| NativeError::Network)?; + Ok(()) }