Skip to content

Commit

Permalink
style: code clean
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsusinn committed Nov 17, 2022
1 parent 18b31d4 commit 926fe09
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 46 deletions.
37 changes: 19 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ color = ["tracing-subscriber/ansi"]

[dependencies]
# bot
teloxide = { version = "0.11.0", default-features = false, features = ["macros", "auto-send","ctrlc_handler","rustls","nightly", "webhooks-axum"] }
teloxide = { version = "0.11.1", default-features = false, features = ["macros", "auto-send","ctrlc_handler","rustls","nightly", "webhooks-axum"] }
teloxide-core = { version = "0.8.0", default-features = false, features = ["rustls", "cache_me"] }
teloxide-macros = "0.7.0"

Expand All @@ -25,48 +25,49 @@ singleton = { branch= "master", git = "https://github.com/Itsusinn/singleton-rs.
# singleton = { path = "/code/singleton-rs" }

# logging
tracing = "0.1.36"
tracing-subscriber = { version = "0.3.15", default-features = false, features = ["tracing-log", "std", "local-time","fmt"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", default-features = false, features = ["tracing-log", "std", "local-time","fmt"] }
colored = "2.0.0"
time = { version = "0.3.14", features = ["macros", "local-offset"] }
chrono = "0.4.22"
time = { version = "0.3.17", features = ["macros", "local-offset"] }
chrono = "0.4.23"

# serialization
serde = { version = "1.0.144", default-features = false, features = ["derive","rc"] }
serde_json = "1.0.85"
serde_yaml = "0.9.13"
serde = { version = "1.0.147", default-features = false, features = ["derive","rc"] }
serde_json = "1.0.87"
serde_yaml = "0.9.14"

# asynchronous
tokio = { version = "1.21.1", default-features = false, features = ["macros","signal","rt-multi-thread"] }
tokio-stream = "0.1.10"
futures-util = "0.3.24"
async-trait = "0.1.57"
reqwest = { version = "0.11.11", default-features = false, features = ["rustls","rustls-tls-native-roots","stream","multipart", "blocking"] }
tokio = { version = "1.21.2", default-features = false, features = ["macros","signal","rt-multi-thread"] }
tokio-stream = "0.1.11"
futures-util = "0.3.25"
async-trait = "0.1.58"
reqwest = { version = "0.11.13", default-features = false, features = ["rustls","rustls-tls-native-roots","stream","multipart", "blocking"] }

# error handling
thiserror = "1.0.35"
thiserror = "1.0.37"
color-eyre = "0.6.2"
tracing-error = "0.2.0"
color-spantrace = "0.2.0"

# encoding and decoding
base64-url = "1.4.13"
image = { version = "0.24.5", features = ["webp-encoder"] }
# hex = "0.4"

# crypto
aes-gcm = { version = "0.10.1", features = ["std"] }

# tools
dashmap = { version = "5.4.0", features = ["serde"] }
once_cell = "1.15.0"
educe = { version = "0.4.19", default-features = false, features = ["Default"] }
arcstr = { version = "1.1.4", features = ["serde"] }
once_cell = "1.16.0"
educe = { version = "0.4.20", default-features = false, features = ["Default"] }
arcstr = { version = "1.1.5", features = ["serde"] }
yaml-rust = "0.4.5"
linked-hash-map = "0.5.6"
either = "1.8.0"
mime = "0.3.16"
infer = "0.11.0"
uuid = "1.2.1"
uuid = "1.2.2"
# self_update = { path = "/home/itsusinn/Workspace/Clone/self_update", features = ["archive-zip","compression-zip-deflate","rustls"] }
self_update = { branch = "proxy", git = "https://github.com/Itsusinn/self_update.git", default-features = false, features = ["archive-zip","compression-zip-deflate","rustls"] }

Expand Down
18 changes: 11 additions & 7 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Deref;
use arcstr::ArcStr;
use color_eyre::eyre::Result;
use lateinit::LateInit;
use mesagisto_client::{cache::CACHE, net::NET, res::RES};
use mesagisto_client::{net::NET, res::RES};
use teloxide::{
adaptors::DefaultParseMode,
payloads::{SendAnimationSetters, SendMessageSetters, SendPhotoSetters},
Expand Down Expand Up @@ -45,7 +45,7 @@ impl TgBot {
let tmp_path = RES.tmp_path(&id_str);
let url = self.get_url_by_path(path);
NET.download(&url, &tmp_path).await?;
CACHE.put_file(uid, &tmp_path).await?;
RES.put_file(uid, &tmp_path).await?;
Ok(())
}

Expand Down Expand Up @@ -109,7 +109,7 @@ impl TgBot {
.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",
Expand All @@ -123,11 +123,15 @@ impl TgBot {
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
if is_webp {
self.inner.send_sticker(chat_id, photo.clone()).await
} else {
send.await
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
}
}
};

Expand Down
9 changes: 3 additions & 6 deletions src/commands/manage.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use color_eyre::eyre::Result;
use teloxide::{
prelude::*,
utils::command::BotCommands
};
use teloxide::{prelude::*, utils::command::BotCommands};

use crate::bot::BotRequester;

Expand All @@ -15,7 +12,7 @@ pub enum ManageCommand {
#[command(description = "Disaplay manage commands help")]
ManageHelp,
#[command(description = "Add a new WS Server", parse_with = "split")]
NewServer { name: String, address: String },
NewProfile { name: String, address: String },
}
impl ManageCommand {
pub async fn answer(msg: Message, bot: BotRequester, cmd: ManageCommand) -> Result<()> {
Expand All @@ -25,7 +22,7 @@ impl ManageCommand {
.send_message(msg.chat.id, ManageCommand::descriptions().to_string())
.await?;
}
ManageCommand::NewServer { name, address } => {}
ManageCommand::NewProfile { name, address } => {}
}
Ok(())
}
Expand Down
10 changes: 7 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ impl Config {
}

pub fn migrate(&self) {
self.centers.insert("mesagisto".into(), "wss://center.mesagisto.org".into());
if !self.centers.contains_key("mesagisto".into()) {
self
.centers
.insert("mesagisto".into(), "wss://center.mesagisto.org".into());
}
}

pub fn migrate_chat(&self, old_chat_id: &i64, new_chat_id: &i64) -> bool {
Expand Down Expand Up @@ -103,5 +107,5 @@ pub struct AutoUpdateConfig {
pub struct TlsConfig {
#[educe(Default = false)]
pub skip_verify: bool,
pub custom_cert: ArcStr
}
pub custom_cert: ArcStr,
}
9 changes: 5 additions & 4 deletions src/dispatch.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::fl;
use teloxide::prelude::*;

use crate::commands::manage::ManageCommand;
use crate::info;
use crate::{bot::BotRequester, commands::bind::BindCommand, handlers};
use crate::{
bot::BotRequester,
commands::{bind::BindCommand, manage::ManageCommand},
fl, handlers, info,
};

pub async fn start(bot: &BotRequester) {
let message_handler = Update::filter_message()
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub async fn answer_common(msg: Message) -> Result<()> {
id: msg.id.0.to_be_bytes().to_vec(),
chain,
reply,
from: target.to_be_bytes().to_vec()
from: target.to_be_bytes().to_vec(),
};
let room_id = SERVER.room_id(room_address);
let packet = Packet::new(room_id, message.tl())?;
Expand Down
5 changes: 2 additions & 3 deletions src/i18n.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use i18n_embed::{
fluent::{FluentLanguageLoader, fluent_language_loader},
DesktopLanguageRequester, LanguageLoader
fluent::{fluent_language_loader, FluentLanguageLoader},
DesktopLanguageRequester, LanguageLoader,
};

use once_cell::sync::Lazy;
use rust_embed::RustEmbed;

Expand Down
2 changes: 1 addition & 1 deletion src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) async fn init() -> Result<()> {
.with_target(true)
.with_timer(tracing_subscriber::fmt::time::OffsetTime::new(
time::UtcOffset::from_whole_seconds(
Local.timestamp(0, 0).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!(
Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![feature(let_chains)]
#![feature(trait_alias)]
#![feature(type_alias_impl_trait)]

use std::ops::Deref;

Expand All @@ -8,6 +10,7 @@ use futures_util::FutureExt;
use locale_config::Locale;
use mesagisto_client::{MesagistoConfig, MesagistoConfigBuilder};
use once_cell::sync::Lazy;
use res::TG_RES;
use self_update::Status;
use teloxide::{prelude::*, types::ParseMode, Bot};

Expand All @@ -32,6 +35,7 @@ mod handlers;
mod i18n;
mod log;
mod net;
pub mod res;
mod update;
mod webhook;

Expand Down Expand Up @@ -87,9 +91,9 @@ async fn run() -> Result<()> {
.remote_address(CONFIG.deref().centers.to_owned())
.same_side_deliver(true)
.skip_verify(CONFIG.tls.skip_verify)
.custom_cert(if CONFIG.tls.custom_cert.is_empty(){
.custom_cert(if CONFIG.tls.custom_cert.is_empty() {
None
}else{
} else {
Some(CONFIG.deref().tls.custom_cert.to_owned())
})
.proxy(if CONFIG.proxy.enable {
Expand All @@ -100,7 +104,7 @@ async fn run() -> Result<()> {
.build()?
.apply()
.await?;

TG_RES.init().await;
MesagistoConfig::packet_handler(|pkt| async { packet_handler(pkt).await }.boxed());
info!("log-boot-start", version = env!("CARGO_PKG_VERSION"));
let bot = Bot::with_client(CONFIG.telegram.token.clone(), net::client_from_config())
Expand Down

0 comments on commit 926fe09

Please sign in to comment.