diff --git a/src/bots/approved_bot/mod.rs b/src/bots/approved_bot/mod.rs index 99125f7..597ae3a 100644 --- a/src/bots/approved_bot/mod.rs +++ b/src/bots/approved_bot/mod.rs @@ -3,6 +3,7 @@ pub mod services; mod tools; use moka::future::Cache; +use smallvec::SmallVec; use teloxide::{prelude::*, types::BotCommand, adaptors::{Throttle, CacheMe}}; use crate::{bots::approved_bot::services::user_settings::create_or_update_user_settings, bots_manager::AppState}; @@ -23,7 +24,7 @@ async fn _update_activity( me: teloxide::types::Me, user: teloxide::types::User, activity_cache: Cache, - user_langs_cache: Cache>, + user_langs_cache: Cache>, ) -> Option<()> { if activity_cache.contains_key(&user.id) { return None; diff --git a/src/bots/approved_bot/modules/book.rs b/src/bots/approved_bot/modules/book.rs index 1296fd2..2fcd928 100644 --- a/src/bots/approved_bot/modules/book.rs +++ b/src/bots/approved_bot/modules/book.rs @@ -3,6 +3,7 @@ use std::str::FromStr; use moka::future::Cache; use regex::Regex; +use smallvec::SmallVec; use teloxide::{dispatching::UpdateFilterExt, dptree, prelude::*, adaptors::{Throttle, CacheMe}}; use crate::{bots::approved_bot::{ @@ -118,8 +119,8 @@ async fn send_book_handler( message: Message, bot: CacheMe>, command: BookCommand, - books_getter: fn(id: u32, page: u32, allowed_langs: Vec) -> Fut, - user_langs_cache: Cache>, + books_getter: fn(id: u32, page: u32, allowed_langs: SmallVec<[String; 3]>) -> Fut, + user_langs_cache: Cache>, ) -> crate::bots::BotHandlerInternal where T: Format + Clone + Debug, @@ -190,8 +191,8 @@ async fn send_pagination_book_handler( cq: CallbackQuery, bot: CacheMe>, callback_data: BookCallbackData, - books_getter: fn(id: u32, page: u32, allowed_langs: Vec) -> Fut, - user_langs_cache: Cache>, + books_getter: fn(id: u32, page: u32, allowed_langs: SmallVec<[String; 3]>) -> Fut, + user_langs_cache: Cache>, ) -> crate::bots::BotHandlerInternal where T: Format + Clone + Debug, diff --git a/src/bots/approved_bot/modules/download.rs b/src/bots/approved_bot/modules/download.rs index 337c3c8..f3e2bc1 100644 --- a/src/bots/approved_bot/modules/download.rs +++ b/src/bots/approved_bot/modules/download.rs @@ -438,7 +438,7 @@ async fn download_archive( object_id: id, object_type: task_type, file_format: file_type, - allowed_langs + allowed_langs: allowed_langs.into_vec(), }).await; let mut task = match task { diff --git a/src/bots/approved_bot/modules/random.rs b/src/bots/approved_bot/modules/random.rs index eccf2a4..d808314 100644 --- a/src/bots/approved_bot/modules/random.rs +++ b/src/bots/approved_bot/modules/random.rs @@ -1,4 +1,5 @@ use moka::future::Cache; +use smallvec::SmallVec; use strum_macros::{Display, EnumIter}; use teloxide::{ prelude::*, @@ -168,8 +169,8 @@ where async fn get_random_item_handler( cq: CallbackQuery, bot: CacheMe>, - item_getter: fn(allowed_langs: Vec) -> Fut, - user_langs_cache: Cache>, + item_getter: fn(allowed_langs: SmallVec<[String; 3]>) -> Fut, + user_langs_cache: Cache>, ) -> BotHandlerInternal where T: Format, @@ -293,7 +294,7 @@ async fn get_random_book_by_genre( cq: CallbackQuery, bot: CacheMe>, genre_id: u32, - user_langs_cache: Cache>, + user_langs_cache: Cache>, ) -> BotHandlerInternal { let allowed_langs = get_user_or_default_lang_codes(cq.from.id, user_langs_cache).await; diff --git a/src/bots/approved_bot/modules/search.rs b/src/bots/approved_bot/modules/search.rs index e5e8b0e..9d873cc 100644 --- a/src/bots/approved_bot/modules/search.rs +++ b/src/bots/approved_bot/modules/search.rs @@ -3,6 +3,7 @@ use std::str::FromStr; use moka::future::Cache; use regex::Regex; +use smallvec::SmallVec; use strum_macros::EnumIter; use teloxide::{ prelude::*, @@ -111,8 +112,8 @@ async fn generic_search_pagination_handler( cq: CallbackQuery, bot: CacheMe>, search_data: SearchCallbackData, - items_getter: fn(query: String, page: u32, allowed_langs: Vec) -> Fut, - user_langs_cache: Cache>, + items_getter: fn(query: String, page: u32, allowed_langs: SmallVec<[String; 3]>) -> Fut, + user_langs_cache: Cache>, ) -> BotHandlerInternal where T: Format + Clone + Debug, diff --git a/src/bots/approved_bot/modules/settings.rs b/src/bots/approved_bot/modules/settings.rs index 31d5eb1..2cebe5c 100644 --- a/src/bots/approved_bot/modules/settings.rs +++ b/src/bots/approved_bot/modules/settings.rs @@ -12,6 +12,7 @@ use crate::{bots::{ use moka::future::Cache; use regex::Regex; +use smallvec::SmallVec; use teloxide::{ prelude::*, types::{InlineKeyboardButton, InlineKeyboardMarkup, Me}, @@ -119,7 +120,7 @@ async fn settings_callback_handler( bot: CacheMe>, callback_data: SettingsCallbackData, me: Me, - user_langs_cache: Cache>, + user_langs_cache: Cache>, ) -> BotHandlerInternal { let message = match cq.message { Some(v) => v, diff --git a/src/bots/approved_bot/services/book_library/mod.rs b/src/bots/approved_bot/services/book_library/mod.rs index 026f16a..fb6c2f0 100644 --- a/src/bots/approved_bot/services/book_library/mod.rs +++ b/src/bots/approved_bot/services/book_library/mod.rs @@ -2,12 +2,13 @@ pub mod formaters; pub mod types; use serde::de::DeserializeOwned; +use smallvec::SmallVec; use crate::config; use self::types::Empty; -fn get_allowed_langs_params(allowed_langs: Vec) -> Vec<(&'static str, String)> { +fn get_allowed_langs_params(allowed_langs: SmallVec<[String; 3]>) -> Vec<(&'static str, String)> { allowed_langs .into_iter() .map(|lang| ("allowed_langs", lang)) @@ -45,7 +46,7 @@ pub async fn get_book( } pub async fn get_random_book_by_genre( - allowed_langs: Vec, + allowed_langs: SmallVec<[String; 3]>, genre: Option, ) -> Result> { let mut params: Vec<(&str, String)> = get_allowed_langs_params(allowed_langs); @@ -58,13 +59,13 @@ pub async fn get_random_book_by_genre( } pub async fn get_random_book( - allowed_langs: Vec, + allowed_langs: SmallVec<[String; 3]>, ) -> Result> { get_random_book_by_genre(allowed_langs, None).await } pub async fn get_random_author( - allowed_langs: Vec, + allowed_langs: SmallVec<[String; 3]>, ) -> Result> { let params: Vec<(&str, String)> = get_allowed_langs_params(allowed_langs); @@ -72,7 +73,7 @@ pub async fn get_random_author( } pub async fn get_random_sequence( - allowed_langs: Vec, + allowed_langs: SmallVec<[String; 3]>, ) -> Result> { let params = get_allowed_langs_params(allowed_langs); @@ -96,7 +97,7 @@ const PAGE_SIZE: &str = "5"; pub async fn search_book( query: String, page: u32, - allowed_langs: Vec, + allowed_langs: SmallVec<[String; 3]>, ) -> Result, Box> { let mut params = get_allowed_langs_params(allowed_langs); @@ -109,7 +110,7 @@ pub async fn search_book( pub async fn search_author( query: String, page: u32, - allowed_langs: Vec, + allowed_langs: SmallVec<[String; 3]>, ) -> Result, Box> { let mut params = get_allowed_langs_params(allowed_langs); @@ -122,7 +123,7 @@ pub async fn search_author( pub async fn search_sequence( query: String, page: u32, - allowed_langs: Vec, + allowed_langs: SmallVec<[String; 3]>, ) -> Result, Box> { let mut params = get_allowed_langs_params(allowed_langs); @@ -135,7 +136,7 @@ pub async fn search_sequence( pub async fn search_translator( query: String, page: u32, - allowed_langs: Vec, + allowed_langs: SmallVec<[String; 3]>, ) -> Result, Box> { let mut params = get_allowed_langs_params(allowed_langs); @@ -164,7 +165,7 @@ pub async fn get_author_annotation( pub async fn get_author_books( id: u32, page: u32, - allowed_langs: Vec, + allowed_langs: SmallVec<[String; 3]>, ) -> Result, Box> { let mut params = get_allowed_langs_params(allowed_langs); @@ -177,7 +178,7 @@ pub async fn get_author_books( pub async fn get_translator_books( id: u32, page: u32, - allowed_langs: Vec, + allowed_langs: SmallVec<[String; 3]>, ) -> Result, Box> { let mut params = get_allowed_langs_params(allowed_langs); @@ -190,7 +191,7 @@ pub async fn get_translator_books( pub async fn get_sequence_books( id: u32, page: u32, - allowed_langs: Vec, + allowed_langs: SmallVec<[String; 3]>, ) -> Result, Box> { let mut params = get_allowed_langs_params(allowed_langs); @@ -218,7 +219,7 @@ pub async fn get_uploaded_books( pub async fn get_author_books_available_types( id: u32, - allowed_langs: Vec, + allowed_langs: SmallVec<[String; 3]>, ) -> Result, Box> { let params = get_allowed_langs_params(allowed_langs); @@ -227,7 +228,7 @@ pub async fn get_author_books_available_types( pub async fn get_translator_books_available_types( id: u32, - allowed_langs: Vec, + allowed_langs: SmallVec<[String; 3]>, ) -> Result, Box> { let params = get_allowed_langs_params(allowed_langs); @@ -236,7 +237,7 @@ pub async fn get_translator_books_available_types( pub async fn get_sequence_books_available_types( id: u32, - allowed_langs: Vec + allowed_langs: SmallVec<[String; 3]> ) -> Result, Box> { let params = get_allowed_langs_params(allowed_langs); diff --git a/src/bots/approved_bot/services/user_settings/mod.rs b/src/bots/approved_bot/services/user_settings/mod.rs index 40d4d25..562abce 100644 --- a/src/bots/approved_bot/services/user_settings/mod.rs +++ b/src/bots/approved_bot/services/user_settings/mod.rs @@ -1,6 +1,7 @@ use moka::future::Cache; use serde::Deserialize; use serde_json::json; +use smallvec::{SmallVec, smallvec}; use teloxide::{types::{UserId, ChatId}}; use crate::config; @@ -41,17 +42,17 @@ pub async fn get_user_settings( pub async fn get_user_or_default_lang_codes( user_id: UserId, - cache: Cache> -) -> Vec { + cache: Cache> +) -> SmallVec<[String; 3]> { if let Some(cached_langs) = cache.get(&user_id) { return cached_langs; } - let default_lang_codes = vec![String::from("ru"), String::from("be"), String::from("uk")]; + let default_lang_codes = smallvec![String::from("ru"), String::from("be"), String::from("uk")]; match get_user_settings(user_id).await { Ok(v) => { - let langs: Vec = v.allowed_langs.into_iter().map(|lang| lang.code).collect(); + let langs: SmallVec<[String; 3]> = v.allowed_langs.into_iter().map(|lang| lang.code).collect(); cache.insert(user_id, langs.clone()).await; langs }, @@ -65,8 +66,8 @@ pub async fn create_or_update_user_settings( first_name: String, username: String, source: String, - allowed_langs: Vec, - cache: Cache> + allowed_langs: SmallVec<[String; 3]>, + cache: Cache> ) -> Result> { cache.invalidate(&user_id).await; @@ -76,7 +77,7 @@ pub async fn create_or_update_user_settings( "first_name": first_name, "username": username, "source": source, - "allowed_langs": allowed_langs + "allowed_langs": allowed_langs.into_vec() }); let response = reqwest::Client::new() diff --git a/src/bots_manager/mod.rs b/src/bots_manager/mod.rs index d04ed40..559060d 100644 --- a/src/bots_manager/mod.rs +++ b/src/bots_manager/mod.rs @@ -6,6 +6,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use axum::Router; +use smallvec::SmallVec; use teloxide::adaptors::throttle::Limits; use teloxide::types::BotCommand; use tokio::time::{sleep, Duration}; @@ -25,7 +26,7 @@ use crate::config; #[derive(Clone)] pub struct AppState { pub user_activity_cache: Cache, - pub user_langs_cache: Cache>, + pub user_langs_cache: Cache>, pub chat_donation_notifications_cache: Cache, }