1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use super::call_method;
use crate::{
    bot::InnerBot,
    errors,
    types::{
        parameters::{ChatId, ImplicitChatId},
        user,
    },
};
use serde::Serialize;

/// Promotes a chat member to an admin.
///
/// Reflects the [`promoteChatMember`][docs] method.
///
/// [docs]: https://core.telegram.org/bots/api#promotechatmember
#[derive(Serialize, Debug, Clone)]
#[must_use = "methods do nothing unless turned into a future"]
pub struct PromoteChatMember<'a> {
    #[serde(skip)]
    bot: &'a InnerBot,
    chat_id: ChatId,
    user_id: user::Id,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_manage_chat: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_change_info: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_post_messages: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_edit_messages: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_delete_messages: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_invite_users: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_restrict_members: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_pin_messages: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_promote_members: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    can_manage_voice_chats: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    is_anonymous: Option<bool>,
}

impl<'a> PromoteChatMember<'a> {
    pub(crate) fn new(
        bot: &'a InnerBot,
        chat_id: impl ImplicitChatId,
        user_id: user::Id,
    ) -> Self {
        Self {
            bot,
            chat_id: chat_id.into(),
            user_id,
            can_manage_chat: None,
            can_change_info: None,
            can_post_messages: None,
            can_edit_messages: None,
            can_delete_messages: None,
            can_invite_users: None,
            can_restrict_members: None,
            can_pin_messages: None,
            can_promote_members: None,
            can_manage_voice_chats: None,
            is_anonymous: None,
        }
    }

    /// Configures if the user will be able to perform administrative actions.
    /// Reflects the `can_manage_chat` parameter.
    pub const fn can_manage_chat(mut self, can_manage: bool) -> Self {
        self.can_manage_chat = Some(can_manage);
        self
    }

    /// Configures if the user will be able to change the group's information.
    /// Reflects the `can_change_info` parameter.
    pub const fn can_change_info(mut self, can_change: bool) -> Self {
        self.can_change_info = Some(can_change);
        self
    }

    /// Configures if the user will be able to post messages, if the chat is
    /// a channel. Reflects the `can_post_messages` parameter.
    pub const fn can_post_messages(mut self, can_post: bool) -> Self {
        self.can_post_messages = Some(can_post);
        self
    }

    /// Configures if the user will be able to edit messages, if the chat is
    /// a channel. Reflects the `can_edit_messages` parameter.
    pub const fn can_edit_messages(mut self, can_edit: bool) -> Self {
        self.can_edit_messages = Some(can_edit);
        self
    }

    /// Configures if the user will be able to delete messages.
    /// Reflects the `can_delete_messages` parameter.
    pub const fn can_delete_messages(mut self, can_delete: bool) -> Self {
        self.can_delete_messages = Some(can_delete);
        self
    }

    /// Configures if the user will be able to invite new users.
    /// Reflects the `can_invite_users` parameter.
    pub const fn can_invite_users(mut self, can_invite: bool) -> Self {
        self.can_invite_users = Some(can_invite);
        self
    }

    /// Configures if the user will be able to restrict members.
    /// Reflects the `can_restrict_members` parameter.
    pub const fn can_restrict_members(mut self, can_restrict: bool) -> Self {
        self.can_restrict_members = Some(can_restrict);
        self
    }

    /// Configures if the user will be able to pin messages.
    /// Reflects the `can_pin_messages` parameter.
    pub const fn can_pin_messages(mut self, can_pin: bool) -> Self {
        self.can_pin_messages = Some(can_pin);
        self
    }

    /// Configures if the user will be able to promote other members.
    /// Reflects the `can_promote_members` parameter.
    pub const fn can_promote_members(mut self, can_promote: bool) -> Self {
        self.can_promote_members = Some(can_promote);
        self
    }

    /// Configures if the user will be able to manage voice chats.
    /// Reflects the `can_manage_voice_chats` parameter.
    pub const fn can_manage_voice_chats(mut self, can_manage: bool) -> Self {
        self.can_manage_voice_chats = Some(can_manage);
        self
    }

    /// Configures if the user will be anonymous.
    /// Reflects the `is_anonymous` parameter.
    pub const fn is_anonymous(mut self, is_anonymous: bool) -> Self {
        self.is_anonymous = Some(is_anonymous);
        self
    }
}

impl PromoteChatMember<'_> {
    /// Calls the method.
    pub async fn call(self) -> Result<(), errors::MethodCall> {
        call_method::<bool>(
            self.bot,
            "promoteChatMember",
            None,
            serde_json::to_vec(&self).unwrap(),
        )
        .await?;

        Ok(())
    }
}