Skip to content

Commit

Permalink
refactor: change Str into Vec<u8>
Browse files Browse the repository at this point in the history
  • Loading branch information
itsusinn committed Nov 14, 2021
1 parent dd6afa2 commit 96eaeee
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
10 changes: 7 additions & 3 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ impl TgBot {
self.inner.init(bot)
}
// fixme use this-error
pub async fn file(&self,uid:&ArcStr,id: &ArcStr) -> anyhow::Result<()>{
let TgFile{ file_path,.. } = self.get_file(id.as_str()).await.expect("failed to get file");
let tmp_path = RES.tmp_path(id);
pub async fn file(&self, uid: &Vec<u8>, id: &Vec<u8>) -> anyhow::Result<()> {
let id_str: ArcStr = base64_url::encode(id).into();
let TgFile { file_path, .. } = self
.get_file(String::from_utf8_lossy(id))
.await
.expect("failed to get file");
let tmp_path = RES.tmp_path(&id_str);
let mut file = tokio::fs::File::create(&tmp_path).await?;
// mention: this is stream
self.inner.download_file(&file_path, &mut file).await?;
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ async fn run() -> Result<(), anyhow::Error> {
RES.init().await;
RES.resolve_photo_url(|id_pair| {
async {
let file_path = TG_BOT.get_file(id_pair.1.as_str()).await.unwrap().file_path;
let file = String::from_utf8_lossy(&id_pair.1);
let file_path = TG_BOT.get_file(file).await.unwrap().file_path;
Ok(TG_BOT.get_url_by_path(file_path))
}
.boxed()
Expand Down
5 changes: 3 additions & 2 deletions src/message/handlers/receive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::ext::db::DbExt;
use crate::CONFIG;
use crate::TG_BOT;
use crate::ext::DB;
use mesagisto_client::db::DB;
use mesagisto_client::{
cache::CACHE,
data::{message::Message, message::MessageType, Packet},
Expand Down Expand Up @@ -30,7 +31,7 @@ pub async fn handle_receive_message(mut message: Message, target: i64) -> anyhow
} else if message.profile.username.is_some() {
message.profile.username.take().unwrap()
} else {
message.profile.id.to_string()
base64_url::encode(&message.profile.id)
};
match single {
MessageType::Text { content } => {
Expand Down
52 changes: 25 additions & 27 deletions src/message/handlers/send.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::config::CONFIG;
use crate::bot::TG_BOT;
use crate::config::CONFIG;
use crate::ext::db::DbExt;
use crate::message::handlers::receive::receive_from_server;
use crate::message::Cx;
use crate::ext::DB;
use arcstr::ArcStr;
use mesagisto_client::EitherExt;
use mesagisto_client::data::message::{MessageType, Profile};
use mesagisto_client::data::{message, Packet};
use mesagisto_client::server::SERVER;
use mesagisto_client::db::DB;
use mesagisto_client::res::RES;
use mesagisto_client::server::SERVER;
use mesagisto_client::EitherExt;
use std::sync::Arc;
use teloxide::prelude::*;

Expand Down Expand Up @@ -49,7 +49,7 @@ pub async fn answer_common(cx: Arc<Cx>) -> anyhow::Result<()> {
};
// let avatar = bot_client().get_user_profile_photos(sender.id).await?;
let profile = Profile {
id: sender.id,
id: sender.id.to_be_bytes().into(),
username: sender.username.clone(),
nick: Some(
sender
Expand All @@ -59,41 +59,39 @@ pub async fn answer_common(cx: Arc<Cx>) -> anyhow::Result<()> {
};
let mut chain = Vec::<MessageType>::new();
if let Some(text) = udp.text() {
chain.push(MessageType::Text{ content: text.to_string() });
chain.push(MessageType::Text {
content: text.to_string(),
});
} else if let Some(image) = udp.photo() {
let photo = image.last().unwrap();
let file_id:ArcStr = photo.file_id.to_owned().into();
let uid:ArcStr = photo.file_unique_id.to_owned().into();
RES.store_photo_id(&uid, &file_id);
TG_BOT.file(&uid,&file_id).await?;
chain.push(MessageType::Image {
id: uid,url:None
})
let file_id: Vec<u8> = photo.file_id.as_bytes().to_vec();
let uid: Vec<u8> = photo.file_unique_id.as_bytes().to_vec();
RES.put_image_id(&uid, file_id.clone());
TG_BOT.file(&uid, &file_id).await?;
chain.push(MessageType::Image { id: uid, url: None })
} else if let Some(sticker) = udp.sticker() {
let file_id:ArcStr = sticker.file_id.to_owned().into();
let uid:ArcStr = sticker.file_unique_id.to_owned().into();
RES.store_photo_id(&uid, &file_id);
TG_BOT.file(&uid,&file_id).await?;
chain.push(MessageType::Image {
id:uid,url:None
})
let file_id: Vec<u8> = sticker.file_id.as_bytes().to_vec();
let uid: Vec<u8> = sticker.file_unique_id.as_bytes().to_vec();
RES.put_image_id(&uid, file_id.clone());
TG_BOT.file(&uid, &file_id).await?;
chain.push(MessageType::Image { id: uid, url: None })
}

let reply = match udp.reply_to_message(){
let reply = match udp.reply_to_message() {
Some(v) => {
let local_id = v.id.to_be_bytes().to_vec();
DB.get_msg_id_2(&target, &local_id).unwrap_or(None)
},
None => None
}
None => None,
};
DB.put_msg_id_0(&udp.chat_id(),&udp.id, &udp.id)?;
DB.put_msg_id_0(&udp.chat_id(), &udp.id, &udp.id)?;
let message = message::Message {
profile,
id:udp.id.to_be_bytes().to_vec(),
id: udp.id.to_be_bytes().to_vec(),
chain,
reply,
};
let packet = Packet::encrypt_from(message.tl())?;
let packet = Packet::from(message.tl())?;

SERVER
.send_and_receive(target, address, packet, receive_from_server)
Expand Down

0 comments on commit 96eaeee

Please sign in to comment.