Skip to content

Commit

Permalink
refactor: update figment, senerity, deps
Browse files Browse the repository at this point in the history
  • Loading branch information
iHsin committed Jan 18, 2024
1 parent 35f01d3 commit ad8027c
Show file tree
Hide file tree
Showing 13 changed files with 1,144 additions and 702 deletions.
1,708 changes: 1,082 additions & 626 deletions Cargo.lock

Large diffs are not rendered by default.

43 changes: 21 additions & 22 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "msgist-dc"
version = "1.4.1"
version = "1.4.3"

authors = ["Itsusinn <itsusinn@foxmail.com>"]
edition = "2021"
Expand All @@ -16,14 +16,12 @@ no-color = ["colored/no-color"]
color = ["tracing-subscriber/ansi"]

[dependencies]
serenity = { branch = "ws-proxy", git = "https://github.com/Itsusinn/serenity.git"}

# serenity = { path = "/home/itsusinn/Workspace/Clone/serenity" }
serenity = "0.12.0"

# logging
tracing= "0.1.37"
tracing-subscriber = { version = "0.3.16", default-features = false, features = [ "tracing-log", "std", "local-time", "fmt"] }
colored = "2.0.0"
tracing= "0.1.40"
tracing-subscriber = { version = "0.3.18", default-features = false, features = [ "tracing-log", "std", "local-time", "fmt"] }
colored = "2.1.0"

# asynchronous
futures-util = "0.3.25"
Expand All @@ -36,42 +34,43 @@ uuid = "1.2.2"
url = "2.3.1"
once_cell = "1.17.0"
dashmap = { version = "5.4.0", features = ["serde"] }
educe = { version = "0.4.20", default-features = false, features = ["Default"] }
educe = { version = "0.5.11", default-features = false, features = ["Default"] }
arcstr = { version = "1.1.5", features = ["serde"] }
either = "1.8.0"
infer = "0.12.0"
infer = "0.15.0"
yaml-rust = "0.4.5"
linked-hash-map = "0.5.6"
time = { version = "0.3.17", features = ["macros", "local-offset"] }
time = { version = "0.3.31", features = ["macros", "local-offset"] }
chrono = "0.4.23"

# error handling
thiserror = "1.0.38"
thiserror = "1.0.56"
color-eyre = "0.6.2"

# encoding and decoding
base64-url = "1.4.13"
base64-url = "2.0.2"

# serialization
serde = { version = "1.0.152", features = ["derive","rc"] }
serde_json = "1.0.91"
serde_yaml = "0.9.16"
ciborium = "0.2.0"
serde = { version = "1.0.195", features = ["derive","rc"] }
serde_json = "1.0.111"
serde_yaml = "0.9.30"
ciborium = "0.2.1"

# mesagisto
mesagisto-client = { branch = "master", git = "https://github.com/MeowCat-Studio/mesagisto-client-rs.git"}
#mesagisto-client = { path = "../../mesagisto-client/rust" }
automatic-config = { branch = "master", git = "https://github.com/Itsusinn/automatic-config-rs.git" }
#automatic-config = { path = "/code/automatic-config-rs" }

singleton = { branch = "master",git = "https://github.com/Itsusinn/singleton-rs.git" }
# singleton = { path = "/code/singleton-rs" }
lateinit = { branch = "master", git = "https://github.com/Itsusinn/lateinit-rs.git" }
self_update = { branch = "proxy", git = "https://github.com/Itsusinn/self_update.git", default-features = false, features = ["archive-zip","compression-zip-deflate","rustls"] }
self_update = { version = "0.39.0", default-features = false, features = ["archive-zip","compression-zip-deflate","rustls"] }
figment-wrapper = { branch = "master", git = "https://github.com/Itsusinn/figment-wrapper.git" }
figment = { version = "0.10.14", features = ["yaml"] }

# i18n
i18n-embed = { version = "0.13.4", features = ["fluent-system", "desktop-requester"]}
i18n-embed-fl = "0.6.4"
rust-embed = "6.4.2"
i18n-embed = { version = "0.14.1", features = ["fluent-system", "desktop-requester"]}
i18n-embed-fl = "0.7.0"
rust-embed = "8.2.0"
locale_config = "0.3.0"


