From 363259e4164f932468c8c387e50091d022ba23ab Mon Sep 17 00:00:00 2001 From: P0lunin Date: Sat, 19 Oct 2019 18:44:16 +0300 Subject: [PATCH] added method setChatPermissions --- src/bot/api.rs | 14 +++- src/requests/mod.rs | 2 + src/requests/set_chat_permissions.rs | 95 ++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/requests/set_chat_permissions.rs diff --git a/src/bot/api.rs b/src/bot/api.rs index cc12f9c2..1d743a15 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -10,7 +10,7 @@ use crate::{ SendVenue, SendVideo, SendVideoNote, SendVoice, SetChatDescription, SetChatStickerSet, StopMessageLiveLocation, UnbanChatMember, UnpinChatMessage, SetChatTitle, DeleteChatPhoto, SetChatPhoto, - ExportCharInviteLink + ExportCharInviteLink, SetChatPermissions }, types::{ChatAction, ChatId, ChatPermissions, InputFile, InputMedia}, }; @@ -401,4 +401,16 @@ impl Bot { { ExportCharInviteLink::new(self, chat_id) } + + pub fn set_chat_permissions( + &self, + chat_id: C, + permissions: CP, + ) -> SetChatPermissions + where + C: Into, + CP: Into, + { + SetChatPermissions::new(self, chat_id, permissions) + } } diff --git a/src/requests/mod.rs b/src/requests/mod.rs index cc8e8e65..013d3707 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -37,6 +37,7 @@ pub use send_video::*; pub use send_video_note::*; pub use send_voice::*; pub use set_chat_description::*; +pub use set_chat_permissions::*; pub use set_chat_photo::*; pub use set_chat_sticker_set::*; pub use set_chat_title::*; @@ -81,6 +82,7 @@ mod send_video; mod send_video_note; mod send_voice; mod set_chat_description; +mod set_chat_permissions; mod set_chat_photo; mod set_chat_sticker_set; mod set_chat_title; diff --git a/src/requests/set_chat_permissions.rs b/src/requests/set_chat_permissions.rs new file mode 100644 index 00000000..fe5d3c52 --- /dev/null +++ b/src/requests/set_chat_permissions.rs @@ -0,0 +1,95 @@ +use async_trait::async_trait; + +use crate::bot::Bot; +use crate::types::{ChatId, ChatPermissions, True}; +use crate::requests::{ResponseResult, Request}; +use crate::network; + +#[derive(Debug, Clone, Serialize)] +pub struct SetChatPermissions<'a> { + #[serde(skip_serializing)] + bot: &'a Bot, + + chat_id: ChatId, + permissions: ChatPermissions +} + +#[async_trait] +impl Request for SetChatPermissions<'_> { + type Output = True; + + async fn send_boxed(self) -> ResponseResult { + self.send().await + } +} + +impl SetChatPermissions<'_> { + async fn send(self) -> ResponseResult { + network::request_json( + self.bot.client(), + self.bot.token(), + "setChatPermissions", + &self + ).await + } +} + +impl<'a> SetChatPermissions<'a> { + pub(crate) fn new( + bot: &'a Bot, + chat_id: C, + permissions: CP, + ) -> Self + where + C: Into, + CP: Into, + { + Self { + bot, + chat_id: chat_id.into(), + permissions: permissions.into(), + } + } + + pub fn chat_id(mut self, chat_id: C) -> Self + where + C: Into, + { + self.chat_id = chat_id.into(); + self + } + + pub fn permissions(mut self, permissions: CP) -> Self + where + CP: Into + { + self.permissions = permissions.into(); + self + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn serialize() { + let bot = Bot::new("token"); + let chat_id = 123; + let permissions = ChatPermissions { + can_send_messages: Some(true), + can_send_media_messages: None, + can_send_polls: None, + can_send_other_messages: None, + can_add_web_page_previews: None, + can_change_info: None, + can_invite_users: None, + can_pin_messages: None + }; + let method = SetChatPermissions::new(&bot, chat_id, permissions); + + let expected = r#"{"chat_id":123,"permissions":{"can_send_messages":true}}"#; + let actual = serde_json::to_string::(&method).unwrap(); + assert_eq!(actual, expected); + } +}