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 { rustNightly = (import fenix {inherit pkgs;}).fromToolchainFile {
file = ./rust-toolchain.toml; file = ./rust-toolchain.toml;
sha256 = "sha256-3+hzpuIkf2KFGyop0DgMPI5nCY6IQX8I6XdIUwn4nAI="; sha256 = "sha256-Je+rS6Tgc04HqvDj2XvjOpbyIAcEee0JqqrG7+cUydI=";
}; };
craneLib = (crane.mkLib pkgs).overrideToolchain rustNightly; craneLib = (crane.mkLib pkgs).overrideToolchain rustNightly;

View File

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

View File

@ -58,12 +58,13 @@ pub(crate) async fn handler(
Command::Ping => { Command::Ping => {
bot.send_chat_action(message.chat.id, ChatAction::Typing) bot.send_chat_action(message.chat.id, ChatAction::Typing)
.await?; .await?;
bot.send_message(message.chat.id, "Pong") bot.send_message(message.chat.id, "Pong").await?;
.await?; }
},
#[cfg(feature = "ddinstagram")] #[cfg(feature = "ddinstagram")]
Command::Instagram { filter_state } => { 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) bot.send_chat_action(message.chat.id, ChatAction::Typing)
.await?; .await?;
bot.send_message(message.chat.id, "You are not authorized for this action") 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 } => { 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) bot.send_chat_action(message.chat.id, ChatAction::Typing)
.await?; .await?;
bot.send_message(message.chat.id, "You are not authorized for this action") bot.send_message(message.chat.id, "You are not authorized for this action")
@ -113,7 +116,9 @@ pub(crate) async fn handler(
.await?; .await?;
} }
Command::Twitter { filter_state } => { 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) bot.send_chat_action(message.chat.id, ChatAction::Typing)
.await?; .await?;
bot.send_message(message.chat.id, "You are not authorized for this action") 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 } => { 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) bot.send_chat_action(message.chat.id, ChatAction::Typing)
.await?; .await?;
bot.send_message(message.chat.id, "You are not authorized for this action") 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, bot: Bot,
message: Message, message: Message,
) -> Result<(), Box<dyn Error + Sync + Send + 'static>> { ) -> 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 mut text = String::from_str(text)?;
let urls = get_urls_from_message(&message); let urls = get_urls_from_message(&message);
debug!(?urls); debug!(?urls);
@ -34,11 +36,7 @@ pub async fn handler(
return Ok(()); return Ok(());
} }
} }
let text = format!( let text = format!("{}: {}", link(user.url().as_str(), &user.full_name()), text);
"{}: {}",
link(user.url().as_str(), &user.full_name()),
text
);
let _del = bot.delete_message(message.chat.id, message.id).await; let _del = bot.delete_message(message.chat.id, message.id).await;
bot.try_reply(message, text).await?; bot.try_reply(message, text).await?;
} }
@ -53,7 +51,9 @@ pub fn is_amp(msg: Message) -> bool {
return false; return false;
} }
urls.iter().flat_map(|url| Url::parse(url)).any(|url| { 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") segments.any(|x| x == "amp") || host.ends_with(".cdn.ampproject.org")
} else { } else {
false false

View File

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

View File

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

View File

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

View File

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

View File

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