grammers_client/types/update/
update.rs1use std::sync::Arc;
10
11use super::{CallbackQuery, InlineQuery, InlineSend, Message, MessageDeletion, Raw};
12use crate::types::Message as Msg;
13use crate::{Client, PeerMap, utils};
14use grammers_session::updates::State;
15use grammers_tl_types as tl;
16
17#[non_exhaustive]
22#[derive(Debug, Clone)]
23pub enum Update {
24 NewMessage(Message),
26 MessageEdited(Message),
28 MessageDeleted(MessageDeletion),
30 CallbackQuery(CallbackQuery),
33 InlineQuery(InlineQuery),
36 InlineSend(InlineSend),
38 Raw(Raw),
46}
47
48impl Update {
49 pub fn new(
51 client: &Client,
52 update: tl::enums::Update,
53 state: State,
54 peers: &Arc<PeerMap>,
55 ) -> Self {
56 match &update {
57 tl::enums::Update::NewMessage(raw) => {
59 if utils::peer_from_message(&raw.message).is_none() {
60 return Self::Raw(Raw { raw: update, state });
61 }
62
63 Self::NewMessage(Message {
64 msg: Msg::from_raw(client, raw.message.clone(), None, peers),
65 raw: update,
66 state,
67 })
68 }
69
70 tl::enums::Update::NewChannelMessage(raw) => {
71 if utils::peer_from_message(&raw.message).is_none() {
72 return Self::Raw(Raw { raw: update, state });
73 }
74
75 Self::NewMessage(Message {
76 msg: Msg::from_raw(client, raw.message.clone(), None, peers),
77 raw: update,
78 state,
79 })
80 }
81
82 tl::enums::Update::EditMessage(raw) => {
84 if utils::peer_from_message(&raw.message).is_none() {
85 return Self::Raw(Raw { raw: update, state });
86 }
87
88 Self::MessageEdited(Message {
89 msg: Msg::from_raw(client, raw.message.clone(), None, peers),
90 raw: update,
91 state,
92 })
93 }
94 tl::enums::Update::EditChannelMessage(raw) => Self::MessageEdited(Message {
95 msg: Msg::from_raw(client, raw.message.clone(), None, peers),
96 raw: update,
97 state,
98 }),
99
100 tl::enums::Update::DeleteMessages(_) => {
102 Self::MessageDeleted(MessageDeletion { raw: update, state })
103 }
104 tl::enums::Update::DeleteChannelMessages(_) => {
105 Self::MessageDeleted(MessageDeletion { raw: update, state })
106 }
107
108 tl::enums::Update::BotCallbackQuery(_) => Self::CallbackQuery(CallbackQuery {
110 raw: update,
111 state,
112 client: client.clone(),
113 peers: Arc::clone(peers),
114 }),
115
116 tl::enums::Update::InlineBotCallbackQuery(_) => Self::CallbackQuery(CallbackQuery {
118 raw: update,
119 state,
120 client: client.clone(),
121 peers: Arc::clone(peers),
122 }),
123
124 tl::enums::Update::BotInlineQuery(_) => Self::InlineQuery(InlineQuery {
126 raw: update,
127 state,
128 client: client.clone(),
129 peers: Arc::clone(peers),
130 }),
131
132 tl::enums::Update::BotInlineSend(_) => Self::InlineSend(InlineSend {
134 raw: update,
135 state,
136 client: client.clone(),
137 peers: Arc::clone(peers),
138 }),
139
140 _ => Self::Raw(Raw { raw: update, state }),
142 }
143 }
144
145 pub fn state(&self) -> &State {
147 match self {
148 Update::NewMessage(update) => &update.state,
149 Update::MessageEdited(update) => &update.state,
150 Update::MessageDeleted(update) => &update.state,
151 Update::CallbackQuery(update) => &update.state,
152 Update::InlineQuery(update) => &update.state,
153 Update::InlineSend(update) => &update.state,
154 Update::Raw(update) => &update.state,
155 }
156 }
157
158 pub fn raw(&self) -> &tl::enums::Update {
163 match self {
164 Update::NewMessage(update) => &update.raw,
165 Update::MessageEdited(update) => &update.raw,
166 Update::MessageDeleted(update) => &update.raw,
167 Update::CallbackQuery(update) => &update.raw,
168 Update::InlineQuery(update) => &update.raw,
169 Update::InlineSend(update) => &update.raw,
170 Update::Raw(update) => &update.raw,
171 }
172 }
173}