Merge pull request #835 from teloxide/sync-update-kinds

Synchronize places that use update kinds
This commit is contained in:
Sima Kinsart 2023-02-05 13:16:31 +06:00 committed by GitHub
commit 6909353500
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 24 deletions

View file

@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## unreleased
### Fixed
- Allow `ChatJoinRequest` updates
### Added
- `Update::filter_chat_join_request`
## 0.12.0 - 2023-01-17
### Changed

View file

@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## unreleased
### Fixed
- `Update::user` now handles channel posts, chat member changes and chat join request updates correctly ([#835][pr835])
[pr835]: https://github.com/teloxide/teloxide/pull/835
## 0.9.0 - 2023-01-17
### Changed

View file

@ -29,39 +29,69 @@ pub struct Update {
}
impl Update {
// FIXME: rename user => from, add mentioned_users -> impl Iterator<&User>
/// Returns the user that performed the action that caused this update, if
/// known.
///
/// This is generally the `from` field (except for `PollAnswer` where it's
/// `user` and `Poll` with `Error` which don't have such field at all).
#[must_use]
pub fn user(&self) -> Option<&User> {
match &self.kind {
UpdateKind::Message(m) => m.from(),
UpdateKind::EditedMessage(m) => m.from(),
UpdateKind::CallbackQuery(query) => Some(&query.from),
UpdateKind::ChosenInlineResult(chosen) => Some(&chosen.from),
UpdateKind::InlineQuery(query) => Some(&query.from),
UpdateKind::ShippingQuery(query) => Some(&query.from),
UpdateKind::PreCheckoutQuery(query) => Some(&query.from),
UpdateKind::PollAnswer(answer) => Some(&answer.user),
_ => None,
}
use UpdateKind::*;
let from = match &self.kind {
Message(m) | EditedMessage(m) | ChannelPost(m) | EditedChannelPost(m) => m.from()?,
CallbackQuery(query) => &query.from,
ChosenInlineResult(chosen) => &chosen.from,
InlineQuery(query) => &query.from,
ShippingQuery(query) => &query.from,
PreCheckoutQuery(query) => &query.from,
PollAnswer(answer) => &answer.user,
MyChatMember(m) | ChatMember(m) => &m.from,
ChatJoinRequest(r) => &r.from,
Poll(_) | Error(_) => return None,
};
Some(from)
}
/// Returns the chat in which is update has happened, if any.
#[must_use]
pub fn chat(&self) -> Option<&Chat> {
match &self.kind {
UpdateKind::Message(m) => Some(&m.chat),
UpdateKind::EditedMessage(m) => Some(&m.chat),
UpdateKind::ChannelPost(p) => Some(&p.chat),
UpdateKind::EditedChannelPost(p) => Some(&p.chat),
UpdateKind::CallbackQuery(q) => Some(&q.message.as_ref()?.chat),
UpdateKind::ChatMember(m) => Some(&m.chat),
UpdateKind::MyChatMember(m) => Some(&m.chat),
UpdateKind::ChatJoinRequest(c) => Some(&c.chat),
_ => None,
}
use UpdateKind::*;
let chat = match &self.kind {
Message(m) | EditedMessage(m) | ChannelPost(m) | EditedChannelPost(m) => &m.chat,
CallbackQuery(q) => &q.message.as_ref()?.chat,
ChatMember(m) => &m.chat,
MyChatMember(m) => &m.chat,
ChatJoinRequest(c) => &c.chat,
InlineQuery(_)
| ChosenInlineResult(_)
| ShippingQuery(_)
| PreCheckoutQuery(_)
| Poll(_)
| PollAnswer(_)
| Error(_) => return None,
};
Some(chat)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum UpdateKind {
// NB: When adding new variants, don't forget to update
// - `AllowedUpdate`
// - `Update::user`
// - `Update::chat`
// - `DpHandlerDescription::full_set`
// - `dispatching/filter_ext.rs`
/// New incoming message of any kind — text, photo, sticker, etc.
Message(Message),

View file

@ -103,7 +103,6 @@ macro_rules! define_update_ext {
}
}
// May be expanded in the future.
define_update_ext! {
(filter_message, UpdateKind::Message, Message),
(filter_edited_message, UpdateKind::EditedMessage, EditedMessage),
@ -118,4 +117,5 @@ define_update_ext! {
(filter_poll_answer, UpdateKind::PollAnswer, PollAnswer),
(filter_my_chat_member, UpdateKind::MyChatMember, MyChatMember),
(filter_chat_member, UpdateKind::ChatMember, ChatMember),
(filter_chat_join_request, UpdateKind::ChatJoinRequest, ChatJoinRequest),
}

View file

@ -21,7 +21,7 @@ impl DpHandlerDescription {
}
pub(crate) fn allowed_updates(&self) -> Vec<AllowedUpdate> {
self.allowed.observed.iter().map(|Kind(x)| x).copied().collect()
self.allowed.observed.iter().map(|&Kind(x)| x).collect()
}
}
@ -50,6 +50,16 @@ impl EventKind for Kind {
fn full_set() -> HashSet<Self> {
use AllowedUpdate::*;
// NB: We need to specify all update kinds by hand, because telegram doesn't
// enable `ChatMember` by default:
//
// > A JSON-serialized list of the update types you want your bot to
// > receive. For example, specify [“message”, “edited_channel_post”,
// > “callback_query”] to only receive updates of these types. See Update
// > for a complete list of available update types. Specify an empty list
// > to receive all update types except chat_member (default). If not
// > specified, the previous setting will be used.
[
Message,
EditedMessage,
@ -64,6 +74,7 @@ impl EventKind for Kind {
PollAnswer,
MyChatMember,
ChatMember,
ChatJoinRequest,
]
.into_iter()
.map(Kind)