summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorsyn <isaqtm@gmail.com>2021-01-16 02:52:10 +0300
committersyn <isaqtm@gmail.com>2021-01-16 02:52:10 +0300
commitfc559079043e649b10cef592718ffbf2025a7b5a (patch)
tree5ce8e093342a20e92f1c2d36b35351fb55777cd8 /src
parent094b3b3e4ff6df5eb084f5f232a42424b7a8432c (diff)
downloadairception-fc559079043e649b10cef592718ffbf2025a7b5a.tar.gz
[wip] redis wip
Diffstat (limited to 'src')
-rw-r--r--src/airdata.rs93
-rw-r--r--src/main.rs37
2 files changed, 120 insertions, 10 deletions
diff --git a/src/airdata.rs b/src/airdata.rs
new file mode 100644
index 0000000..3bdd0df
--- /dev/null
+++ b/src/airdata.rs
@@ -0,0 +1,93 @@
+use crate::error::AirceptionResult;
+//use crate::lossy::{LossyChatTitle, LossyUsername};
+//use paperplane::types;
+//use redis::AsyncCommands;
+
+#[derive(Clone)]
+pub struct AsyncData {
+ pub con_man: redis::aio::ConnectionManager,
+}
+
+impl AsyncData {
+ pub async fn new(con_info: redis::ConnectionInfo) -> AirceptionResult<Self> {
+ Ok(Self {
+ con_man: redis::aio::ConnectionManager::new(
+ redis::Client::open(con_info)?
+ ).await?
+ })
+ }
+}
+
+/*
+impl AsyncData {
+ fn user_key(id: i32) -> String {
+ format!("user.{}", id)
+ }
+ fn message_key(id: i64) -> String {
+ format!("message.{}", id)
+ }
+ fn chat_key(id: i64) -> String {
+ format!("chat.{}", id)
+ }
+ fn file_key(id: i32) -> String {
+ format!("file.{}", id)
+ }
+
+ async fn save_file(&self, file: types::File) -> AirceptionResult<()> {
+ let id = file.id;
+ self.set_entity(&Self::file_key(id), &file).await?;
+ Ok(())
+ }
+
+ async fn get_entity<T: serde::de::DeserializeOwned>(&self, key: &str) -> Option<T> {
+ self.con_man
+ .clone()
+ .get(key)
+ .await
+ .ok()
+ .and_then(|json: String| serde_json::from_str(json.as_ref()).ok())
+ }
+ pub async fn get_user(&self, id: i32) -> Option<types::User> {
+ self.get_entity(Self::user_key(id).as_ref()).await
+ }
+ pub async fn get_username_lossy(&self, id: i32) -> LossyUsername {
+ match self.get_user(id).await {
+ Some(user) => LossyUsername::from_user_ref(&user),
+ None => LossyUsername::Unknown,
+ }
+ }
+ pub async fn get_chat(&self, id: i64) -> Option<types::Chat> {
+ self.get_entity(Self::chat_key(id).as_ref()).await
+ }
+ pub async fn get_chat_title_lossy(&self, id: i64) -> LossyChatTitle {
+ match self.get_chat(id).await {
+ Some(chat) => LossyChatTitle::from_chat_ref(&chat),
+ None => LossyChatTitle::Unknown,
+ }
+ }
+ pub async fn get_msg(&self, id: i64) -> Option<types::Message> {
+ self.get_entity(Self::message_key(id).as_ref()).await
+ }
+
+ pub async fn set_entity<T: serde::ser::Serialize>(
+ &self,
+ key: &str,
+ ent: &T,
+ ) -> AirceptionResult<()> {
+ let json = serde_json::to_string(ent)?;
+ Ok(self.con_man.clone().set(key, json).await?)
+ }
+ pub async fn insert_user(&self, user: types::User) -> AirceptionResult<()> {
+ self.set_entity(Self::user_key(user.id).as_ref(), &user)
+ .await
+ }
+ pub async fn insert_message(&self, message: types::Message) -> AirceptionResult<()> {
+ self.set_entity(Self::message_key(message.id).as_ref(), &message)
+ .await
+ }
+ pub async fn insert_chat(&self, chat: types::Chat) -> AirceptionResult<()> {
+ self.set_entity(Self::chat_key(chat.id).as_ref(), &chat)
+ .await
+ }
+}
+*/
diff --git a/src/main.rs b/src/main.rs
index e90cacf..749578d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,6 @@
-// use redis::IntoConnectionInfo;
-// use std::env;
+use redis::IntoConnectionInfo;
-//mod airdata;
+mod airdata;
mod auth;
mod error;
mod client_ext;
@@ -11,24 +10,42 @@ use client_ext::ClientExt;
mod router;
use router::UpdateHandler;
-#[tokio::main(flavor = "multi_thread", worker_threads = 5)]
-async fn main() -> error::AirceptionResult<()> {
+fn main() {
+ let runtime = tokio::runtime::Builder::new_multi_thread()
+ .worker_threads(5)
+ .enable_io()
+ .build()
+ .unwrap();
+
+ runtime.block_on(async_main()).expect("Main errored");
+}
+
+async fn async_main() -> error::AirceptionResult<()> {
dotenv::dotenv().ok();
env_logger::init();
- //let tg_log: Option<i32> = env::var("TG_LOG").ok().and_then(|var| var.parse().ok());
- //let data = airdata::AsyncData::new("redis://127.0.0.1/".into_connection_info()?).await?;
+
+ let data = airdata::AsyncData::new("redis://127.0.0.1/".into_connection_info()?).await?;
#[allow(unused)]
- let tg = tdlib_rs::ClientBuilder::new(UpdateHandler {})
+ let tg = tdlib_rs::ClientBuilder::new(UpdateHandler(data))
.log_level(tdlib_rs::LogLevel::Warn)
//.log_file("tg.log")
.build();
std::thread::sleep(std::time::Duration::from_secs(1));
- //log::warn!("{:#?}", tg.get_me().await);
+ log::warn!("{:#?}", tg.get_me().await);
log::warn!("{:#?}", tg.get_network_statistics(true).await);
+ loop {
+ let auth_state = tg.get_authorization_state().await;
+ match auth_state {
+ Ok(client_ext::AuthorizationState::AuthorizationStateReady) => break,
+ _ => {}
+ };
+ log::info!("{:#?}", auth_state);
+ std::thread::sleep(std::time::Duration::from_secs(5));
+ }
/* sleep forever */
std::thread::park();
Ok(())
-}
+} \ No newline at end of file