diff --git a/src/bot.rs b/src/bot.rs index a858a33..14dcda9 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -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, id: &Vec) -> 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?; diff --git a/src/main.rs b/src/main.rs index 64534c3..d11b257 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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() diff --git a/src/message/handlers/receive.rs b/src/message/handlers/receive.rs index 02dc31b..eae08e2 100644 --- a/src/message/handlers/receive.rs +++ b/src/message/handlers/receive.rs @@ -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}, @@ -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 } => { diff --git a/src/message/handlers/send.rs b/src/message/handlers/send.rs index 8dd4e7a..6c8a330 100644 --- a/src/message/handlers/send.rs +++ b/src/message/handlers/send.rs @@ -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::*; @@ -49,7 +49,7 @@ pub async fn answer_common(cx: Arc) -> 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 @@ -59,41 +59,39 @@ pub async fn answer_common(cx: Arc) -> anyhow::Result<()> { }; let mut chain = Vec::::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 = photo.file_id.as_bytes().to_vec(); + let uid: Vec = 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 = sticker.file_id.as_bytes().to_vec(); + let uid: Vec = 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)