1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
}
}
*/
|