Expand Down
1 change: 0 additions & 1 deletion README.md

This file was deleted.

8 changes: 4 additions & 4 deletions i18n/en-US/msgist_dc.ftl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
log-not-enable = MesagistoTG is not enabled, about to exit the program.
log-not-enable = MesagistoDC is not enabled, about to exit the program.
log-not-enable-helper = To enable, please modify the configuration file.
log-boot-start = MesagistoTG is starting, version: v{$version}
log-boot-sucess = MesagistoTG activation successful.
log-boot-start = MesagistoDC is starting, version: v{$version}
log-boot-sucess = MesagistoDC activation successful.
log-callback-error = An error occurred in the Teloxide dispatcher.
log-shutdown = MesagistoTG is about to close.
log-shutdown = MesagistoDC is about to close.
log-update-check-success = Auto update check successfully, program is up to date.
log-upgrade-success = Auto upgrade successfully, please restart the program mannualy.
4 changes: 2 additions & 2 deletions i18n/zh-CN/msgist_dc.ftl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
log-not-enable = 信使TG未被启用, 即将退出程序。
log-not-enable = 信使DC未被启用, 即将退出程序。
log-not-enable-helper = 若要启用,请修改配置文件。
log-boot-start = 信使dc正在启动, version: v{$version}
log-boot-sucess = 信使TG启动成功
log-boot-sucess = 信使DC启动成功
log-callback-error = Teloxide调度器发生错误
log-shutdown = DC信使即将关闭。
log-update-check-success = 自动更新检查成功,程序是最新的。
Expand Down
3 changes: 0 additions & 3 deletions locales/TODO.en-US.yml

This file was deleted.

3 changes: 0 additions & 3 deletions locales/TODO.zh-CN.yml

This file was deleted.

