From dbdea741356b93aa1e70c8da2aa05a9c9f1dffee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Itsusinn=E9=80=B8=E6=96=B0?= Date: Sun, 14 Jan 2024 02:03:46 +0800 Subject: [PATCH] build: feature(impl_trait_in_assoc_type) --- src/bot.rs | 354 +++++++++++++++++++++---------------------- src/config.rs | 5 +- src/ext/mod.rs | 6 +- src/handlers/send.rs | 8 +- src/log.rs | 7 +- src/main.rs | 1 + 6 files changed, 191 insertions(+), 190 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index 903779b..a2f064d 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,177 +1,177 @@ -use std::ops::Deref; - -use arcstr::ArcStr; -use color_eyre::eyre::Result; -use lateinit::LateInit; -use mesagisto_client::{net::NET, res::RES}; -use teloxide::{ - adaptors::DefaultParseMode, - payloads::{SendAnimationSetters, SendMessageSetters, SendPhotoSetters}, - prelude::Requester, - types::{File as TgFile, InputFile, Message, MessageId}, - utils::command::BotCommands, - Bot, -}; -use teloxide_core::types::ChatId; -use tracing::{instrument, warn}; - -use crate::{ - commands::{bind::BindCommand, manage::ManageCommand}, - config::CONFIG, -}; -pub type BotRequester = DefaultParseMode; - -#[derive(Singleton, Default, Debug)] -pub struct TgBot { - inner: LateInit, -} -impl TgBot { - pub async fn init(&self, bot: BotRequester) -> Result<()> { - let mut commands = Vec::new(); - commands.append(&mut BindCommand::bot_commands()); - commands.append(&mut ManageCommand::bot_commands()); - bot.set_my_commands(commands).await?; - self.inner.init(bot); - Ok(()) - } - - // fixme use this-error - pub async fn file(&self, uid: &Vec, id: &Vec) -> Result<()> { - let id_str: ArcStr = base64_url::encode(id).into(); - let TgFile { path, .. } = self - .get_file(String::from_utf8_lossy(id)) - .await - .expect("获取文件失败"); - let tmp_path = RES.tmp_path(&id_str); - let url = self.get_url_by_path(path); - NET.download(&url, &tmp_path).await?; - RES.put_file(uid, &tmp_path).await?; - Ok(()) - } - - pub fn get_url_by_path(&self, file_path: String) -> ArcStr { - format!( - "https://api.telegram.org/file/bot{token}/{file}", - token = CONFIG.telegram.token, - file = file_path - ) - .into() - } - - #[instrument(skip(self))] - pub async fn send_text( - &self, - chat_id: ChatId, - text: T, - reply: Option, - ) -> Result - where - T: Into + Clone + std::fmt::Debug, - { - let send = self.inner.send_message(chat_id, text.clone()); - let send = if let Some(reply) = reply { - send.reply_to_message_id(MessageId(reply)) - } else { - send - }; - match send.await { - Ok(ok) => Ok(ok), - Err(e) => match e { - teloxide::RequestError::MigrateToChatId(new_id) => { - let target = chat_id.0; - warn!("Chat migrated from {} to {}", target, new_id); - if CONFIG.migrate_chat(&target, &new_id) { - let send = TG_BOT.send_message(ChatId(new_id), text.clone()); - let receipt = if let Some(reply) = reply { - send.reply_to_message_id(MessageId(reply)).await? - } else { - send.await? - }; - Ok(receipt) - } else { - Err(e.into()) - } - } - _ => Err(e.into()), - }, - } - } - - #[instrument(skip(self))] - pub async fn send_image( - &self, - chat_id: ChatId, - image_path: &std::path::Path, - reply: Option, - ) -> Result { - let photo = InputFile::file(image_path); - let kind = infer::get_from_path(image_path) - .expect("file read failed when refering file type") - .expect("Unkown file type"); - let is_gif = "gif" == kind.extension(); - let is_webp = "webp" == kind.extension(); - let result = if is_gif { - let photo = photo.clone().file_name(format!( - "{:?}.gif", - image_path.file_name().expect("Wrong filename") - )); - - let send = self.inner.send_animation(chat_id, photo.clone()); - if let Some(reply) = reply { - send.reply_to_message_id(MessageId(reply)).await - } else { - send.await - } - } else { - if is_webp { - self.inner.send_sticker(chat_id, photo.clone()).await - } else { - let send = self.inner.send_photo(chat_id, photo.clone()); - if let Some(reply) = reply { - send.reply_to_message_id(MessageId(reply)).await - } else { - send.await - } - } - }; - - match result { - Ok(ok) => Ok(ok), - Err(e) => match e { - teloxide::RequestError::MigrateToChatId(new_id) => { - let target = chat_id.0; - warn!("Chat migrated from {} to {}", target, new_id); - if CONFIG.migrate_chat(&target, &new_id) { - let receipt: Message = if is_gif { - let send = self.inner.send_animation(chat_id, photo.clone()); - if let Some(reply) = reply { - send.reply_to_message_id(MessageId(reply)).await? - } else { - send.await? - } - } else { - let send = self.inner.send_photo(chat_id, photo.clone()); - if let Some(reply) = reply { - send.reply_to_message_id(MessageId(reply)).await? - } else { - send.await? - } - }; - Ok(receipt) - } else { - Err(e.into()) - } - } - _ => Err(e.into()), - }, - } - } -} - -impl Deref for TgBot { - type Target = BotRequester; - - fn deref(&self) -> &Self::Target { - &self.inner - } -} +use std::ops::Deref; + +use arcstr::ArcStr; +use color_eyre::eyre::Result; +use lateinit::LateInit; +use mesagisto_client::{net::NET, res::RES}; +use teloxide::{ + adaptors::DefaultParseMode, + payloads::{SendAnimationSetters, SendMessageSetters, SendPhotoSetters}, + prelude::Requester, + types::{File as TgFile, InputFile, Message, MessageId}, + utils::command::BotCommands, + Bot, +}; +use teloxide_core::types::ChatId; +use tracing::{instrument, warn}; + +use crate::{ + commands::{bind::BindCommand, manage::ManageCommand}, + config::CONFIG, +}; +pub type BotRequester = DefaultParseMode; + +#[derive(Singleton, Default, Debug)] +pub struct TgBot { + inner: LateInit, +} +impl TgBot { + pub async fn init(&self, bot: BotRequester) -> Result<()> { + let mut commands = Vec::new(); + commands.append(&mut BindCommand::bot_commands()); + commands.append(&mut ManageCommand::bot_commands()); + bot.set_my_commands(commands).await?; + self.inner.init(bot); + Ok(()) + } + + // fixme use this-error + pub async fn file(&self, uid: &Vec, id: &Vec) -> Result<()> { + let id_str: ArcStr = base64_url::encode(id).into(); + let TgFile { path, .. } = self + .get_file(String::from_utf8_lossy(id)) + .await + .expect("获取文件失败"); + let tmp_path = RES.tmp_path(&id_str); + let url = self.get_url_by_path(path); + NET.download(&url, &tmp_path).await?; + RES.put_file(uid, &tmp_path).await?; + Ok(()) + } + + pub fn get_url_by_path(&self, file_path: String) -> ArcStr { + format!( + "https://api.telegram.org/file/bot{token}/{file}", + token = CONFIG.telegram.token, + file = file_path + ) + .into() + } + + #[instrument(skip(self))] + pub async fn send_text( + &self, + chat_id: ChatId, + text: T, + reply: Option, + ) -> Result + where + T: Into + Clone + std::fmt::Debug, + { + let send = self.inner.send_message(chat_id, text.clone()); + let send = if let Some(reply) = reply { + send.reply_to_message_id(MessageId(reply)) + } else { + send + }; + match send.await { + Ok(ok) => Ok(ok), + Err(e) => match e { + teloxide::RequestError::MigrateToChatId(new_id) => { + let target = chat_id.0; + warn!("Chat migrated from {} to {}", target, new_id); + if CONFIG.migrate_chat(&target, &new_id) { + let send = TG_BOT.send_message(ChatId(new_id), text.clone()); + let receipt = if let Some(reply) = reply { + send.reply_to_message_id(MessageId(reply)).await? + } else { + send.await? + }; + Ok(receipt) + } else { + Err(e.into()) + } + } + _ => Err(e.into()), + }, + } + } + + #[instrument(skip(self))] + pub async fn send_image( + &self, + chat_id: ChatId, + image_path: &std::path::Path, + reply: Option, + ) -> Result { + let photo = InputFile::file(image_path); + let kind = infer::get_from_path(image_path) + .expect("file read failed when refering file type") + .expect("Unkown file type"); + let is_gif = "gif" == kind.extension(); + let is_webp = "webp" == kind.extension(); + let result = if is_gif { + let photo = photo.clone().file_name(format!( + "{:?}.gif", + image_path.file_name().expect("Wrong filename") + )); + + let send = self.inner.send_animation(chat_id, photo.clone()); + if let Some(reply) = reply { + send.reply_to_message_id(MessageId(reply)).await + } else { + send.await + } + } else { + if is_webp { + self.inner.send_sticker(chat_id, photo.clone()).await + } else { + let send = self.inner.send_photo(chat_id, photo.clone()); + if let Some(reply) = reply { + send.reply_to_message_id(MessageId(reply)).await + } else { + send.await + } + } + }; + + match result { + Ok(ok) => Ok(ok), + Err(e) => match e { + teloxide::RequestError::MigrateToChatId(new_id) => { + let target = chat_id.0; + warn!("Chat migrated from {} to {}", target, new_id); + if CONFIG.migrate_chat(&target, &new_id) { + let receipt: Message = if is_gif { + let send = self.inner.send_animation(chat_id, photo.clone()); + if let Some(reply) = reply { + send.reply_to_message_id(MessageId(reply)).await? + } else { + send.await? + } + } else { + let send = self.inner.send_photo(chat_id, photo.clone()); + if let Some(reply) = reply { + send.reply_to_message_id(MessageId(reply)).await? + } else { + send.await? + } + }; + Ok(receipt) + } else { + Err(e.into()) + } + } + _ => Err(e.into()), + }, + } + } +} + +impl Deref for TgBot { + type Target = BotRequester; + + fn deref(&self) -> &Self::Target { + &self.inner + } +} diff --git a/src/config.rs b/src/config.rs index 85ed5e8..72e2652 100644 --- a/src/config.rs +++ b/src/config.rs @@ -53,9 +53,8 @@ impl Config { pub fn migrate(&self) { self - .centers - .insert("mesagisto".into(), "wss://builtin".into()); - + .centers + .insert("mesagisto".into(), "wss://builtin".into()); } pub fn migrate_chat(&self, old_chat_id: &i64, new_chat_id: &i64) -> bool { diff --git a/src/ext/mod.rs b/src/ext/mod.rs index 088dfc5..ccf2a78 100644 --- a/src/ext/mod.rs +++ b/src/ext/mod.rs @@ -1,8 +1,4 @@ -use std::{ - fs::File, - io::BufReader, - path::PathBuf, -}; +use std::{fs::File, io::BufReader, path::PathBuf}; use image::ImageFormat; diff --git a/src/handlers/send.rs b/src/handlers/send.rs index bf1fe59..8ec40fb 100644 --- a/src/handlers/send.rs +++ b/src/handlers/send.rs @@ -61,7 +61,7 @@ pub async fn answer_common(msg: Message) -> Result<()> { } else if let Some(_v) = msg.audio() { // TODO } else if let Some(animation) = msg.animation() { - if let Some(mime_type) = animation.mime_type.as_ref() + if let Some(mime_type) = animation.mime_type.as_ref() && let mime::GIF = mime_type.subtype() { let file_id: Vec = animation.file.id.as_bytes().to_vec(); @@ -70,11 +70,11 @@ pub async fn answer_common(msg: Message) -> Result<()> { TG_BOT.file(&uid, &file_id).await?; chain.push(MessageType::Image { id: uid, url: None }) } - if let Some(mime_type) = animation.mime_type.as_ref() + if let Some(mime_type) = animation.mime_type.as_ref() && let mime::VIDEO = mime_type.type_() { - // TODO - // animation is video + // TODO + // animation is video } } if let Some(caption) = msg.caption() { diff --git a/src/log.rs b/src/log.rs index 39fba8d..810b4f2 100644 --- a/src/log.rs +++ b/src/log.rs @@ -28,7 +28,12 @@ pub(crate) async fn init() -> Result<()> { .with_target(true) .with_timer(tracing_subscriber::fmt::time::OffsetTime::new( time::UtcOffset::from_whole_seconds( - Local.timestamp_opt(0, 0).unwrap().offset().fix().local_minus_utc(), + Local + .timestamp_opt(0, 0) + .unwrap() + .offset() + .fix() + .local_minus_utc(), ) .unwrap_or(time::UtcOffset::UTC), time::macros::format_description!( diff --git a/src/main.rs b/src/main.rs index aa2a993..8d8728b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![feature(let_chains)] #![feature(trait_alias)] #![feature(type_alias_impl_trait)] +#![feature(impl_trait_in_assoc_type)] use std::ops::Deref;