1use crate::error::Error;
2use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
3use crate::users::user::User;
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 cursor: Option<String>,
11 pub include_locale: Option<bool>,
12 pub limit: Option<i32>,
13 pub team_id: Option<String>,
14}
15
16#[skip_serializing_none]
17#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
18pub struct ListResponse {
19 pub ok: bool,
20 pub error: Option<String>,
21 pub response_metadata: Option<ResponseMetadata>,
22 pub members: Option<Vec<User>>,
23 pub cache_ts: Option<i32>,
24}
25
26pub async fn list<T>(
27 client: &T,
28 param: &ListRequest,
29 bot_token: &str,
30) -> Result<ListResponse, Error>
31where
32 T: SlackWebAPIClient,
33{
34 let url = get_slack_url("users.list");
35 let json = serde_json::to_string(¶m)?;
36
37 client
38 .post_json(&url, &json, bot_token)
39 .await
40 .and_then(|result| {
41 serde_json::from_str::<ListResponse>(&result).map_err(Error::SerdeJsonError)
42 })
43}
44
45#[cfg(test)]
46mod test {
47 use super::*;
48
49 use crate::http_client::MockSlackWebAPIClient;
50
51 use crate::users::user::UserProfile;
52
53 #[test]
54 fn convert_request() {
55 let request = ListRequest {
56 cursor: Some("test".to_string()),
57 include_locale: Some(true),
58 limit: Some(1),
59 team_id: Some("test".to_string()),
60 };
61 let json = r##"{
62 "cursor": "test",
63 "include_locale": true,
64 "limit": 1,
65 "team_id": "test"
66}"##;
67
68 let j = serde_json::to_string_pretty(&request).unwrap();
69 assert_eq!(json, j);
70
71 let s = serde_json::from_str::<ListRequest>(json).unwrap();
72 assert_eq!(request, s);
73 }
74
75 #[test]
76 fn convert_response() {
77 let response = ListResponse {
78 ok: true,
79 members: Some(vec![User {
80 id: Some("USLAOCKO".to_string()),
81 team_id: Some("T02H783QNL9".to_string()),
82 name: Some("slackbot".to_string()),
83 deleted: Some(false),
84 color: Some("757575".to_string()),
85 real_name: Some("Slackbot".to_string()),
86 tz: Some("America/Los_Angeles".to_string()),
87 tz_label: Some("Pacific Standard Time".to_string()),
88 tz_offset: Some(-28800),
89 profile: Some(UserProfile {
90 title: Some("test".to_string()),
91 phone: Some("test".to_string()),
92 skype: Some("test".to_string()),
93 real_name: Some("Slackbot".to_string()),
94 real_name_normalized: Some("Slackbot".to_string()),
95 display_name: Some("Slackbot".to_string()),
96 display_name_normalized: Some("Slackbot".to_string()),
97 status_text: Some("test".to_string()),
98 status_emoji: Some("test".to_string()),
99 status_expiration: Some(0),
100 always_active: Some(true),
101 first_name: Some("slackbot".to_string()),
102 last_name: Some("test".to_string()),
103 image_24: Some("https://example.com/img/slackbot_24.png".to_string()),
104 image_32: Some("https://example.com/img/slackbot_32.png".to_string()),
105 image_48: Some("https://example.com/img/slackbot_48.png".to_string()),
106 image_72: Some("https://example.com//img/slackbot_72.png".to_string()),
107 image_192: Some("https://example.com/img/avatar-slackbot.png".to_string()),
108 image_512: Some("https://example.com/img/slackbot_512.png".to_string()),
109 status_text_canonical: Some("".to_string()),
110 team: Some("T02H7RHQNL9".to_string()),
111 ..Default::default()
112 }),
113 is_admin: Some(false),
114 is_owner: Some(false),
115 is_primary_owner: Some(false),
116 is_restricted: Some(false),
117 is_ultra_restricted: Some(false),
118 is_bot: Some(false),
119 is_app_user: Some(false),
120 updated: Some(0),
121 is_email_confirmed: Some(false),
122 who_can_share_contact_card: Some("EVERYONE".to_string()),
123 ..Default::default()
124 }]),
125 cache_ts: Some(1111),
126 ..Default::default()
127 };
128 let json = r##"{
129 "ok": true,
130 "members": [
131 {
132 "id": "USLAOCKO",
133 "team_id": "T02H783QNL9",
134 "name": "slackbot",
135 "deleted": false,
136 "color": "757575",
137 "real_name": "Slackbot",
138 "tz": "America/Los_Angeles",
139 "tz_label": "Pacific Standard Time",
140 "tz_offset": -28800,
141 "profile": {
142 "first_name": "slackbot",
143 "last_name": "test",
144 "real_name": "Slackbot",
145 "real_name_normalized": "Slackbot",
146 "display_name": "Slackbot",
147 "display_name_normalized": "Slackbot",
148 "skype": "test",
149 "phone": "test",
150 "image_24": "https://example.com/img/slackbot_24.png",
151 "image_32": "https://example.com/img/slackbot_32.png",
152 "image_48": "https://example.com/img/slackbot_48.png",
153 "image_72": "https://example.com//img/slackbot_72.png",
154 "image_192": "https://example.com/img/avatar-slackbot.png",
155 "image_512": "https://example.com/img/slackbot_512.png",
156 "title": "test",
157 "status_text": "test",
158 "status_emoji": "test",
159 "status_expiration": 0,
160 "team": "T02H7RHQNL9",
161 "always_active": true,
162 "status_text_canonical": ""
163 },
164 "is_bot": false,
165 "is_admin": false,
166 "is_owner": false,
167 "is_primary_owner": false,
168 "is_restricted": false,
169 "is_ultra_restricted": false,
170 "is_app_user": false,
171 "updated": 0,
172 "is_email_confirmed": false,
173 "who_can_share_contact_card": "EVERYONE"
174 }
175 ],
176 "cache_ts": 1111
177}"##;
178
179 let j = serde_json::to_string_pretty(&response).unwrap();
180 assert_eq!(json, j);
181
182 let s = serde_json::from_str::<ListResponse>(json).unwrap();
183 assert_eq!(response, s);
184 }
185
186 #[async_std::test]
187 async fn test_list() {
188 let param = ListRequest {
189 team_id: Some("test_list".to_string()),
190 ..Default::default()
191 };
192
193 let mut mock = MockSlackWebAPIClient::new();
194 mock.expect_post_json().returning(|_, _, _| {
195 Ok(r##"{
196 "ok": true,
197 "members": [
198 {
199 "id": "USLAOCKO",
200 "team_id": "test_list",
201 "name": "slackbot",
202 "deleted": false,
203 "color": "757575",
204 "real_name": "Slackbot",
205 "tz": "America/Los_Angeles",
206 "tz_label": "Pacific Standard Time",
207 "tz_offset": -28800,
208 "profile": {
209 "first_name": "slackbot",
210 "last_name": "test",
211 "real_name": "Slackbot",
212 "real_name_normalized": "Slackbot",
213 "display_name": "Slackbot",
214 "display_name_normalized": "Slackbot",
215 "skype": "test",
216 "phone": "test",
217 "image_24": "https://example.com/img/slackbot_24.png",
218 "image_32": "https://example.com/img/slackbot_32.png",
219 "image_48": "https://example.com/img/slackbot_48.png",
220 "image_72": "https://example.com//img/slackbot_72.png",
221 "image_192": "https://example.com/img/avatar-slackbot.png",
222 "image_512": "https://example.com/img/slackbot_512.png",
223 "title": "test",
224 "status_text": "test",
225 "status_emoji": "test",
226 "status_expiration": 0,
227 "team": "T02H7RHQNL9",
228 "always_active": true,
229 "status_text_canonical": ""
230 },
231 "is_bot": false,
232 "is_admin": false,
233 "is_owner": false,
234 "is_primary_owner": false,
235 "is_restricted": false,
236 "is_ultra_restricted": false,
237 "is_app_user": false,
238 "updated": 0,
239 "is_email_confirmed": false,
240 "who_can_share_contact_card": "EVERYONE"
241 }
242 ],
243 "cache_ts": 1111
244}"##
245 .to_string())
246 });
247
248 let response = list(&mock, ¶m, &"test_token".to_string())
249 .await
250 .unwrap();
251 let expect = ListResponse {
252 ok: true,
253 members: Some(vec![User {
254 id: Some("USLAOCKO".to_string()),
255 team_id: Some("test_list".to_string()),
256 name: Some("slackbot".to_string()),
257 deleted: Some(false),
258 color: Some("757575".to_string()),
259 real_name: Some("Slackbot".to_string()),
260 tz: Some("America/Los_Angeles".to_string()),
261 tz_label: Some("Pacific Standard Time".to_string()),
262 tz_offset: Some(-28800),
263 profile: Some(UserProfile {
264 title: Some("test".to_string()),
265 phone: Some("test".to_string()),
266 skype: Some("test".to_string()),
267 real_name: Some("Slackbot".to_string()),
268 real_name_normalized: Some("Slackbot".to_string()),
269 display_name: Some("Slackbot".to_string()),
270 display_name_normalized: Some("Slackbot".to_string()),
271 status_text: Some("test".to_string()),
272 status_emoji: Some("test".to_string()),
273 status_expiration: Some(0),
274 always_active: Some(true),
275 first_name: Some("slackbot".to_string()),
276 last_name: Some("test".to_string()),
277 image_24: Some("https://example.com/img/slackbot_24.png".to_string()),
278 image_32: Some("https://example.com/img/slackbot_32.png".to_string()),
279 image_48: Some("https://example.com/img/slackbot_48.png".to_string()),
280 image_72: Some("https://example.com//img/slackbot_72.png".to_string()),
281 image_192: Some("https://example.com/img/avatar-slackbot.png".to_string()),
282 image_512: Some("https://example.com/img/slackbot_512.png".to_string()),
283 status_text_canonical: Some("".to_string()),
284 team: Some("T02H7RHQNL9".to_string()),
285 ..Default::default()
286 }),
287 is_admin: Some(false),
288 is_owner: Some(false),
289 is_primary_owner: Some(false),
290 is_restricted: Some(false),
291 is_ultra_restricted: Some(false),
292 is_bot: Some(false),
293 is_app_user: Some(false),
294 updated: Some(0),
295 is_email_confirmed: Some(false),
296 who_can_share_contact_card: Some("EVERYONE".to_string()),
297 ..Default::default()
298 }]),
299 cache_ts: Some(1111),
300 ..Default::default()
301 };
302
303 assert_eq!(expect, response);
304 }
305}