forked from Lonami/Talaria
Inline native methods
The split no longer makes sense now that uniffi is the bridge.
This commit is contained in:
parent
d11a00d062
commit
1574ce683c
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
mod db;
|
mod db;
|
||||||
|
|
||||||
use grammers_client::types::{Dialog as OgDialog, LoginToken};
|
use grammers_client::types::LoginToken;
|
||||||
use grammers_client::{Client, Config, InitParams};
|
use grammers_client::{Client, Config, InitParams};
|
||||||
use grammers_session::{PackedChat, Session, UpdateState};
|
use grammers_session::{PackedChat, Session, UpdateState};
|
||||||
use grammers_tl_types as tl;
|
use grammers_tl_types as tl;
|
||||||
|
@ -104,7 +104,22 @@ fn block_on<F: Future>(future: F) -> F::Output {
|
||||||
RUNTIME.get().unwrap().block_on(future)
|
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::init_once(
|
||||||
android_logger::Config::default()
|
android_logger::Config::default()
|
||||||
.with_min_level(LOG_MIN_LEVEL)
|
.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,
|
session,
|
||||||
api_id: API_ID,
|
api_id: API_ID,
|
||||||
api_hash: API_HASH.to_string(),
|
api_hash: API_HASH.to_string(),
|
||||||
|
@ -167,8 +182,7 @@ async fn init_client() -> Result<()> {
|
||||||
},
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
})
|
}))
|
||||||
.await
|
|
||||||
.map_err(|_| NativeError::Network)?;
|
.map_err(|_| NativeError::Network)?;
|
||||||
|
|
||||||
info!("Connected!");
|
info!("Connected!");
|
||||||
|
@ -180,28 +194,23 @@ async fn init_client() -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn need_login() -> Result<bool> {
|
pub fn needLogin() -> Result<bool> {
|
||||||
let client = CLIENT.get().ok_or(NativeError::Initialization)?;
|
let client = CLIENT.get().ok_or(NativeError::Initialization)?;
|
||||||
client
|
block_on(client.is_authorized()).map_err(|_| NativeError::Network)
|
||||||
.is_authorized()
|
}
|
||||||
.await
|
|
||||||
|
pub fn requestLoginCode(phone: String) -> Result<u64> {
|
||||||
|
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)
|
.map_err(|_| NativeError::Network)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn request_login_code(phone: &str) -> Result<LoginToken> {
|
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)?;
|
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<()> {
|
block_on(client.sign_in(&token, &code)).map_err(|_| NativeError::Network)?;
|
||||||
let client = CLIENT.get().ok_or(NativeError::Initialization)?;
|
|
||||||
client
|
|
||||||
.sign_in(&token, &code)
|
|
||||||
.await
|
|
||||||
.map_err(|_| NativeError::Network)?;
|
|
||||||
|
|
||||||
let guard = DATABASE.lock().unwrap();
|
let guard = DATABASE.lock().unwrap();
|
||||||
let conn = match guard.as_ref() {
|
let conn = match guard.as_ref() {
|
||||||
|
@ -247,61 +256,18 @@ async fn sign_in(token: LoginToken, code: &str) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_dialogs() -> Result<Vec<OgDialog>> {
|
pub fn getDialogs() -> Result<Vec<Dialog>> {
|
||||||
let client = CLIENT.get().ok_or(NativeError::Initialization)?;
|
let client = CLIENT.get().ok_or(NativeError::Initialization)?;
|
||||||
|
|
||||||
|
block_on(async {
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
let mut dialogs = client.iter_dialogs();
|
let mut dialogs = client.iter_dialogs();
|
||||||
while let Some(dialog) = dialogs.next().await.map_err(|_| NativeError::Network)? {
|
while let Some(dialog) = dialogs.next().await.map_err(|_| NativeError::Network)? {
|
||||||
result.push(dialog);
|
result.push(dialog);
|
||||||
}
|
}
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
})
|
||||||
|
.map(|dialogs| {
|
||||||
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<bool> {
|
|
||||||
block_on(need_login())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn requestLoginCode(phone: String) -> Result<u64> {
|
|
||||||
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<Vec<Dialog>> {
|
|
||||||
block_on(get_dialogs()).map(|dialogs| {
|
|
||||||
dialogs
|
dialogs
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|d| Dialog {
|
.map(|d| Dialog {
|
||||||
|
@ -340,6 +306,8 @@ pub fn getDialogs() -> Result<Vec<Dialog>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sendMessage(packed: String, text: String) -> Result<()> {
|
pub fn sendMessage(packed: String, text: String) -> Result<()> {
|
||||||
let packed = PackedChat::from_hex(&packed).unwrap();
|
let chat = PackedChat::from_hex(&packed).unwrap();
|
||||||
block_on(send_message(packed, &text))
|
let client = CLIENT.get().ok_or(NativeError::Initialization)?;
|
||||||
|
block_on(client.send_message(chat, text)).map_err(|_| NativeError::Network)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue