diff --git a/Cargo.toml b/Cargo.toml index afc8adb3..b4ae61d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -121,6 +121,7 @@ service_telegram = [ service_brooch = [ "interface_database", "interface_stratz", + "teloxide", "graphql_client" ] diff --git a/src/instance/config.rs b/src/instance/config.rs index a8036375..9770ea47 100644 --- a/src/instance/config.rs +++ b/src/instance/config.rs @@ -39,17 +39,17 @@ pub mod brooch { } } -#[cfg(feature = "service_telegram")] +#[cfg(any(feature = "service_telegram", feature = "service_brooch"))] pub struct ChatIdConversionHack(i64); -#[cfg(feature = "service_telegram")] +#[cfg(any(feature = "service_telegram", feature = "service_brooch"))] impl From for ChatIdConversionHack { fn from(value: i64) -> Self { Self(value) } } -#[cfg(feature = "service_telegram")] +#[cfg(any(feature = "service_telegram", feature = "service_brooch"))] impl From for teloxide::types::ChatId { fn from(value: ChatIdConversionHack) -> Self { Self(value.0) diff --git a/src/interfaces/database/models/matchmaking_messages_telegram.rs b/src/interfaces/database/models/matchmaking_messages_telegram.rs index 4c180348..b6f187ec 100644 --- a/src/interfaces/database/models/matchmaking_messages_telegram.rs +++ b/src/interfaces/database/models/matchmaking_messages_telegram.rs @@ -20,8 +20,6 @@ pub struct MatchmakingMessageTelegram { #[cfg(feature = "service_telegram")] pub(crate) mod telegram_ext { - use std::str::FromStr; - use anyhow::Context; use chrono::Local; use diesel::PgConnection; @@ -36,69 +34,6 @@ pub(crate) mod telegram_ext { use super::*; - #[derive(Debug, Clone, Copy, PartialEq, Eq)] - pub enum MatchmakingTelegramKeyboardCallback { - Yes, - Plus5Min, - Plus15Min, - Plus60Min, - Maybe, - DontWait, - Cant, - Wont, - } - - impl MatchmakingTelegramKeyboardCallback { - /// Create callback data representing the [MatchmakingTelegramKeyboardCallback] in the given [MatchmakingId]. - pub fn callback_data(self, matchmaking_id: MatchmakingId) -> String { - matchmaking_id.callback_data(self.into()) - } - - pub fn inline_button(self, matchmaking_id: MatchmakingId, text: &str) -> teloxide::types::InlineKeyboardButton { - teloxide::types::InlineKeyboardButton::new( - text, - teloxide::types::InlineKeyboardButtonKind::CallbackData( - self.callback_data(matchmaking_id) - ), - ) - } - } - - impl FromStr for MatchmakingTelegramKeyboardCallback { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - Ok( - match s { - "yes" => Self::Yes, - "5min" => Self::Plus5Min, - "15min" => Self::Plus15Min, - "60min" => Self::Plus60Min, - "maybe" => Self::Maybe, - "dontw" => Self::DontWait, - "cant" => Self::Cant, - "wont" => Self::Wont, - x => anyhow::bail!("Unknown keyboard callback: {x:?}"), - } - ) - } - } - - impl From for &'static str { - fn from(value: MatchmakingTelegramKeyboardCallback) -> Self { - match value { - MatchmakingTelegramKeyboardCallback::Yes => "yes", - MatchmakingTelegramKeyboardCallback::Plus5Min => "5min", - MatchmakingTelegramKeyboardCallback::Plus15Min => "15min", - MatchmakingTelegramKeyboardCallback::Plus60Min => "60min", - MatchmakingTelegramKeyboardCallback::Maybe => "maybe", - MatchmakingTelegramKeyboardCallback::DontWait => "dontw", - MatchmakingTelegramKeyboardCallback::Cant => "cant", - MatchmakingTelegramKeyboardCallback::Wont => "wont", - } - } - } - impl MatchmakingMessageTelegram { /// Get all the [MatchmakingMessageTelegram] for a specific [MatchmakingId]. pub fn get_all(database: &mut PgConnection, matchmaking_id: MatchmakingId) -> AnyResult> { @@ -111,7 +46,7 @@ pub(crate) mod telegram_ext { } fn reply_markup(matchmaking_id: MatchmakingId) -> teloxide::types::InlineKeyboardMarkup { - use MatchmakingTelegramKeyboardCallback::*; + use crate::services::telegram::utils::matchmaking::MatchmakingTelegramKeyboardCallback::*; let button_yes = Yes.inline_button(matchmaking_id, "🔵 Ci sarò!"); let button_5min = Plus5Min.inline_button(matchmaking_id, "🕐 +5 min"); diff --git a/src/interfaces/database/models/mod.rs b/src/interfaces/database/models/mod.rs index 5d50e25b..68722d19 100644 --- a/src/interfaces/database/models/mod.rs +++ b/src/interfaces/database/models/mod.rs @@ -6,14 +6,12 @@ use diesel::deserialize::FromSql; use diesel::serialize::ToSql; pub use brooch_match::{BroochMatch, DotaMatchId}; -pub use diario::{Diario, DiarioId}; -pub use discord::DiscordUserId; +pub use diario::Diario; pub use matchmaking_choice::MatchmakingChoice; pub use matchmaking_events::{MatchmakingEvent, MatchmakingId}; -pub use matchmaking_messages_telegram::{MatchmakingMessageTelegram, telegram_ext::MatchmakingTelegramKeyboardCallback}; +pub use matchmaking_messages_telegram::MatchmakingMessageTelegram; pub use matchmaking_replies::MatchmakingReply; -pub use steam::SteamId64; -pub use telegram::{TelegramChatId, TelegramMessageId, TelegramUser, TelegramUserId}; +pub use telegram::{TelegramUser, TelegramUserId}; pub use users::{RoyalnetUser, RoyalnetUserId}; mod users; diff --git a/src/services/telegram/keyboard_callbacks/matchmaking.rs b/src/services/telegram/keyboard_callbacks/matchmaking.rs index f1945dbe..abae2196 100644 --- a/src/services/telegram/keyboard_callbacks/matchmaking.rs +++ b/src/services/telegram/keyboard_callbacks/matchmaking.rs @@ -4,12 +4,13 @@ use teloxide::payloads::AnswerCallbackQuerySetters; use teloxide::requests::Requester; use teloxide::types::CallbackQuery; -use crate::interfaces::database::models::{MatchmakingChoice, MatchmakingId, MatchmakingMessageTelegram, MatchmakingReply, MatchmakingTelegramKeyboardCallback, RoyalnetUser}; +use crate::interfaces::database::models::{MatchmakingChoice, MatchmakingId, MatchmakingMessageTelegram, MatchmakingReply, RoyalnetUser}; use crate::services::telegram::dependencies::interface_database::DatabaseInterface; use crate::services::telegram::keyboard_callbacks::KeyboardCallbackResult; +use crate::services::telegram::utils::matchmaking::MatchmakingTelegramKeyboardCallback; pub async fn handler(bot: &Bot, query: CallbackQuery, matchmaking_id: MatchmakingId, callback: MatchmakingTelegramKeyboardCallback, database: &DatabaseInterface) -> KeyboardCallbackResult { - use MatchmakingTelegramKeyboardCallback::*; + use crate::services::telegram::utils::matchmaking::MatchmakingTelegramKeyboardCallback::*; let mut database = database.connect() .context("Non è stato possibile connettersi al database RYG.")?; diff --git a/src/services/telegram/keyboard_callbacks/mod.rs b/src/services/telegram/keyboard_callbacks/mod.rs index 44432f33..52e341ea 100644 --- a/src/services/telegram/keyboard_callbacks/mod.rs +++ b/src/services/telegram/keyboard_callbacks/mod.rs @@ -7,8 +7,9 @@ use teloxide::payloads::AnswerCallbackQuerySetters; use teloxide::prelude::CallbackQuery; use teloxide::requests::Requester; -use crate::interfaces::database::models::{MatchmakingId, MatchmakingTelegramKeyboardCallback}; +use crate::interfaces::database::models::MatchmakingId; use crate::services::telegram::dependencies::interface_database::DatabaseInterface; +use crate::services::telegram::utils::matchmaking::MatchmakingTelegramKeyboardCallback; mod matchmaking; diff --git a/src/services/telegram/mod.rs b/src/services/telegram/mod.rs index 5ed71ddb..085360bc 100644 --- a/src/services/telegram/mod.rs +++ b/src/services/telegram/mod.rs @@ -19,7 +19,7 @@ use super::RoyalnetService; mod commands; mod dependencies; mod keyboard_callbacks; -mod utils; +pub(crate) mod utils; #[derive(Debug, Clone)] pub struct TelegramService { diff --git a/src/services/telegram/utils/matchmaking.rs b/src/services/telegram/utils/matchmaking.rs new file mode 100644 index 00000000..09ea336b --- /dev/null +++ b/src/services/telegram/utils/matchmaking.rs @@ -0,0 +1,66 @@ +use std::str::FromStr; + +use crate::interfaces::database::models::MatchmakingId; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum MatchmakingTelegramKeyboardCallback { + Yes, + Plus5Min, + Plus15Min, + Plus60Min, + Maybe, + DontWait, + Cant, + Wont, +} + +impl MatchmakingTelegramKeyboardCallback { + /// Create callback data representing the [MatchmakingTelegramKeyboardCallback] in the given [MatchmakingId]. + pub fn callback_data(self, matchmaking_id: MatchmakingId) -> String { + matchmaking_id.callback_data(self.into()) + } + + pub fn inline_button(self, matchmaking_id: MatchmakingId, text: &str) -> teloxide::types::InlineKeyboardButton { + teloxide::types::InlineKeyboardButton::new( + text, + teloxide::types::InlineKeyboardButtonKind::CallbackData( + self.callback_data(matchmaking_id) + ), + ) + } +} + +impl FromStr for MatchmakingTelegramKeyboardCallback { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + Ok( + match s { + "yes" => Self::Yes, + "5min" => Self::Plus5Min, + "15min" => Self::Plus15Min, + "60min" => Self::Plus60Min, + "maybe" => Self::Maybe, + "dontw" => Self::DontWait, + "cant" => Self::Cant, + "wont" => Self::Wont, + x => anyhow::bail!("Unknown keyboard callback: {x:?}"), + } + ) + } +} + +impl From for &'static str { + fn from(value: MatchmakingTelegramKeyboardCallback) -> Self { + match value { + MatchmakingTelegramKeyboardCallback::Yes => "yes", + MatchmakingTelegramKeyboardCallback::Plus5Min => "5min", + MatchmakingTelegramKeyboardCallback::Plus15Min => "15min", + MatchmakingTelegramKeyboardCallback::Plus60Min => "60min", + MatchmakingTelegramKeyboardCallback::Maybe => "maybe", + MatchmakingTelegramKeyboardCallback::DontWait => "dontw", + MatchmakingTelegramKeyboardCallback::Cant => "cant", + MatchmakingTelegramKeyboardCallback::Wont => "wont", + } + } +} \ No newline at end of file diff --git a/src/services/telegram/utils/mod.rs b/src/services/telegram/utils/mod.rs index 6edabfad..7c3ee5e9 100644 --- a/src/services/telegram/utils/mod.rs +++ b/src/services/telegram/utils/mod.rs @@ -1 +1,2 @@ -mod database; \ No newline at end of file +pub mod database; +pub mod matchmaking; \ No newline at end of file