use grammers_session::updates::State;
use grammers_tl_types as tl;
use super::{CallbackQuery, InlineQuery, InlineSend, Message, MessageDeletion, Raw};
use crate::{Client, peer::PeerMap, utils};
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum Update {
NewMessage(Message),
MessageEdited(Message),
MessageDeleted(MessageDeletion),
CallbackQuery(CallbackQuery),
InlineQuery(InlineQuery),
InlineSend(InlineSend),
Raw(Raw),
}
impl Update {
pub fn from_raw(
client: &Client,
update: tl::enums::Update,
state: State,
peers: PeerMap,
) -> Self {
match &update {
tl::enums::Update::NewMessage(raw) => {
if utils::peer_from_message(&raw.message).is_none() {
return Self::Raw(Raw { raw: update, state });
}
Self::NewMessage(Message {
msg: crate::message::Message::from_raw(
client,
raw.message.clone(),
None,
peers,
),
raw: update,
state,
})
}
tl::enums::Update::NewChannelMessage(raw) => {
if utils::peer_from_message(&raw.message).is_none() {
return Self::Raw(Raw { raw: update, state });
}
Self::NewMessage(Message {
msg: crate::message::Message::from_raw(
client,
raw.message.clone(),
None,
peers,
),
raw: update,
state,
})
}
tl::enums::Update::EditMessage(raw) => {
if utils::peer_from_message(&raw.message).is_none() {
return Self::Raw(Raw { raw: update, state });
}
Self::MessageEdited(Message {
msg: crate::message::Message::from_raw(
client,
raw.message.clone(),
None,
peers,
),
raw: update,
state,
})
}
tl::enums::Update::EditChannelMessage(raw) => Self::MessageEdited(Message {
msg: crate::message::Message::from_raw(client, raw.message.clone(), None, peers),
raw: update,
state,
}),
tl::enums::Update::DeleteMessages(_) => {
Self::MessageDeleted(MessageDeletion { raw: update, state })
}
tl::enums::Update::DeleteChannelMessages(_) => {
Self::MessageDeleted(MessageDeletion { raw: update, state })
}
tl::enums::Update::BotCallbackQuery(_) => Self::CallbackQuery(CallbackQuery {
raw: update,
state,
client: client.clone(),
peers,
}),
tl::enums::Update::InlineBotCallbackQuery(_) => Self::CallbackQuery(CallbackQuery {
raw: update,
state,
client: client.clone(),
peers,
}),
tl::enums::Update::BotInlineQuery(_) => Self::InlineQuery(InlineQuery {
raw: update,
state,
client: client.clone(),
peers,
}),
tl::enums::Update::BotInlineSend(_) => Self::InlineSend(InlineSend {
raw: update,
state,
client: client.clone(),
peers,
}),
_ => Self::Raw(Raw { raw: update, state }),
}
}
pub fn state(&self) -> &State {
match self {
Update::NewMessage(update) => &update.state,
Update::MessageEdited(update) => &update.state,
Update::MessageDeleted(update) => &update.state,
Update::CallbackQuery(update) => &update.state,
Update::InlineQuery(update) => &update.state,
Update::InlineSend(update) => &update.state,
Update::Raw(update) => &update.state,
}
}
pub fn raw(&self) -> &tl::enums::Update {
match self {
Update::NewMessage(update) => &update.raw,
Update::MessageEdited(update) => &update.raw,
Update::MessageDeleted(update) => &update.raw,
Update::CallbackQuery(update) => &update.raw,
Update::InlineQuery(update) => &update.raw,
Update::InlineSend(update) => &update.raw,
Update::Raw(update) => &update.raw,
}
}
}