Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clippy issues, remove some dep #452

Merged
merged 6 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
669 changes: 377 additions & 292 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 2 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@ native-tls = [
"reqwest/native-tls-vendored",
"reqwest/native-tls-alpn",
]
rustls = [
"tbot/rustls",
"hyper-proxy/rustls",
"reqwest/rustls-tls",
]
rustls = ["tbot/rustls", "hyper-proxy/rustls", "reqwest/rustls-tls"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change the style


[build-dependencies]
ctl10n = "0.2.0"

[dependencies]
lazy_static = "1.4"
once_cell = "1.18"
anyhow = "1.0"
structopt = "0.3"
futures = "0.3"
Expand Down Expand Up @@ -55,6 +49,6 @@ version = "0.9"
default-features = false

[dependencies.reqwest]
version = "0.11"
version = "0.12"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reqwest 0.12 breaks tbot

default-features = false
features = ["gzip", "json"]
27 changes: 14 additions & 13 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
Telegram RSS bot [@RustRssBot](http://t.me/RustRssBot)

**Supports:**
- [x] RSS 0.9
- [x] RSS 0.91
- [x] RSS 0.92
- [x] RSS 0.93
- [x] RSS 0.94
- [x] RSS 1.0
- [x] RSS 2.0
- [x] Atom 0.3
- [x] Atom 1.0
- [x] JSON Feed 1

- [x] RSS 0.9
- [x] RSS 0.91
- [x] RSS 0.92
- [x] RSS 0.93
- [x] RSS 0.94
- [x] RSS 1.0
- [x] RSS 2.0
- [x] Atom 0.3
- [x] Atom 1.0
- [x] JSON Feed 1

## Usage

Expand All @@ -25,13 +26,13 @@ Telegram RSS bot [@RustRssBot](http://t.me/RustRssBot)

## Download

The pre-compiled binaries can be downloaded directly from [Releases](https://github.com/iovxw/rssbot/releases). Make sure to use the english binary (`rssbot-en-amd64-linux`). The Linux version is statically linked to *musl*, no other dependencies required.
The pre-compiled binaries can be downloaded directly from [Releases](https://github.com/iovxw/rssbot/releases). Make sure to use the english binary (`rssbot-en-amd64-linux`). The Linux version is statically linked to _musl_, no other dependencies required.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change the style


## Compile

**Please try to download from the Link above, if that's not feasible or you have other requirements you should compile manually**

Install *Rust Nightly* and *Cargo* ([`rustup` recommended](https://www.rustup.rs/)) first, then:
Install _Rust Nightly_ and _Cargo_ ([`rustup` recommended](https://www.rustup.rs/)) first, then:

```
LOCALE=en cargo build --release
Expand All @@ -58,7 +59,7 @@ OPTIONS:
multiple times to allow multiple admins
--api-uri <tgapi-uri> Custom telegram api URI [default: https://api.telegram.org/]
-d, --database <path> Path to database [default: ./rssbot.json]
--max-feed-size <bytes> Maximum feed size, 0 is unlimited [default: 2097152]
--max-feed-size <bytes> Maximum feed size, 0 is unlimited [default: 2M]
--max-interval <seconds> Maximum fetch interval [default: 43200]
--min-interval <seconds> Minimum fetch interval [default: 300]

Expand Down
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

**Other Languages:** [English](README.en.md)

Telegram RSS 机器人 [@RustRssBot](http://t.me/RustRssBot)
Telegram RSS 机器人  [@RustRssBot](http://t.me/RustRssBot)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 space?


**支持:**
- [x] RSS 0.9
- [x] RSS 0.91
- [x] RSS 0.92
- [x] RSS 0.93
- [x] RSS 0.94
- [x] RSS 1.0
- [x] RSS 2.0
- [x] Atom 0.3
- [x] Atom 1.0
- [x] JSON Feed 1

- [x] RSS 0.9
- [x] RSS 0.91
- [x] RSS 0.92
- [x] RSS 0.93
- [x] RSS 0.94
- [x] RSS 1.0
- [x] RSS 2.0
- [x] Atom 0.3
- [x] Atom 1.0
- [x] JSON Feed 1

## 使用

Expand All @@ -25,13 +26,13 @@ Telegram RSS 机器人 [@RustRssBot](http://t.me/RustRssBot)

## 下载

可直接从 [Releases](https://github.com/iovxw/rssbot/releases) 下载预编译的程序(带 `zh` 的为中文版), Linux 版本为 *musl* 静态链接, 无需其他依赖
可直接从 [Releases](https://github.com/iovxw/rssbot/releases) 下载预编译的程序(带 `zh` 的为中文版), Linux 版本为 _musl_ 静态链接, 无需其他依赖

## 编译

**请先尝试从上面下载, 如不可行或者有其他需求再手动编译**

先安装 *Rust Nightly* 以及 *Cargo* (推荐使用 [`rustup`](https://www.rustup.rs/)), 然后:
先安装 _Rust Nightly_ 以及 _Cargo_ (推荐使用 [`rustup`](https://www.rustup.rs/)), 然后:

```
cargo build --release
Expand All @@ -56,7 +57,7 @@ OPTIONS:
multiple times to allow multiple admins
--api-uri <tgapi-uri> Custom telegram api URI [default: https://api.telegram.org/]
-d, --database <path> Path to database [default: ./rssbot.json]
--max-feed-size <bytes> Maximum feed size, 0 is unlimited [default: 2097152]
--max-feed-size <bytes> Maximum feed size, 0 is unlimited [default: 2M]
--max-interval <seconds> Maximum fetch interval [default: 43200]
--min-interval <seconds> Minimum fetch interval [default: 300]

Expand Down
2 changes: 0 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::env;
use std::path::Path;

use ctl10n;

const LOCALES: &[&str] = &["zh", "en"];

fn main() {
Expand Down
15 changes: 7 additions & 8 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::env;
use std::time::Duration;

use once_cell::sync::OnceCell;
use reqwest::{
self,
header::{HeaderValue, CONTENT_TYPE},
};
use std::sync::OnceLock;
use thiserror::Error;

use crate::feed::Rss;

static RESP_SIZE_LIMIT: OnceCell<u64> = OnceCell::new();
static CLIENT: OnceCell<reqwest::Client> = OnceCell::new();
static RESP_SIZE_LIMIT: OnceLock<u64> = OnceLock::new();
static CLIENT: OnceLock<reqwest::Client> = OnceLock::new();

#[derive(Error, Debug)]
pub enum FeedError {
Expand All @@ -28,10 +28,9 @@ impl FeedError {
match self {
Self::Network(source) => tr!("network_error", source = source),
Self::Parsing(source) => tr!("parsing_error", source = source),
Self::TooLarge(limit) => tr!(
"rss_size_limit_exceeded",
size = format_byte_size((*limit).into())
),
Self::TooLarge(limit) => {
tr!("rss_size_limit_exceeded", size = format_byte_size(*limit))
}
}
}
}
Expand Down Expand Up @@ -153,7 +152,7 @@ mod test {

#[test]
fn max_format_byte_size() {
assert_eq!(format_byte_size(std::u64::MAX), "16EiB");
assert_eq!(format_byte_size(u64::MAX), "16EiB");
}

#[test]
Expand Down
7 changes: 3 additions & 4 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub async fn check_command(opt: &crate::Opt, cmd: &Command) -> bool {
let reply_target = &mut MsgTarget::new(cmd.chat.id, cmd.message_id);

// Private mode
if !opt.admin.is_empty() && !is_from_bot_admin(&cmd, &opt.admin) {
if !opt.admin.is_empty() && !is_from_bot_admin(cmd, &opt.admin) {
eprintln!(
"Unauthenticated request from user/channel: {:?}, command: {}, args: {}",
cmd.from, cmd.command, cmd.text.value
Expand All @@ -62,7 +62,7 @@ pub async fn check_command(opt: &crate::Opt, cmd: &Command) -> bool {
}
// Restrict mode: bot commands are only accessible to admins.
Group { .. } | Supergroup { .. } if opt.restricted => {
let user_is_admin = is_from_chat_admin(&cmd).await;
let user_is_admin = is_from_chat_admin(cmd).await;
if !user_is_admin {
let _ignore_result = update_response(
&cmd.bot,
Expand Down Expand Up @@ -226,8 +226,7 @@ async fn check_channel_permission(
}
let bot_is_admin = admins
.iter()
.find(|member| member.user.id == *crate::BOT_ID.get().unwrap())
.is_some();
.any(|member| member.user.id == *crate::BOT_ID.get().unwrap());
if !bot_is_admin {
update_response(
bot,
Expand Down
18 changes: 7 additions & 11 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::time::{Duration, SystemTime};

use atomicwrites::{AtomicFile, OverwriteBehavior};
use serde::{Deserialize, Serialize};
use serde_json;
use thiserror::Error;

use crate::feed;
Expand Down Expand Up @@ -94,11 +93,11 @@ impl Database {
}

pub fn all_feeds(&self) -> Vec<Feed> {
self.feeds.iter().map(|(_, v)| v.clone()).collect()
self.feeds.values().cloned().collect()
}

pub fn all_subscribers(&self) -> Vec<SubscriberId> {
self.subscribers.iter().map(|(k, _)| *k).collect()
self.subscribers.keys().copied().collect()
}

pub fn subscribed_feeds(&self, subscriber: SubscriberId) -> Option<Vec<Feed>> {
Expand Down Expand Up @@ -144,10 +143,7 @@ impl Database {
pub fn subscribe(&mut self, subscriber: SubscriberId, rss_link: &str, rss: &feed::Rss) -> bool {
let feed_id = gen_hash(&rss_link);
{
let subscribed_feeds = self
.subscribers
.entry(subscriber)
.or_insert_with(HashSet::default);
let subscribed_feeds = self.subscribers.entry(subscriber).or_default();
if !subscribed_feeds.insert(feed_id) {
return false;
}
Expand Down Expand Up @@ -218,7 +214,7 @@ impl Database {
.remove(&from)
.map(|feeds| {
for feed_id in &feeds {
let feed = self.feeds.get_mut(&feed_id).unwrap();
let feed = self.feeds.get_mut(feed_id).unwrap();
feed.subscribers.remove(&from);
feed.subscribers.insert(to);
}
Expand Down Expand Up @@ -273,7 +269,7 @@ impl Database {
}

pub fn save(&self) -> Result<(), DataError> {
let feeds_list: Vec<&Feed> = self.feeds.iter().map(|(_id, feed)| feed).collect();
let feeds_list: Vec<&Feed> = self.feeds.values().collect();
let file = AtomicFile::new(&self.path, OverwriteBehavior::AllowOverwrite);
file.write(|file| serde_json::to_writer(file, &feeds_list))
.map_err(|e| match e {
Expand All @@ -294,8 +290,8 @@ pub enum FeedUpdate {

fn gen_item_hash(item: &feed::Item) -> u64 {
item.id.as_ref().map(|id| gen_hash(&id)).unwrap_or_else(|| {
let title = item.title.as_ref().map(|s| s.as_str()).unwrap_or_default();
let link = item.link.as_ref().map(|s| s.as_str()).unwrap_or_default();
let title = item.title.as_deref().unwrap_or_default();
let link = item.link.as_deref().unwrap_or_default();
gen_hash(&format!("{}{}", title, link))
})
}
Expand Down
Loading