Skip to main content

grammers_client/update/
message_deletion.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 grammers_session::updates::State;
10use grammers_tl_types as tl;
11
12/// Update that all receive whenever a message is deleted.
13#[derive(Debug, Clone)]
14pub struct MessageDeletion {
15    pub raw: tl::enums::Update,
16    pub state: State,
17}
18
19impl MessageDeletion {
20    /// Returns the channel ID if the message was deleted from a channel.
21    pub fn channel_id(&self) -> Option<i64> {
22        match &self.raw {
23            tl::enums::Update::DeleteMessages(_) => None,
24            tl::enums::Update::DeleteChannelMessages(update) => Some(update.channel_id),
25            _ => unreachable!(),
26        }
27    }
28
29    /// Returns the slice of message IDs that was deleted.
30    pub fn messages(&self) -> &[i32] {
31        match &self.raw {
32            tl::enums::Update::DeleteMessages(update) => update.messages.as_slice(),
33            tl::enums::Update::DeleteChannelMessages(update) => update.messages.as_slice(),
34            _ => unreachable!(),
35        }
36    }
37
38    /// Gain ownership of underlying Vec of message IDs that was deleted.
39    pub fn into_messages(self) -> Vec<i32> {
40        match self.raw {
41            tl::enums::Update::DeleteMessages(update) => update.messages,
42            tl::enums::Update::DeleteChannelMessages(update) => update.messages,
43            _ => unreachable!(),
44        }
45    }
46}