From 24310df9b305b077ae540701445a90c40c8ad4cb Mon Sep 17 00:00:00 2001 From: Henriquelay <37563861+Henriquelay@users.noreply.github.com> Date: Tue, 6 Feb 2024 01:25:41 -0300 Subject: [PATCH 1/4] Implement `GetChatId` for `teloxide-core::types::*` --- CHANGELOG.md | 1 + .../src/dispatching/dialogue/get_chat_id.rs | 105 ++++++++++++++++-- 2 files changed, 98 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f458549..b72fa104 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `filter_video_chat_ended` - `filter_video_chat_participants_invited` - `filter_web_app_data` +- Implement `GetChatId` for `teloxide_core::types::{BotCommandScope, Chat, ChatJoinRequest, ChatMemberUpdated, MessageCommon, Recipient, ResponseParameters, TargetMessage, UpdateKind}`. ### Fixed diff --git a/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs b/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs index a0e72f8b..c80bd76a 100644 --- a/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs +++ b/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs @@ -1,25 +1,114 @@ -use crate::types::{CallbackQuery, ChatId, Message, Update}; +use crate::types::{ + BotCommandScope, CallbackQuery, Chat, ChatId, ChatJoinRequest, ChatMemberUpdated, Message, + MessageCommon, Recipient, ResponseParameters, TargetMessage, Update, UpdateKind, +}; -/// Something that may has a chat ID. +/// Something that may have a chat ID. pub trait GetChatId { #[must_use] fn chat_id(&self) -> Option; } -impl GetChatId for Message { - fn chat_id(&self) -> Option { - Some(self.chat.id) - } -} - impl GetChatId for CallbackQuery { fn chat_id(&self) -> Option { self.message.as_ref().map(|mes| mes.chat.id) } } +impl GetChatId for MessageCommon { + fn chat_id(&self) -> Option { + self.sender_chat.as_ref().map(|chat| chat.id) + } +} + impl GetChatId for Update { fn chat_id(&self) -> Option { self.chat().map(|chat| chat.id) } } + +impl GetChatId for Recipient { + fn chat_id(&self) -> Option { + match self { + Recipient::Id(chat_id) => Some(*chat_id), + Recipient::ChannelUsername(_) => None, + } + } +} + +impl GetChatId for BotCommandScope { + fn chat_id(&self) -> Option { + match self { + BotCommandScope::Default + | BotCommandScope::AllPrivateChats + | BotCommandScope::AllGroupChats + | BotCommandScope::AllChatAdministrators => None, + BotCommandScope::Chat { chat_id: recipient } + | BotCommandScope::ChatAdministrators { chat_id: recipient } + | BotCommandScope::ChatMember { chat_id: recipient, .. } => recipient.chat_id(), + } + } +} + +impl GetChatId for Chat { + fn chat_id(&self) -> Option { + Some(self.id) + } +} + +impl GetChatId for UpdateKind { + fn chat_id(&self) -> Option { + match self { + UpdateKind::Message(message) + | UpdateKind::EditedMessage(message) + | UpdateKind::ChannelPost(message) + | UpdateKind::EditedChannelPost(message) => GetChatId::chat_id(message), + UpdateKind::CallbackQuery(callback_query) => callback_query.chat_id(), + UpdateKind::MyChatMember(chat_member_updated) => chat_member_updated.chat_id(), + UpdateKind::ChatMember(chat_member_updated) => chat_member_updated.chat_id(), + UpdateKind::ChatJoinRequest(chat_join_request) => chat_join_request.chat_id(), + UpdateKind::InlineQuery(_) + | UpdateKind::ChosenInlineResult(_) + | UpdateKind::ShippingQuery(_) + | UpdateKind::PreCheckoutQuery(_) + | UpdateKind::Poll(_) + | UpdateKind::PollAnswer(_) + | UpdateKind::Error(_) => None, + } + } +} + +impl GetChatId for ResponseParameters { + fn chat_id(&self) -> Option { + match self { + ResponseParameters::MigrateToChatId(chat_id) => Some(*chat_id), + ResponseParameters::RetryAfter(_) => None, + } + } +} + +impl GetChatId for TargetMessage { + fn chat_id(&self) -> Option { + match self { + TargetMessage::Common { chat_id: recipient, .. } => recipient.chat_id(), + TargetMessage::Inline { .. } => None, + } + } +} + +/// Implements [`GetChatId`] for all types passed in, as long as they have a +/// `chat` field with a `Chat` type +macro_rules! impl_GetChatId_for_chat_field { + // Comma-separated list of types bound to `t` + ($($t:ty),* $(,)?) => { + $( + impl GetChatId for $t { + fn chat_id(&self) -> Option { + Some(self.chat.id) + } + } + )* + }; +} + +impl_GetChatId_for_chat_field!(Message, ChatMemberUpdated, ChatJoinRequest); From 301f5929ad4f4a08f25cc65dded0550512a65187 Mon Sep 17 00:00:00 2001 From: Henriquelay <37563861+Henriquelay@users.noreply.github.com> Date: Thu, 8 Feb 2024 02:59:53 -0300 Subject: [PATCH 2/4] Remove impl GetChatId for UpdateKind --- CHANGELOG.md | 2 +- .../src/dispatching/dialogue/get_chat_id.rs | 53 ++++++------------- 2 files changed, 17 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b72fa104..25418fef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `filter_video_chat_ended` - `filter_video_chat_participants_invited` - `filter_web_app_data` -- Implement `GetChatId` for `teloxide_core::types::{BotCommandScope, Chat, ChatJoinRequest, ChatMemberUpdated, MessageCommon, Recipient, ResponseParameters, TargetMessage, UpdateKind}`. +- Implement `GetChatId` for `teloxide_core::types::{BotCommandScope, Chat, ChatJoinRequest, ChatMemberUpdated, MessageCommon, Recipient, ResponseParameters, TargetMessage}`. ### Fixed diff --git a/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs b/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs index c80bd76a..30f8f41d 100644 --- a/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs +++ b/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs @@ -1,6 +1,6 @@ use crate::types::{ BotCommandScope, CallbackQuery, Chat, ChatId, ChatJoinRequest, ChatMemberUpdated, Message, - MessageCommon, Recipient, ResponseParameters, TargetMessage, Update, UpdateKind, + MessageCommon, Recipient, ResponseParameters, TargetMessage, Update, }; /// Something that may have a chat ID. @@ -56,28 +56,6 @@ impl GetChatId for Chat { } } -impl GetChatId for UpdateKind { - fn chat_id(&self) -> Option { - match self { - UpdateKind::Message(message) - | UpdateKind::EditedMessage(message) - | UpdateKind::ChannelPost(message) - | UpdateKind::EditedChannelPost(message) => GetChatId::chat_id(message), - UpdateKind::CallbackQuery(callback_query) => callback_query.chat_id(), - UpdateKind::MyChatMember(chat_member_updated) => chat_member_updated.chat_id(), - UpdateKind::ChatMember(chat_member_updated) => chat_member_updated.chat_id(), - UpdateKind::ChatJoinRequest(chat_join_request) => chat_join_request.chat_id(), - UpdateKind::InlineQuery(_) - | UpdateKind::ChosenInlineResult(_) - | UpdateKind::ShippingQuery(_) - | UpdateKind::PreCheckoutQuery(_) - | UpdateKind::Poll(_) - | UpdateKind::PollAnswer(_) - | UpdateKind::Error(_) => None, - } - } -} - impl GetChatId for ResponseParameters { fn chat_id(&self) -> Option { match self { @@ -96,19 +74,20 @@ impl GetChatId for TargetMessage { } } -/// Implements [`GetChatId`] for all types passed in, as long as they have a -/// `chat` field with a `Chat` type -macro_rules! impl_GetChatId_for_chat_field { - // Comma-separated list of types bound to `t` - ($($t:ty),* $(,)?) => { - $( - impl GetChatId for $t { - fn chat_id(&self) -> Option { - Some(self.chat.id) - } - } - )* - }; +impl GetChatId for Message { + fn chat_id(&self) -> Option { + Some(self.chat.id) + } } -impl_GetChatId_for_chat_field!(Message, ChatMemberUpdated, ChatJoinRequest); +impl GetChatId for ChatMemberUpdated { + fn chat_id(&self) -> Option { + Some(self.chat.id) + } +} + +impl GetChatId for ChatJoinRequest { + fn chat_id(&self) -> Option { + Some(self.chat.id) + } +} From 69874823d7b03ce8d13034686f239bb88b1d1c8b Mon Sep 17 00:00:00 2001 From: Henriquelay <37563861+Henriquelay@users.noreply.github.com> Date: Tue, 5 Mar 2024 22:19:53 -0300 Subject: [PATCH 3/4] Remove useless impl, order diff --- CHANGELOG.md | 2 +- .../src/dispatching/dialogue/get_chat_id.rs | 41 +++---------------- 2 files changed, 7 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25418fef..b570b7f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `filter_video_chat_ended` - `filter_video_chat_participants_invited` - `filter_web_app_data` -- Implement `GetChatId` for `teloxide_core::types::{BotCommandScope, Chat, ChatJoinRequest, ChatMemberUpdated, MessageCommon, Recipient, ResponseParameters, TargetMessage}`. +- Implement `GetChatId` for `teloxide_core::types::{Chat, ChatJoinRequest, ChatMemberUpdated, Recipient, TargetMessage}`. ### Fixed diff --git a/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs b/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs index 30f8f41d..2b27b4a2 100644 --- a/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs +++ b/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs @@ -1,6 +1,6 @@ use crate::types::{ - BotCommandScope, CallbackQuery, Chat, ChatId, ChatJoinRequest, ChatMemberUpdated, Message, - MessageCommon, Recipient, ResponseParameters, TargetMessage, Update, + CallbackQuery, Chat, ChatId, ChatJoinRequest, ChatMemberUpdated, Message, + Recipient, TargetMessage, Update, }; /// Something that may have a chat ID. @@ -9,15 +9,15 @@ pub trait GetChatId { fn chat_id(&self) -> Option; } -impl GetChatId for CallbackQuery { +impl GetChatId for Message { fn chat_id(&self) -> Option { - self.message.as_ref().map(|mes| mes.chat.id) + Some(self.chat.id) } } -impl GetChatId for MessageCommon { +impl GetChatId for CallbackQuery { fn chat_id(&self) -> Option { - self.sender_chat.as_ref().map(|chat| chat.id) + self.message.as_ref().map(|mes| mes.chat.id) } } @@ -36,35 +36,12 @@ impl GetChatId for Recipient { } } -impl GetChatId for BotCommandScope { - fn chat_id(&self) -> Option { - match self { - BotCommandScope::Default - | BotCommandScope::AllPrivateChats - | BotCommandScope::AllGroupChats - | BotCommandScope::AllChatAdministrators => None, - BotCommandScope::Chat { chat_id: recipient } - | BotCommandScope::ChatAdministrators { chat_id: recipient } - | BotCommandScope::ChatMember { chat_id: recipient, .. } => recipient.chat_id(), - } - } -} - impl GetChatId for Chat { fn chat_id(&self) -> Option { Some(self.id) } } -impl GetChatId for ResponseParameters { - fn chat_id(&self) -> Option { - match self { - ResponseParameters::MigrateToChatId(chat_id) => Some(*chat_id), - ResponseParameters::RetryAfter(_) => None, - } - } -} - impl GetChatId for TargetMessage { fn chat_id(&self) -> Option { match self { @@ -74,12 +51,6 @@ impl GetChatId for TargetMessage { } } -impl GetChatId for Message { - fn chat_id(&self) -> Option { - Some(self.chat.id) - } -} - impl GetChatId for ChatMemberUpdated { fn chat_id(&self) -> Option { Some(self.chat.id) From 77909e04b0e924efe81836f66ef2ab16247c98e9 Mon Sep 17 00:00:00 2001 From: Henriquelay <37563861+Henriquelay@users.noreply.github.com> Date: Thu, 7 Mar 2024 00:51:10 -0300 Subject: [PATCH 4/4] Remove meaningless impls --- CHANGELOG.md | 2 +- .../src/dispatching/dialogue/get_chat_id.rs | 21 +------------------ 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b570b7f8..69728953 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `filter_video_chat_ended` - `filter_video_chat_participants_invited` - `filter_web_app_data` -- Implement `GetChatId` for `teloxide_core::types::{Chat, ChatJoinRequest, ChatMemberUpdated, Recipient, TargetMessage}`. +- Implement `GetChatId` for `teloxide_core::types::{Chat, ChatJoinRequest, ChatMemberUpdated}`. ### Fixed diff --git a/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs b/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs index 2b27b4a2..632f4bbe 100644 --- a/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs +++ b/crates/teloxide/src/dispatching/dialogue/get_chat_id.rs @@ -1,6 +1,5 @@ use crate::types::{ - CallbackQuery, Chat, ChatId, ChatJoinRequest, ChatMemberUpdated, Message, - Recipient, TargetMessage, Update, + CallbackQuery, Chat, ChatId, ChatJoinRequest, ChatMemberUpdated, Message, Update, }; /// Something that may have a chat ID. @@ -27,30 +26,12 @@ impl GetChatId for Update { } } -impl GetChatId for Recipient { - fn chat_id(&self) -> Option { - match self { - Recipient::Id(chat_id) => Some(*chat_id), - Recipient::ChannelUsername(_) => None, - } - } -} - impl GetChatId for Chat { fn chat_id(&self) -> Option { Some(self.id) } } -impl GetChatId for TargetMessage { - fn chat_id(&self) -> Option { - match self { - TargetMessage::Common { chat_id: recipient, .. } => recipient.chat_id(), - TargetMessage::Inline { .. } => None, - } - } -} - impl GetChatId for ChatMemberUpdated { fn chat_id(&self) -> Option { Some(self.chat.id)