Skip to content

Commit

Permalink
style: cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
itsusinn committed Nov 14, 2021
1 parent 5843391 commit dd6afa2
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 40 deletions.
11 changes: 6 additions & 5 deletions src/bot.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{ops::Deref, sync::Arc};

use arcstr::ArcStr;
use crate::config::CONFIG;
use mesagisto_client::{LateInit, cache::CACHE, res::RES};
use teloxide::{Bot, adaptors::AutoSend, net::Download, prelude::Requester, types::File as TgFile};
use arcstr::ArcStr;
use mesagisto_client::{cache::CACHE, res::RES, LateInit};
use teloxide::{adaptors::AutoSend, net::Download, prelude::Requester, types::File as TgFile, Bot};

#[derive(Singleton, Default)]
pub struct TgBot {
Expand All @@ -23,12 +23,13 @@ impl TgBot {
CACHE.put_file(uid, &tmp_path).await?;
Ok(())
}
pub fn get_url_by_path(&self,file_path: String) -> ArcStr{
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()
)
.into()
}
}
impl Deref for TgBot {
Expand Down
7 changes: 2 additions & 5 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub async fn answer(
Command::SetAddress { address } => {
let sender_id = cx.update.from().unwrap().id;
let chat_id = cx.chat_id();
let admins =cx.requester.get_chat_administrators(chat_id).await?;
let admins = cx.requester.get_chat_administrators(chat_id).await?;
let mut is_admin = false;
for admin in admins {
if admin.user.id == sender_id {
Expand All @@ -43,14 +43,11 @@ pub async fn answer(
}
}
if is_admin {
CONFIG
.target_address_mapper
.insert(chat_id, address.into());
CONFIG.target_address_mapper.insert(chat_id, address.into());
cx.answer("成功设置当前Group的信使地址").await?;
} else {
cx.answer("权限不足,拒绝设置信使频道").await?;
}

}
};
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pub struct Config {
}

impl Config {
pub fn mapper(&self,target: &i64) -> Option<ArcStr>{
pub fn mapper(&self, target: &i64) -> Option<ArcStr> {
match self.target_address_mapper.get(target) {
Some(v) => return Some(v.clone()),
None => return None,
Some(v) => return Some(v.clone()),
None => return None,
}
}
}
Expand Down Expand Up @@ -48,7 +48,7 @@ pub struct CipherConfig {
#[educe(Default = "this-is-an-example-key")]
pub key: String,
#[educe(Default = true)]
pub refuse_plain: bool
pub refuse_plain: bool,
}

#[basic_derive]
Expand Down
7 changes: 6 additions & 1 deletion src/despatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ impl TracingErrorHandler {
use futures::future::BoxFuture;
impl ErrorHandler<anyhow::Error> for TracingErrorHandler {
fn handle_error(self: Arc<Self>, error: anyhow::Error) -> BoxFuture<'static, ()> {
log::error!("{}:{}, \n Backtrace {}",self.text,error,error.backtrace());
log::error!(
"{}:{}, \n Backtrace {}",
self.text,
error,
error.backtrace()
);
Box::pin(async {})
}
}
23 changes: 11 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#![allow(incomplete_features)]
#![feature(backtrace,capture_disjoint_fields)]
#![feature(backtrace, capture_disjoint_fields)]

use std::sync::Arc;

use arcstr::ArcStr;
use futures::FutureExt;
use mesagisto_client::{OptionExt, cache::CACHE, cipher::CIPHER, db::DB, res::RES, server::SERVER};
use teloxide::{Bot, prelude::*};
use mesagisto_client::{cache::CACHE, cipher::CIPHER, db::DB, res::RES, server::SERVER, OptionExt};
use teloxide::{prelude::*, Bot};

use bot::TG_BOT;
use config::CONFIG;
Expand All @@ -22,10 +22,10 @@ mod bot;
mod command;
mod config;
mod despatch;
pub mod ext;
mod message;
mod net;
mod webhook;
pub mod ext;

fn main() {
std::env::set_var("RUST_BACKTRACE", "1");
Expand All @@ -39,7 +39,7 @@ fn main() {
.filter(Some("teloxide"), log::LevelFilter::Info)
.init();
tokio::runtime::Builder::new_multi_thread()
// fixme: how many do we need
// fixme: how many do we need
.worker_threads(5)
.enable_all()
.build()
Expand All @@ -59,7 +59,7 @@ async fn run() -> Result<(), anyhow::Error> {
}
CACHE.init();
if CONFIG.cipher.enable {
CIPHER.init(&CONFIG.cipher.key,&CONFIG.cipher.refuse_plain);
CIPHER.init(&CONFIG.cipher.key, &CONFIG.cipher.refuse_plain);
} else {
CIPHER.deinit();
}
Expand All @@ -71,14 +71,12 @@ async fn run() -> Result<(), anyhow::Error> {
async {
let file_path = TG_BOT.get_file(id_pair.1.as_str()).await.unwrap().file_path;
Ok(TG_BOT.get_url_by_path(file_path))
}.boxed()
}
.boxed()
});
SERVER.init(&CONFIG.nats.address).await;

let bot = Bot::with_client(
CONFIG.telegram.token.clone(),
net::client_from_config()
).auto_send();
let bot = Bot::with_client(CONFIG.telegram.token.clone(), net::client_from_config()).auto_send();

TG_BOT.init(Arc::new(bot));

Expand All @@ -87,7 +85,8 @@ async fn run() -> Result<(), anyhow::Error> {
&*CONFIG.telegram.bot_name,
command::answer,
message::handler::answer_msg,
).await;
)
.await;

CONFIG.save();
log::info!("Mesagisto Bot is going to shut down");
Expand Down
28 changes: 17 additions & 11 deletions src/message/handlers/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@ use crate::TG_BOT;
use crate::ext::DB;
use mesagisto_client::{
cache::CACHE,
data::{message::MessageType, message::Message,Packet},
data::{message::Message, message::MessageType, Packet},
};
use teloxide::payloads::SendMessageSetters;
use teloxide::prelude::Requester;

use teloxide::types::InputFile;

pub async fn receive_from_server(message: nats::asynk::Message, target: i64) -> anyhow::Result<()> {
log::trace!("Receive from {}",target);
log::trace!("Receive from {}", target);
let packet = Packet::from_cbor(&message.data)?;
match packet {
either::Left(msg) => {
handle_receive_message(msg,target).await?;
handle_receive_message(msg, target).await?;
}
either::Right(_) => {}
}
Ok(())
}

pub async fn handle_receive_message(mut message: Message, target: i64) -> anyhow::Result<()> {

for single in message.chain {
log::trace!("handling element in chain");
let sender_name = if message.profile.nick.is_some() {
Expand All @@ -39,24 +38,31 @@ pub async fn handle_receive_message(mut message: Message, target: i64) -> anyhow
let receipt = if let Some(reply_to) = &message.reply {
let local_id = DB.get_msg_id_1(&target, reply_to)?;
match local_id {
Some(local_id) => TG_BOT.send_message(target, content).reply_to_message_id(local_id).await?,
None => TG_BOT.send_message(target, content).await?
Some(local_id) => {
TG_BOT
.send_message(target, content)
.reply_to_message_id(local_id)
.await?
}
None => TG_BOT.send_message(target, content).await?,
}
} else {
TG_BOT.send_message(target, content).await?
};
DB.put_msg_id_1(&target, &message.id, &receipt.id)?;
},
MessageType::Image { id,url } => {
}
MessageType::Image { id, url } => {
let channel = CONFIG.mapper(&target).expect("Channel don't exist");
let path = CACHE.file(&id, &url, &channel).await?;
let receipt = TG_BOT.send_message(target, format!("{} :",sender_name)).await?;
DB.put_msg_id_ir_2(&target, &receipt.id,&message.id)?;
let receipt = TG_BOT
.send_message(target, format!("{} :", sender_name))
.await?;
DB.put_msg_id_ir_2(&target, &receipt.id, &message.id)?;
let receipt = TG_BOT.send_photo(target, InputFile::File(path)).await?;
DB.put_msg_id_1(&target, &message.id, &receipt.id)?;
}
}
}

Ok(())
}
}
4 changes: 2 additions & 2 deletions src/message/handlers/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ pub async fn answer_common(cx: Arc<Cx>) -> anyhow::Result<()> {
return Ok(());
}
let address = CONFIG.target_address_mapper.get(&target).unwrap().clone();
let sender = match cx.update.from(){
let sender = match cx.update.from() {
Some(v) => v,
//fixme
None => return Ok(())
None => return Ok(()),
};
// let avatar = bot_client().get_user_profile_photos(sender.id).await?;
let profile = Profile {
Expand Down

0 comments on commit dd6afa2

Please sign in to comment.