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
use crate::{
    methods::Method,
    request::Request,
    types::{ChatId, Integer},
};
use serde::Serialize;

/// Ban a channel chat in a supergroup or a channel
///
/// Until the chat is unbanned, the owner of the banned chat won't be able to send messages
/// on behalf of any of their channels. The bot must be an administrator in the supergroup or
/// channel for this to work and must have the appropriate administrator rights.
#[derive(Clone, Debug, Serialize)]
pub struct BanChatSenderChat {
    chat_id: ChatId,
    sender_chat_id: Integer,
}

impl BanChatSenderChat {
    /// Creates a new BanChatSenderChat
    ///
    /// # Arguments
    ///
    /// * chat_id - Unique identifier for the target
    /// * sender_chat_id - Unique identifier of the target sender chat
    pub fn new<C: Into<ChatId>>(chat_id: C, sender_chat_id: Integer) -> Self {
        Self {
            chat_id: chat_id.into(),
            sender_chat_id,
        }
    }
}

impl Method for BanChatSenderChat {
    type Response = bool;

    fn into_request(self) -> Request {
        Request::json("banChatSenderChat", self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::request::{RequestBody, RequestMethod};
    use serde_json::Value;

    #[test]
    fn ban_chat_sender_chat() {
        let request = BanChatSenderChat::new(1, 1).into_request();
        assert_eq!(request.get_method(), RequestMethod::Post);
        assert_eq!(
            request.build_url("base-url", "token"),
            "base-url/bottoken/banChatSenderChat"
        );
        if let RequestBody::Json(data) = request.into_body() {
            let data: Value = serde_json::from_str(&data.unwrap()).unwrap();
            assert_eq!(
                data,
                serde_json::json!({
                    "chat_id": 1,
                    "sender_chat_id": 1,
                })
            );
        } else {
            panic!("Unexpected request body");
        }
    }
}