slack_rust/conversations/
accept_shared_invite.rs

1use crate::error::Error;
2use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
3use serde::{Deserialize, Serialize};
4use serde_with::skip_serializing_none;
5
6#[skip_serializing_none]
7#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
8pub struct AcceptSharedInviteRequest {
9    pub channel_name: String,
10    pub channel_id: Option<String>,
11    pub free_trial_accepted: Option<bool>,
12    pub invite_id: Option<String>,
13    pub is_private: Option<bool>,
14    pub team_id: Option<String>,
15}
16
17#[skip_serializing_none]
18#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
19pub struct AcceptSharedInviteResponse {
20    pub ok: bool,
21    pub error: Option<String>,
22    pub response_metadata: Option<ResponseMetadata>,
23    pub implicit_approval: Option<bool>,
24    pub channel_id: Option<String>,
25    pub invite_id: Option<String>,
26}
27
28pub async fn accept_shared_invite<T>(
29    client: &T,
30    param: &AcceptSharedInviteRequest,
31    bot_token: &str,
32) -> Result<AcceptSharedInviteResponse, Error>
33where
34    T: SlackWebAPIClient,
35{
36    let url = get_slack_url("conversations.acceptSharedInvite");
37    let json = serde_json::to_string(&param)?;
38
39    client
40        .post_json(&url, &json, bot_token)
41        .await
42        .and_then(|result| {
43            serde_json::from_str::<AcceptSharedInviteResponse>(&result)
44                .map_err(Error::SerdeJsonError)
45        })
46}
47
48#[cfg(test)]
49mod test {
50    use super::*;
51    use crate::http_client::MockSlackWebAPIClient;
52
53    #[test]
54    fn convert_request() {
55        let request = AcceptSharedInviteRequest {
56            channel_name: "puppies-r-us".to_string(),
57            channel_id: Some("xxxxxxxxxxx".to_string()),
58            free_trial_accepted: Some(true),
59            invite_id: Some("xxxxxxxxxxx".to_string()),
60            is_private: Some(true),
61            team_id: Some("T1234567890".to_string()),
62        };
63        let json = r##"{
64  "channel_name": "puppies-r-us",
65  "channel_id": "xxxxxxxxxxx",
66  "free_trial_accepted": true,
67  "invite_id": "xxxxxxxxxxx",
68  "is_private": true,
69  "team_id": "T1234567890"
70}"##;
71
72        let j = serde_json::to_string_pretty(&request).unwrap();
73        assert_eq!(json, j);
74
75        let s = serde_json::from_str::<AcceptSharedInviteRequest>(json).unwrap();
76        assert_eq!(request, s);
77    }
78
79    #[test]
80    fn convert_response() {
81        let response = AcceptSharedInviteResponse {
82            ok: true,
83            implicit_approval: Some(true),
84            channel_id: Some("C0001111".to_string()),
85            invite_id: Some("I00043221".to_string()),
86            ..Default::default()
87        };
88        let json = r##"{
89  "ok": true,
90  "implicit_approval": true,
91  "channel_id": "C0001111",
92  "invite_id": "I00043221"
93}"##;
94
95        let j = serde_json::to_string_pretty(&response).unwrap();
96        assert_eq!(json, j);
97
98        let s = serde_json::from_str::<AcceptSharedInviteResponse>(json).unwrap();
99        assert_eq!(response, s);
100    }
101
102    #[async_std::test]
103    async fn test_accept_shared_invite() {
104        let param = AcceptSharedInviteRequest {
105            channel_name: "puppies-r-us".to_string(),
106            channel_id: Some("xxxxxxxxxxx".to_string()),
107            free_trial_accepted: Some(true),
108            invite_id: Some("xxxxxxxxxxx".to_string()),
109            is_private: Some(true),
110            team_id: Some("T1234567890".to_string()),
111        };
112
113        let mut mock = MockSlackWebAPIClient::new();
114        mock.expect_post_json().returning(|_, _, _| {
115            Ok(r##"{
116  "ok": true,
117  "implicit_approval": true,
118  "channel_id": "xxxxxxxxxxx",
119  "invite_id": "xxxxxxxxxxx"
120}"##
121            .to_string())
122        });
123
124        let response = accept_shared_invite(&mock, &param, &"test_token".to_string())
125            .await
126            .unwrap();
127        let expect = AcceptSharedInviteResponse {
128            ok: true,
129            implicit_approval: Some(true),
130            channel_id: Some("xxxxxxxxxxx".to_string()),
131            invite_id: Some("xxxxxxxxxxx".to_string()),
132            ..Default::default()
133        };
134
135        assert_eq!(expect, response);
136    }
137}