diff options
Diffstat (limited to 'src/airdata.rs')
-rw-r--r-- | src/airdata.rs | 93 |
1 files changed, 93 insertions, 0 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 + } +} +*/ |