Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
misc: 升级依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloxaf committed Dec 15, 2022
1 parent 59ec744 commit d8d6ff3
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 346 deletions.
692 changes: 408 additions & 284 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "exloli"
description = "根据指定关键词从 E 站下载本子并上传到 telegraph 并发布到 Telegram 频道"
version = "0.5.6"
version = "0.5.7"
authors = ["Aloxaf <[email protected]>"]
edition = "2018"

Expand All @@ -14,7 +14,7 @@ libxml = "0.2.15"
reqwest = { version = "0.11.2", features = ["json", "cookies", "gzip", "socks", "brotli"] }
tempfile = "3.1.0"
v_htmlescape = "0.15.1"
teloxide = { version = "0.10.1", features = ["auto-send"] }
teloxide = "0.11.3"
tokio = { version = "1.4.0", features = ["time", "rt-multi-thread", "macros"] }
telegraph-rs = { version = "0.5.0", git = "https://github.com/Aloxaf/telegraph-rs" }
futures = "0.3.5"
Expand Down Expand Up @@ -44,6 +44,6 @@ env_logger = "0.9.0"
dashmap = "5.3.4"

[dependencies.image]
version = "0.23.14"
version = "0.24"
default-features = false
features = ["gif", "jpeg", "png"]
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.3"
services:
skrman:
image: aloxaf/exloli:0.5.4
image: aloxaf/exloli:0.5.7
container_name: exloli
volumes:
- ./db.sqlite:/app/db.sqlite
Expand Down
6 changes: 1 addition & 5 deletions src/bot/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ pub enum RuaCommand {

impl RuaCommand {
/// 将消息解析为命令
pub async fn parse(
bot: AutoSend<Bot>,
message: &Message,
bot_id: &str,
) -> Result<Self, CommandError> {
pub async fn parse(bot: Bot, message: &Message, bot_id: &str) -> Result<Self, CommandError> {
use CommandError::*;

let text = message.text().unwrap_or("");
Expand Down
60 changes: 19 additions & 41 deletions src/bot/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ macro_rules! reply_to {
static LIMIT: Lazy<RateLimiter<u64>> =
Lazy::new(|| RateLimiter::new(std::time::Duration::from_secs(60), 10));
//
async fn on_new_gallery(bot: AutoSend<Bot>, message: &Message) -> Result<()> {
async fn on_new_gallery(bot: Bot, message: &Message) -> Result<()> {
info!("频道消息更新,发送投票");
// 辣鸡 tg 安卓客户端在置顶消息过多时似乎在进群时会卡住
bot.unpin_chat_message(message.chat.id)
Expand All @@ -36,15 +36,15 @@ async fn on_new_gallery(bot: AutoSend<Bot>, message: &Message) -> Result<()> {
Ok(())
}
//
async fn cmd_delete(bot: AutoSend<Bot>, message: &Message, real: bool) -> Result<Message> {
info!("执行命令: delete_{} {}", real, message.id);
async fn cmd_delete(bot: Bot, message: &Message, real: bool) -> Result<Message> {
info!("执行命令: delete_{} {:?}", real, message.id);
let to_del = message.reply_to_message().context("找不到回复")?;
let channel = to_del.forward_from_chat().context("获取来源对话失败")?;
let msg_id = to_del
.forward_from_message_id()
.context("获取转发来源失败")?;
bot.delete_message(to_del.chat.id, to_del.id).await?;
bot.delete_message(channel.id, msg_id).await?;
bot.delete_message(channel.id, MessageId(msg_id)).await?;
let gallery = DB.query_gallery(msg_id)?;
match real {
false => DB.delete_gallery(msg_id)?,
Expand All @@ -55,7 +55,7 @@ async fn cmd_delete(bot: AutoSend<Bot>, message: &Message, real: bool) -> Result
}

async fn do_chain_action<T, F, Fut>(
bot: AutoSend<Bot>,
bot: Bot,
message: &Message,
input: &[T],
action: F,
Expand Down Expand Up @@ -96,7 +96,7 @@ where
.await?)
}

async fn cmd_upload(bot: AutoSend<Bot>, message: &Message, urls: &[String]) -> Result<Message> {
async fn cmd_upload(bot: Bot, message: &Message, urls: &[String]) -> Result<Message> {
info!("执行命令: upload {:?}", urls);
do_chain_action(bot, message, urls, |url| {
let url = url.clone();
Expand All @@ -105,11 +105,7 @@ async fn cmd_upload(bot: AutoSend<Bot>, message: &Message, urls: &[String]) -> R
.await
}

async fn cmd_reupload(
bot: AutoSend<Bot>,
message: &Message,
old_gallery: &InputGallery,
) -> Result<Message> {
async fn cmd_reupload(bot: Bot, message: &Message, old_gallery: &InputGallery) -> Result<Message> {
info!("执行命令: reupload {:?}", old_gallery);
let mut text = "收到命令,执行中……".to_owned();
let reply_message = reply_to!(bot, message, &text).await?;
Expand All @@ -122,11 +118,7 @@ async fn cmd_reupload(
.await?)
}

async fn cmd_full(
bot: AutoSend<Bot>,
message: &Message,
galleries: &[InputGallery],
) -> Result<Message> {
async fn cmd_full(bot: Bot, message: &Message, galleries: &[InputGallery]) -> Result<Message> {
info!("执行命令: full {:?}", galleries);
do_chain_action(bot, message, galleries, |gallery| {
let gallery = match block_on(gallery.to_gallery()) {
Expand All @@ -139,7 +131,7 @@ async fn cmd_full(
}

async fn cmd_update_tag(
bot: AutoSend<Bot>,
bot: Bot,
message: &Message,
galleries: &[InputGallery],
) -> Result<Message> {
Expand Down Expand Up @@ -178,7 +170,7 @@ fn query_best_text(from: i64, to: i64, offset: i64) -> Result<String> {
Ok(text)
}

async fn cmd_best(bot: AutoSend<Bot>, message: &Message, from: i64, to: i64) -> Result<Message> {
async fn cmd_best(bot: Bot, message: &Message, from: i64, to: i64) -> Result<Message> {
info!("执行命令: best {} {}", from, to);
let text = query_best_text(from, to, 1)?;
let reply_markup = query_best_keyboard(from, to, 1);
Expand All @@ -189,11 +181,7 @@ async fn cmd_best(bot: AutoSend<Bot>, message: &Message, from: i64, to: i64) ->
}

/// 查询画廊,若失败则返回失败消息,成功则直接发送
async fn cmd_query(
bot: AutoSend<Bot>,
message: &Message,
galleries: &[InputGallery],
) -> Result<Message> {
async fn cmd_query(bot: Bot, message: &Message, galleries: &[InputGallery]) -> Result<Message> {
info!("执行命令: query {:?}", galleries);
let text = match galleries.len() {
1 => galleries[0]
Expand Down Expand Up @@ -244,7 +232,7 @@ fn is_new_gallery(message: &Message) -> bool {
.unwrap_or(false)
}

pub async fn message_handler(message: Message, bot: AutoSend<Bot>) -> Result<()> {
pub async fn message_handler(message: Message, bot: Bot) -> Result<()> {
use RuaCommand::*;

trace!("{:#?}", message);
Expand Down Expand Up @@ -320,15 +308,15 @@ pub async fn message_handler(message: Message, bot: AutoSend<Bot>) -> Result<()>
tokio::spawn(async move {
sleep(time::Duration::from_secs(300)).await;
for id in to_delete {
info!("清除消息 {}", id);
info!("清除消息 {:?}", id);
bot.delete_message(chat_id, id).await.log_on_error().await;
}
});
}
Ok(())
}

pub async fn poll_handler(poll: Poll, _bot: AutoSend<Bot>) -> Result<()> {
pub async fn poll_handler(poll: Poll, _bot: Bot) -> Result<()> {
let options = poll.options;
let votes = options.iter().map(|s| s.voter_count).collect::<Vec<_>>();
let score = Vote::wilson_score(&votes);
Expand All @@ -337,7 +325,7 @@ pub async fn poll_handler(poll: Poll, _bot: AutoSend<Bot>) -> Result<()> {
DB.update_score(&poll.id, score, votes)
}

pub async fn inline_handler(query: InlineQuery, bot: AutoSend<Bot>) -> Result<()> {
pub async fn inline_handler(query: InlineQuery, bot: Bot) -> Result<()> {
let text = query.query.trim();
info!("行内查询:{}", text);
let mut answer = vec![];
Expand All @@ -363,13 +351,8 @@ fn split_vec<T: FromStr>(s: &str) -> std::result::Result<Vec<T>, T::Err> {
.collect::<std::result::Result<Vec<_>, _>>()
}

async fn callback_change_page(
bot: AutoSend<Bot>,
message: &Message,
cmd: &str,
data: &str,
) -> Result<()> {
info!("翻页:{} {}", message.id, cmd);
async fn callback_change_page(bot: Bot, message: &Message, cmd: &str, data: &str) -> Result<()> {
info!("翻页:{:?} {}", message.id, cmd);
// vec![from, to, offset]
let data = split_vec::<i64>(data)?;
let [from, to, mut offset] = match TryInto::<[i64; 3]>::try_into(data) {
Expand All @@ -392,12 +375,7 @@ async fn callback_change_page(
Ok(())
}

async fn callback_poll(
bot: AutoSend<Bot>,
message: &Message,
user_id: u64,
data: &str,
) -> Result<()> {
async fn callback_poll(bot: Bot, message: &Message, user_id: u64, data: &str) -> Result<()> {
let data = split_vec::<i32>(data)?;
let [poll_id, option] = match TryInto::<[i32; 2]>::try_into(data) {
Ok(v) => v,
Expand All @@ -421,7 +399,7 @@ async fn callback_poll(
Ok(())
}

pub async fn callback_handler(callback: CallbackQuery, bot: AutoSend<Bot>) -> Result<()> {
pub async fn callback_handler(callback: CallbackQuery, bot: Bot) -> Result<()> {
debug!("回调:{:?}", callback.data);

if let Some(d) = LIMIT.insert(callback.from.id.0) {
Expand Down
2 changes: 1 addition & 1 deletion src/bot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod utils;
use handler::*;
use teloxide::prelude::*;

pub async fn start_bot(bot: AutoSend<Bot>) {
pub async fn start_bot(bot: Bot) {
info!("BOT 启动");

let handler = dptree::entry()
Expand Down
4 changes: 2 additions & 2 deletions src/bot/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl MessageExt for Message {

// TODO: 缓存
/// 获取管理员列表
async fn get_admins(bot: AutoSend<Bot>) -> Option<Vec<User>> {
async fn get_admins(bot: Bot) -> Option<Vec<User>> {
let mut admins = bot
.get_chat_administrators(CONFIG.telegram.channel_id.clone())
.await
Expand All @@ -74,7 +74,7 @@ async fn get_admins(bot: AutoSend<Bot>) -> Option<Vec<User>> {
}

// 检测是否是指定频道的管理员
pub async fn check_is_channel_admin(bot: AutoSend<Bot>, message: &Message) -> (bool, bool) {
pub async fn check_is_channel_admin(bot: Bot, message: &Message) -> (bool, bool) {
// 先检测是否为匿名管理员
let from_user = message.from();
if from_user
Expand Down
18 changes: 11 additions & 7 deletions src/exloli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use chrono::{Datelike, Duration, Timelike, Utc};
use futures::TryFutureExt;
use telegraph_rs::{html_to_node, Page, Telegraph};
use teloxide::prelude::*;
use teloxide::types::ParseMode;
use teloxide::types::{MessageId, ParseMode};
use v_htmlescape::escape;

pub struct ExLoli {
Expand Down Expand Up @@ -151,10 +151,10 @@ impl ExLoli {
// 生成 poll_id,仅当旧画廊使用的新格式 poll_id 的情况下才会继承
let poll_id = old_gallery
.and_then(|g| g.poll_id.parse::<i32>().map_err(anyhow::Error::new))
.unwrap_or(message.id);
.unwrap_or(message.id.0);

DB.insert_gallery(message.id, &gallery, page.url)?;
DB.update_poll_id(message.id, &poll_id.to_string())
DB.insert_gallery(message.id.0, &gallery, page.url)?;
DB.update_poll_id(message.id.0, &poll_id.to_string())
}

/// 原地更新画廊,若 gallery 为 None 则原地更新为原画廊的完整版
Expand Down Expand Up @@ -249,9 +249,13 @@ impl ExLoli {
) -> Result<()> {
info!("更新 Telegram 频道消息");
let text = Self::get_message_string(gallery, article);
BOT.edit_message_text(CONFIG.telegram.channel_id.clone(), message_id, &text)
.parse_mode(ParseMode::Html)
.await?;
BOT.edit_message_text(
CONFIG.telegram.channel_id.clone(),
MessageId(message_id),
&text,
)
.parse_mode(ParseMode::Html)
.await?;
DB.update_gallery(message_id, gallery, article, upload_images)
}

Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ static CONFIG: Lazy<Config> = Lazy::new(|| {
let config_file = config_file.as_deref().unwrap_or("config.toml");
Config::new(config_file).expect("配置文件解析失败")
});
static BOT: Lazy<AutoSend<Bot>> =
Lazy::new(|| teloxide::Bot::new(&CONFIG.telegram.token).auto_send());
static BOT: Lazy<Bot> = Lazy::new(|| teloxide::Bot::new(&CONFIG.telegram.token));
static DB: Lazy<DataBase> = Lazy::new(|| DataBase::init().expect("数据库初始化失败"));
static EXLOLI: Lazy<ExLoli> = Lazy::new(|| block_on(ExLoli::new()).expect("登录失败"));

Expand Down

0 comments on commit d8d6ff3

Please sign in to comment.