grammers_client/types/update/
update.rs

1// Copyright 2020 - developers of the `grammers` project.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use 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/// An update that indicates some event, which may be of interest to the logged-in account, has occured.
18///
19/// Only updates pertaining to messages are guaranteed to be delivered, and can be fetched on-demand if
20/// they occured while the client was offline by enabling [`catch_up`](crate::UpdatesConfiguration::catch_up).
21#[non_exhaustive]
22#[derive(Debug, Clone)]
23pub enum Update {
24    /// Occurs whenever a new text message or a message with media is produced.
25    NewMessage(Message),
26    /// Occurs when a message is updated.
27    MessageEdited(Message),
28    /// Occurs when a message is deleted.
29    MessageDeleted(MessageDeletion),
30    /// Occurs when Telegram calls back into your bot because an inline callback
31    /// button was pressed.
32    CallbackQuery(CallbackQuery),
33    /// Occurs whenever you sign in as a bot and a user sends an inline query
34    /// such as `@bot query`.
35    InlineQuery(InlineQuery),
36    /// Represents an update of user choosing the result of inline query and sending it to their peer partner.
37    InlineSend(InlineSend),
38    /// Raw events are not actual events.
39    /// Instead, they are the raw Update object that Telegram sends. You
40    /// normally shouldn’t need these.
41    ///
42    /// **NOTE**: the library can split raw updates into actual `Update`
43    /// variants so use this only as the workaround when such variant is not
44    /// available yet.
45    Raw(Raw),
46}
47
48impl Update {
49    /// Create new friendly to use Update from its raw version and peer map
50    pub fn new(
51        client: &Client,
52        update: tl::enums::Update,
53        state: State,
54        peers: &Arc<PeerMap>,
55    ) -> Self {
56        match &update {
57            // NewMessage
58            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            // MessageEdited
83            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            // MessageDeleted
101            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            // CallbackQuery
109            tl::enums::Update::BotCallbackQuery(_) => Self::CallbackQuery(CallbackQuery {
110                raw: update,
111                state,
112                client: client.clone(),
113                peers: Arc::clone(peers),
114            }),
115
116            // InlineCallbackQuery
117            tl::enums::Update::InlineBotCallbackQuery(_) => Self::CallbackQuery(CallbackQuery {
118                raw: update,
119                state,
120                client: client.clone(),
121                peers: Arc::clone(peers),
122            }),
123
124            // InlineQuery
125            tl::enums::Update::BotInlineQuery(_) => Self::InlineQuery(InlineQuery {
126                raw: update,
127                state,
128                client: client.clone(),
129                peers: Arc::clone(peers),
130            }),
131
132            // InlineSend
133            tl::enums::Update::BotInlineSend(_) => Self::InlineSend(InlineSend {
134                raw: update,
135                state,
136                client: client.clone(),
137                peers: Arc::clone(peers),
138            }),
139
140            // Raw
141            _ => Self::Raw(Raw { raw: update, state }),
142        }
143    }
144
145    /// Update state.
146    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    /// Raw update, as sent by Telegram.
159    ///
160    /// Only contains the individual [`Update`](tl::enums::Update),
161    /// not the [`Updates`](tl::enums::Updates) container from which it may have come from.
162    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}