feat(rust): upgrade to latest nightly

This commit is contained in:
Harsh Shandilya 2024-01-10 11:41:24 +05:30
parent aae6d57da2
commit 3769487741
9 changed files with 58 additions and 50 deletions

View File

@ -42,7 +42,7 @@
rustNightly = (import fenix {inherit pkgs;}).fromToolchainFile {
file = ./rust-toolchain.toml;
sha256 = "sha256-3+hzpuIkf2KFGyop0DgMPI5nCY6IQX8I6XdIUwn4nAI=";
sha256 = "sha256-Je+rS6Tgc04HqvDj2XvjOpbyIAcEee0JqqrG7+cUydI=";
};
craneLib = (crane.mkLib pkgs).overrideToolchain rustNightly;

View File

@ -1,5 +1,5 @@
[toolchain]
channel = "nightly-2023-10-22"
channel = "nightly-2024-01-09"
components = ["clippy", "rustfmt", "rust-src"]
targets = ["x86_64-unknown-linux-gnu"]
profile = "minimal"

View File

@ -58,12 +58,13 @@ pub(crate) async fn handler(
Command::Ping => {
bot.send_chat_action(message.chat.id, ChatAction::Typing)
.await?;
bot.send_message(message.chat.id, "Pong")
.await?;
},
bot.send_message(message.chat.id, "Pong").await?;
}
#[cfg(feature = "ddinstagram")]
Command::Instagram { filter_state } => {
if let Some(from) = message.from() && from.id != *BOT_OWNER {
if let Some(from) = message.from()
&& from.id != *BOT_OWNER
{
bot.send_chat_action(message.chat.id, ChatAction::Typing)
.await?;
bot.send_message(message.chat.id, "You are not authorized for this action")
@ -85,7 +86,9 @@ pub(crate) async fn handler(
}
}
Command::Medium { filter_state } => {
if let Some(from) = message.from() && from.id != *BOT_OWNER {
if let Some(from) = message.from()
&& from.id != *BOT_OWNER
{
bot.send_chat_action(message.chat.id, ChatAction::Typing)
.await?;
bot.send_message(message.chat.id, "You are not authorized for this action")
@ -113,7 +116,9 @@ pub(crate) async fn handler(
.await?;
}
Command::Twitter { filter_state } => {
if let Some(from) = message.from() && from.id != *BOT_OWNER {
if let Some(from) = message.from()
&& from.id != *BOT_OWNER
{
bot.send_chat_action(message.chat.id, ChatAction::Typing)
.await?;
bot.send_message(message.chat.id, "You are not authorized for this action")
@ -135,7 +140,9 @@ pub(crate) async fn handler(
}
}
Command::YouTube { filter_state } => {
if let Some(from) = message.from() && from.id != *BOT_OWNER {
if let Some(from) = message.from()
&& from.id != *BOT_OWNER
{
bot.send_chat_action(message.chat.id, ChatAction::Typing)
.await?;
bot.send_message(message.chat.id, "You are not authorized for this action")

View File

@ -17,7 +17,9 @@ pub async fn handler(
bot: Bot,
message: Message,
) -> Result<(), Box<dyn Error + Sync + Send + 'static>> {
if let Some(text) = message.text() && let Some(user) = message.from() {
if let Some(text) = message.text()
&& let Some(user) = message.from()
{
let mut text = String::from_str(text)?;
let urls = get_urls_from_message(&message);
debug!(?urls);
@ -34,11 +36,7 @@ pub async fn handler(
return Ok(());
}
}
let text = format!(
"{}: {}",
link(user.url().as_str(), &user.full_name()),
text
);
let text = format!("{}: {}", link(user.url().as_str(), &user.full_name()), text);
let _del = bot.delete_message(message.chat.id, message.id).await;
bot.try_reply(message, text).await?;
}
@ -53,7 +51,9 @@ pub fn is_amp(msg: Message) -> bool {
return false;
}
urls.iter().flat_map(|url| Url::parse(url)).any(|url| {
if let Some(mut segments) = url.path_segments() && let Some(host) = url.host_str() {
if let Some(mut segments) = url.path_segments()
&& let Some(host) = url.host_str()
{
segments.any(|x| x == "amp") || host.ends_with(".cdn.ampproject.org")
} else {
false

View File

@ -62,14 +62,12 @@ pub async fn handler(
bot: Bot,
message: Message,
) -> Result<(), Box<dyn Error + Sync + Send + 'static>> {
if let Some(text) = scrub_urls(&message) && let Some(user) = message.from() &&
let Some(caps) = MATCH_REGEX.captures(&text) {
if let Some(text) = scrub_urls(&message)
&& let Some(user) = message.from()
&& let Some(caps) = MATCH_REGEX.captures(&text)
{
let text = text.replace(&caps[HOST_MATCH_GROUP], "ddinstagram.com");
let text = format!(
"{}: {}",
link(user.url().as_str(), &user.full_name()),
text
);
let text = format!("{}: {}", link(user.url().as_str(), &user.full_name()), text);
let _del = bot.delete_message(message.chat.id, message.id).await;
bot.try_reply(message, text).await?;
}

View File

@ -61,14 +61,12 @@ pub async fn handler(
bot: Bot,
message: Message,
) -> Result<(), Box<dyn Error + Sync + Send + 'static>> {
if let Some(text) = scrub_urls(&message) && let Some(user) = message.from() &&
let Some(caps) = MATCH_REGEX.captures(&text) {
if let Some(text) = scrub_urls(&message)
&& let Some(user) = message.from()
&& let Some(caps) = MATCH_REGEX.captures(&text)
{
let text = text.replace(&caps[HOST_MATCH_GROUP], "medium.rip");
let text = format!(
"{}: {}",
link(user.url().as_str(), &user.full_name()),
text
);
let text = format!("{}: {}", link(user.url().as_str(), &user.full_name()), text);
let _del = bot.delete_message(message.chat.id, message.id).await;
bot.try_reply(message, text).await?;
}

View File

@ -64,21 +64,19 @@ pub async fn handler(
bot: Bot,
message: Message,
) -> Result<(), Box<dyn Error + Sync + Send + 'static>> {
if let Some(text) = scrub_urls(&message) && let Some(user) = message.from() &&
let Some(caps) = MATCH_REGEX.captures(&text) {
if let Some(text) = scrub_urls(&message)
&& let Some(user) = message.from()
&& let Some(caps) = MATCH_REGEX.captures(&text)
{
let text = match &caps[ROOT_MATCH_GROUP] {
"twitter" => text.replace(&caps[HOST_MATCH_GROUP], "vxtwitter.com"),
"x" => text.replace(&caps[HOST_MATCH_GROUP], "fixupx.com"),
_ => {
tracing::trace!("No URL match found in {text}");
return Ok(())
},
return Ok(());
}
};
let text = format!(
"{}: {}",
link(user.url().as_str(), &user.full_name()),
text
);
let text = format!("{}: {}", link(user.url().as_str(), &user.full_name()), text);
let _del = bot.delete_message(message.chat.id, message.id).await;
bot.try_reply(message, text).await?;
}

View File

@ -4,8 +4,13 @@ use teloxide::types::{Message, MessageEntityKind};
use tracing::{error, info};
pub(crate) fn get_urls_from_message(msg: &Message) -> Vec<String> {
if let Some(entities) = msg.entities() && !entities.is_empty() && let Some(text) = msg.text() {
if entities[0].kind == MessageEntityKind::BotCommand { return Vec::new(); };
if let Some(entities) = msg.entities()
&& !entities.is_empty()
&& let Some(text) = msg.text()
{
if entities[0].kind == MessageEntityKind::BotCommand {
return Vec::new();
};
let url_entities: Vec<_> = entities
.iter()
.filter(|entity| entity.kind == MessageEntityKind::Url)
@ -16,7 +21,9 @@ pub(crate) fn get_urls_from_message(msg: &Message) -> Vec<String> {
let utf16 = text.encode_utf16().collect::<Vec<u16>>();
let mut urls = Vec::with_capacity(url_entities.len());
for entity in &url_entities {
urls.push(String::from_utf16_lossy(&utf16[entity.offset..entity.offset + entity.length]));
urls.push(String::from_utf16_lossy(
&utf16[entity.offset..entity.offset + entity.length],
));
}
info!(message_id = %msg.id.0, ?urls, "get_urls_from_message");
return urls;
@ -29,7 +36,9 @@ pub(crate) fn scrub_urls(msg: &Message) -> Option<String> {
let urls = get_urls_from_message(msg);
let mut final_text = text.to_owned();
for item in urls {
if let Ok(url) = Url::parse(&item) && let Some(query_str) = url.query() {
if let Ok(url) = Url::parse(&item)
&& let Some(query_str) = url.query()
{
let scrubbed_url = item.replace(&format!("?{query_str}"), "");
final_text = final_text.replace(&item, &scrubbed_url);
}

View File

@ -60,14 +60,12 @@ pub async fn handler(
bot: Bot,
message: Message,
) -> Result<(), Box<dyn Error + Sync + Send + 'static>> {
if let Some(text) = scrub_urls(&message) && let Some(user) = message.from() &&
let Some(caps) = MATCH_REGEX.captures(&text) {
if let Some(text) = scrub_urls(&message)
&& let Some(user) = message.from()
&& let Some(caps) = MATCH_REGEX.captures(&text)
{
let text = text.replace(&caps["shorts"], "watch?v=");
let text = format!(
"{}: {}",
link(user.url().as_str(), &user.full_name()),
text
);
let text = format!("{}: {}", link(user.url().as_str(), &user.full_name()), text);
let _del = bot.delete_message(message.chat.id, message.id).await;
bot.try_reply(message, text).await?;
}