slack_rust/reactions/
list.rs

1use crate::error::Error;
2use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
3use crate::items::item::Item;
4use serde::{Deserialize, Serialize};
5use serde_with::skip_serializing_none;
6
7#[skip_serializing_none]
8#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
9pub struct ListRequest {
10    pub count: Option<i32>,
11    pub cursor: Option<String>,
12    pub full: Option<bool>,
13    pub limit: Option<i32>,
14    pub page: Option<i32>,
15    pub team_id: Option<String>,
16    pub user: Option<String>,
17}
18
19#[skip_serializing_none]
20#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
21pub struct ListResponse {
22    pub ok: bool,
23    pub error: Option<String>,
24    pub response_metadata: Option<ResponseMetadata>,
25    // TODO
26    // https://api.slack.com/methods/reactions.list#examples
27    pub items: Option<Vec<Item>>,
28}
29
30pub async fn list<T>(
31    client: &T,
32    param: &ListRequest,
33    bot_token: &str,
34) -> Result<ListResponse, Error>
35where
36    T: SlackWebAPIClient,
37{
38    let url = get_slack_url("reactions.list");
39    let json = serde_json::to_string(&param)?;
40
41    client
42        .post_json(&url, &json, bot_token)
43        .await
44        .and_then(|result| {
45            serde_json::from_str::<ListResponse>(&result).map_err(Error::SerdeJsonError)
46        })
47}
48
49#[cfg(test)]
50mod test {
51    use super::*;
52    use crate::chat::message::Message;
53    use crate::http_client::MockSlackWebAPIClient;
54    use crate::reactions::reaction::Reaction;
55
56    #[test]
57    fn convert_request() {
58        let request = ListRequest {
59            count: Some(20),
60            full: Some(true),
61            limit: Some(20),
62            page: Some(2),
63            team_id: Some("T1234567890".to_string()),
64            cursor: Some("dXNlcjpVMDYxTkZUVDI=".to_string()),
65            user: Some("W1234567890".to_string()),
66        };
67        let json = r##"{
68  "count": 20,
69  "cursor": "dXNlcjpVMDYxTkZUVDI=",
70  "full": true,
71  "limit": 20,
72  "page": 2,
73  "team_id": "T1234567890",
74  "user": "W1234567890"
75}"##;
76
77        let j = serde_json::to_string_pretty(&request).unwrap();
78        assert_eq!(json, j);
79
80        let s = serde_json::from_str::<ListRequest>(json).unwrap();
81        assert_eq!(request, s);
82    }
83
84    #[test]
85    fn convert_response() {
86        let response = ListResponse {
87            ok: true,
88            items: Some(vec![Item {
89                type_filed: Some("message".to_string()),
90                channel: Some("C3UKJTQAC".to_string()),
91                message: Some(Message {
92                    bot_id: Some("B4VLRLMKJ".to_string()),
93                    text: Some("Hello from Python! :tada:".to_string()),
94                    username: Some("Shipit Notifications".to_string()),
95                    ts: Some("1507849573.000090".to_string()),
96                    subtype: Some("bot_message".to_string()),
97                    reactions: Some(vec![Reaction {
98                        count: Some(1),
99                        name: Some("robot_face".to_string()),
100                        users: Some(vec!["U2U85N1RV".to_string()]),
101                    }]),
102                    ..Default::default()
103                }),
104                ..Default::default()
105            }]),
106            ..Default::default()
107        };
108        let json = r##"{
109  "ok": true,
110  "items": [
111    {
112      "type": "message",
113      "channel": "C3UKJTQAC",
114      "message": {
115        "bot_id": "B4VLRLMKJ",
116        "text": "Hello from Python! :tada:",
117        "username": "Shipit Notifications",
118        "ts": "1507849573.000090",
119        "subtype": "bot_message",
120        "reactions": [
121          {
122            "count": 1,
123            "name": "robot_face",
124            "users": [
125              "U2U85N1RV"
126            ]
127          }
128        ]
129      }
130    }
131  ]
132}"##;
133
134        let j = serde_json::to_string_pretty(&response).unwrap();
135        assert_eq!(json, j);
136
137        let s = serde_json::from_str::<ListResponse>(json).unwrap();
138        assert_eq!(response, s);
139    }
140
141    #[async_std::test]
142    async fn test_list() {
143        let param = ListRequest {
144            count: Some(20),
145            full: Some(true),
146            limit: Some(20),
147            page: Some(2),
148            team_id: Some("T1234567890".to_string()),
149            cursor: Some("dXNlcjpVMDYxTkZUVDI=".to_string()),
150            user: Some("W1234567890".to_string()),
151        };
152
153        let mut mock = MockSlackWebAPIClient::new();
154        mock.expect_post_json().returning(|_, _, _| {
155            Ok(r##"{
156  "ok": true,
157  "items": [
158    {
159      "type": "message",
160      "channel": "C3UKJTQAC",
161      "message": {
162        "bot_id": "B4VLRLMKJ",
163        "text": "Hello from Python! :tada:",
164        "username": "Shipit Notifications",
165        "ts": "1507849573.000090",
166        "subtype": "bot_message",
167        "reactions": [
168          {
169            "count": 1,
170            "name": "robot_face",
171            "users": [
172              "U2U85N1RV"
173            ]
174          }
175        ]
176      }
177    }
178  ]
179}"##
180            .to_string())
181        });
182
183        let response = list(&mock, &param, &"test_token".to_string())
184            .await
185            .unwrap();
186        let expect = ListResponse {
187            ok: true,
188            items: Some(vec![Item {
189                type_filed: Some("message".to_string()),
190                channel: Some("C3UKJTQAC".to_string()),
191                message: Some(Message {
192                    bot_id: Some("B4VLRLMKJ".to_string()),
193                    text: Some("Hello from Python! :tada:".to_string()),
194                    username: Some("Shipit Notifications".to_string()),
195                    ts: Some("1507849573.000090".to_string()),
196                    subtype: Some("bot_message".to_string()),
197                    reactions: Some(vec![Reaction {
198                        count: Some(1),
199                        name: Some("robot_face".to_string()),
200                        users: Some(vec!["U2U85N1RV".to_string()]),
201                    }]),
202                    ..Default::default()
203                }),
204                ..Default::default()
205            }]),
206            ..Default::default()
207        };
208
209        assert_eq!(expect, response);
210    }
211}