Fix Update::user (return Some on some forgotten variants)

The following update types now are handled in `Update::user`:
- chat join requests
- chat members
- channel post
This commit is contained in:
Maybe Waffle 2023-02-03 16:36:01 +04:00
parent 7a871b7487
commit b5bd147c7c

View file

@ -31,32 +31,48 @@ pub struct Update {
impl Update { impl Update {
#[must_use] #[must_use]
pub fn user(&self) -> Option<&User> { pub fn user(&self) -> Option<&User> {
match &self.kind { use UpdateKind::*;
UpdateKind::Message(m) => m.from(),
UpdateKind::EditedMessage(m) => m.from(), let from = match &self.kind {
UpdateKind::CallbackQuery(query) => Some(&query.from), Message(m) | EditedMessage(m) | ChannelPost(m) | EditedChannelPost(m) => m.from()?,
UpdateKind::ChosenInlineResult(chosen) => Some(&chosen.from),
UpdateKind::InlineQuery(query) => Some(&query.from), CallbackQuery(query) => &query.from,
UpdateKind::ShippingQuery(query) => Some(&query.from), ChosenInlineResult(chosen) => &chosen.from,
UpdateKind::PreCheckoutQuery(query) => Some(&query.from), InlineQuery(query) => &query.from,
UpdateKind::PollAnswer(answer) => Some(&answer.user), ShippingQuery(query) => &query.from,
_ => None, PreCheckoutQuery(query) => &query.from,
} PollAnswer(answer) => &answer.user,
MyChatMember(m) | ChatMember(m) => &m.from,
ChatJoinRequest(r) => &r.from,
Poll(_) | Error(_) => return None,
};
Some(from)
} }
#[must_use] #[must_use]
pub fn chat(&self) -> Option<&Chat> { pub fn chat(&self) -> Option<&Chat> {
match &self.kind { use UpdateKind::*;
UpdateKind::Message(m) => Some(&m.chat),
UpdateKind::EditedMessage(m) => Some(&m.chat), let chat = match &self.kind {
UpdateKind::ChannelPost(p) => Some(&p.chat), Message(m) | EditedMessage(m) | ChannelPost(m) | EditedChannelPost(m) => &m.chat,
UpdateKind::EditedChannelPost(p) => Some(&p.chat), CallbackQuery(q) => &q.message.as_ref()?.chat,
UpdateKind::CallbackQuery(q) => Some(&q.message.as_ref()?.chat), ChatMember(m) => &m.chat,
UpdateKind::ChatMember(m) => Some(&m.chat), MyChatMember(m) => &m.chat,
UpdateKind::MyChatMember(m) => Some(&m.chat), ChatJoinRequest(c) => &c.chat,
UpdateKind::ChatJoinRequest(c) => Some(&c.chat),
_ => None, InlineQuery(_)
} | ChosenInlineResult(_)
| ShippingQuery(_)
| PreCheckoutQuery(_)
| Poll(_)
| PollAnswer(_)
| Error(_) => return None,
};
Some(chat)
} }
} }