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