4 changes: 2 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub async fn bind(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
let address: ArcStr = args.single::<String>().unwrap().into();
match CONFIG
.bindings
.insert(msg.channel_id.0.get(), address.clone())
.insert(msg.channel_id.get(), address.clone())
{
Some(before) => {
msg
Expand All @@ -56,7 +56,7 @@ pub async fn bind(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
#[required_permissions("ADMINISTRATOR")]
#[description = "Unbind mesagisto-channel for current channel | 删除当前频道的信使频道"]
pub async fn unbind(ctx: &Context, msg: &Message) -> CommandResult {
let chat_id = msg.channel_id.0.get();
let chat_id = msg.channel_id.get();
match CONFIG.bindings.remove(&chat_id) {
Some(before) => {
msg.reply(ctx, "成功解绑当前频道的信使地址").await?;
Expand Down
21 changes: 9 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use std::sync::Arc;
use arcstr::ArcStr;
use color_eyre::eyre::{Error, Result};
use dashmap::DashMap;
use figment_wrapper::{figment_derive, FigmentWrapper};
use mesagisto_client::server::SERVER;
use uuid::Uuid;

#[config_derive]
#[derive(AutomaticConfig)]
#[location = "config/dc.yml"]
#[figment_derive]
#[derive(FigmentWrapper)]
#[location= "config/dc.yml"]
pub struct Config {
#[educe(Default = false)]
pub enable: bool,
Expand Down Expand Up @@ -49,20 +50,16 @@ impl Config {
.collect::<Vec<_>>();
Some(targets)
}

pub fn migrate(&self) {
self.centers.insert("mesagisto".into(), "wss://builtin".into());
}
}


#[config_derive]
#[figment_derive]
pub struct DiscordConfig {
#[educe(Default = "BOT.TOKEN")]
pub token: String,
}

#[config_derive]
#[figment_derive]
pub struct ProxyConfig {
#[educe(Default = false)]
pub enable: bool,
Expand All @@ -71,13 +68,13 @@ pub struct ProxyConfig {
pub address: ArcStr,
}

#[config_derive]
#[figment_derive]
pub struct CipherConfig {
#[educe(Default = "default")]
pub key: ArcStr,
}

#[config_derive]
#[figment_derive]
pub struct AutoUpdateConfig {
#[educe(Default = true)]
pub enable: bool,
Expand All @@ -87,7 +84,7 @@ pub struct AutoUpdateConfig {
pub no_confirm: bool,
}

#[config_derive]
#[figment_derive]
pub struct TlsConfig {
#[educe(Default = false)]
pub skip_verify: bool,
Expand Down
20 changes: 10 additions & 10 deletions src/handlers/receive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ops::ControlFlow, num::NonZeroU64};
use std::ops::ControlFlow;

use arcstr::ArcStr;
use color_eyre::eyre::Result;
Expand Down Expand Up @@ -62,7 +62,7 @@ pub async fn del(room_address: &ArcStr) -> Result<()> {
}

pub async fn packet_handler(pkt: Packet) -> Result<ControlFlow<Packet>> {
debug!("recv msg pkt from {:#?}", pkt.room_id);
tracing::debug!("recv msg pkt from {:#?}", pkt.room_id);
match pkt.decrypt() {
Ok(either::Either::Left(message)) => {
if let Some(targets) = CONFIG.target_id(pkt.room_id.clone()) {
Expand Down Expand Up @@ -102,7 +102,7 @@ pub async fn packet_handler(pkt: Packet) -> Result<ControlFlow<Packet>> {
}
}
Ok(either::Either::Right(event)) => {
debug!("recv event pkt {:#?}", event);
tracing::debug!("recv event pkt {:#?}", event);
return Ok(ControlFlow::Break(pkt));
}
Err(e) => {
Expand All @@ -113,7 +113,7 @@ pub async fn packet_handler(pkt: Packet) -> Result<ControlFlow<Packet>> {
}

async fn msg_handler(mut message: Message, target_id: u64, server: ArcStr) -> Result<()> {
let target = BOT_CLIENT.get_channel(ChannelId(NonZeroU64::new(target_id).unwrap())).await?.id();
let target = BOT_CLIENT.get_channel(ChannelId::from(target_id)).await?.id();
let room = CONFIG.room_address(&target_id).expect("Room不存在");
let room_id = SERVER.room_id(room);

Expand All @@ -134,7 +134,7 @@ async fn msg_handler(mut message: Message, target_id: u64, server: ArcStr) -> Re
let local_id = DB.get_msg_id_1(&target_id, reply_to)?;
match local_id {
Some(local_id) => {
let refer = MessageReference::from((ChannelId(NonZeroU64::new(target_id).unwrap()), MessageId::from(local_id)));
let refer = MessageReference::from((ChannelId::from(target_id), MessageId::from(local_id)));
target
.send_message(&**BOT_CLIENT, CreateMessage::new().content(content).reference_message(refer))
.await
Expand All @@ -150,14 +150,14 @@ async fn msg_handler(mut message: Message, target_id: u64, server: ArcStr) -> Re
.send_message(&**BOT_CLIENT, CreateMessage::new().content(content))
.await
}?;
DB.put_msg_id_1(&target_id, &message.id, &receipt.id.0.get())?;
DB.put_msg_id_1(&target_id, &message.id, &receipt.id.get())?;
}
MessageType::Image { id, url } => {
let path = RES.file(&id, &url, room_id.clone(), &server).await?;
let receipt = target
.send_message(&**BOT_CLIENT, CreateMessage::new().content(format!("{sender_name}:")))
.await?;
DB.put_msg_id_ir_2(&target_id, &receipt.id.0.get(), &message.id)?;
DB.put_msg_id_ir_2(&target_id, &receipt.id.get(), &message.id)?;
let kind = infer::get_from_path(&path).expect("file read failed when refering file type");

let filename = match kind {
Expand All @@ -169,14 +169,14 @@ async fn msg_handler(mut message: Message, target_id: u64, server: ArcStr) -> Re
let receipt = target
.send_message(&**BOT_CLIENT, CreateMessage::new().add_file(attachment))
.await?;
DB.put_msg_id_1(&target_id, &message.id, &receipt.id.0.get())?;
DB.put_msg_id_1(&target_id, &message.id, &receipt.id.get())?;
}
MessageType::Sticker { id, url } => {
let path = RES.file(&id, &url, room_id.clone(), &server).await?;
let receipt = target
.send_message(&**BOT_CLIENT, CreateMessage::new().content(format!("{sender_name}:")))
.await?;
DB.put_msg_id_ir_2(&target_id, &receipt.id.0.get(), &message.id)?;
DB.put_msg_id_ir_2(&target_id, &receipt.id.get(), &message.id)?;
let kind = infer::get_from_path(&path).expect("file read failed when refering file type");

let filename = match kind {
Expand All @@ -188,7 +188,7 @@ async fn msg_handler(mut message: Message, target_id: u64, server: ArcStr) -> Re
let receipt = target
.send_message(&**BOT_CLIENT, CreateMessage::new().add_file(attachment))
.await?;
DB.put_msg_id_1(&target_id, &message.id, &receipt.id.0.get())?;
DB.put_msg_id_1(&target_id, &message.id, &receipt.id.get())?;
}
_ => {}
}
Expand Down
16 changes: 8 additions & 8 deletions src/handlers/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
};

pub async fn answer_common(msg: Message) -> Result<()> {
let target = msg.channel_id.0.get();
let target = msg.channel_id.get();
if !CONFIG.bindings.contains_key(&target) {
return Ok(());
}
Expand All @@ -28,7 +28,7 @@ pub async fn answer_common(msg: Message) -> Result<()> {
let sender = &msg.author;
let nick = msg.member.as_ref().and_then(|v| v.nick.clone());
let profile = Profile {
id: sender.id.0.get().to_be_bytes().to_vec(), // fixme
id: sender.id.get().to_be_bytes().to_vec(), // fixme
username: Some(sender.name.clone()),
nick,
};
Expand All @@ -45,13 +45,13 @@ pub async fn answer_common(msg: Message) -> Result<()> {
if ty.starts_with("image") {
let dc_file = DcFile::new(
&target,
&attach.id.0.get(),
&attach.id.get(),
&ArcStr::from(attach.filename.clone()),
);
RES.put_dc_image_id(&attach.id.0.get(), &dc_file)?;
RES.put_dc_image_id(&attach.id.get(), &dc_file)?;
BOT_CLIENT.download_file(&dc_file).await?;
chain.push(MessageType::Image {
id: attach.id.0.get().to_be_bytes().to_vec(),
id: attach.id.get().to_be_bytes().to_vec(),
url: None,
});
}
Expand All @@ -62,16 +62,16 @@ pub async fn answer_common(msg: Message) -> Result<()> {

let reply = match &msg.referenced_message {
Some(v) => {
let local_id = v.id.0.get().to_be_bytes().to_vec();
let local_id = v.id.get().to_be_bytes().to_vec();
DB.get_msg_id_2(&target, &local_id).unwrap_or(None)
}
None => None,
};
// msg.attachments.get(0).unwrap().content_type;
DB.put_msg_id_ir_0(&target, &msg.id.0.get(), &msg.id.0.get())?;
DB.put_msg_id_ir_0(&target, &msg.id.get(), &msg.id.get())?;
let message = message::Message {
profile,
id: msg.id.0.get().to_be_bytes().to_vec(),
id: msg.id.get().to_be_bytes().to_vec(),
reply,
chain,
from: target.to_be_bytes().to_vec(),
Expand Down
2 changes: 1 addition & 1 deletion src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) fn init() {
.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
13 changes: 5 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ use crate::{

#[macro_use]
extern crate educe;
#[macro_use]
extern crate automatic_config;

#[macro_use]
extern crate singleton;
#[macro_use]
extern crate tracing;

mod bot;
mod commands;
Expand Down Expand Up @@ -64,8 +61,7 @@ async fn run() -> Result<()> {
warn!("log-not-enable-helper");
return Ok(());
}
CONFIG.migrate();

CONFIG.save().await?;

if CONFIG.auto_update.enable {
tokio::task::spawn_blocking(|| {
Expand Down Expand Up @@ -109,7 +105,8 @@ async fn run() -> Result<()> {
let framework = StandardFramework::new()
.help(&framework::HELP)
.group(&framework::MESAGISTO_GROUP);
framework.configure(|c| c.prefix("/"));
let conf = serenity::framework::standard::Configuration::default().prefix("/");
framework.configure(conf);

// .normal_message(handlers::message_hook);

Expand All @@ -134,7 +131,7 @@ async fn run() -> Result<()> {
tokio::spawn(async move {
client.start().await.expect("Client error");
});

tokio::signal::ctrl_c().await?;

info!("log-shutdown");
Expand Down

0 comments on commit ad8027c

Please sign in to comment.