1use crate::error::Error;
2use crate::files::file::File;
3use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
4use serde::{Deserialize, Serialize};
5use serde_with::skip_serializing_none;
6
7#[skip_serializing_none]
8#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
9pub struct GetRequest {
10 pub channel: Option<String>,
11 pub file: Option<String>,
12 pub file_comment: Option<String>,
13 pub full: Option<bool>,
14 pub timestamp: Option<String>,
15}
16
17#[skip_serializing_none]
18#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
19pub struct GetResponse {
20 pub ok: bool,
21 pub error: Option<String>,
22 pub response_metadata: Option<ResponseMetadata>,
23 pub file: Option<File>,
24 #[serde(rename = "type")]
25 pub type_filed: Option<String>,
26}
27
28pub async fn get<T>(client: &T, param: &GetRequest, bot_token: &str) -> Result<GetResponse, Error>
29where
30 T: SlackWebAPIClient,
31{
32 let url = get_slack_url("reactions.get");
33 let json = serde_json::to_string(¶m)?;
34
35 client
36 .post_json(&url, &json, bot_token)
37 .await
38 .and_then(|result| {
39 serde_json::from_str::<GetResponse>(&result).map_err(Error::SerdeJsonError)
40 })
41}
42
43#[cfg(test)]
44mod test {
45 use super::*;
46 use crate::http_client::MockSlackWebAPIClient;
47 use crate::reactions::reaction::Reaction;
48
49 #[test]
50 fn convert_request() {
51 let request = GetRequest {
52 channel: Some("C0NF841BK".to_string()),
53 file: Some("F1234567890".to_string()),
54 file_comment: Some("Fc1234567890".to_string()),
55 full: Some(true),
56 timestamp: Some("1524523204.000192".to_string()),
57 };
58 let json = r##"{
59 "channel": "C0NF841BK",
60 "file": "F1234567890",
61 "file_comment": "Fc1234567890",
62 "full": true,
63 "timestamp": "1524523204.000192"
64}"##;
65
66 let j = serde_json::to_string_pretty(&request).unwrap();
67 assert_eq!(json, j);
68
69 let s = serde_json::from_str::<GetRequest>(json).unwrap();
70 assert_eq!(request, s);
71 }
72
73 #[test]
74 fn convert_response() {
75 let response = GetResponse {
76 ok: true,
77 file: Some(File {
78 channels: Some(vec!["C2U7V2YA2".to_string()]),
79 comments_count: Some(1),
80 created: Some(1507850315),
81 groups: Some(vec![]),
82 id: Some("F7H0D7ZA4".to_string()),
83 ims: Some(vec![]),
84 name: Some("computer.gif".to_string()),
85 reactions: Some(vec![Reaction {
86 count: Some(1),
87 name: Some("stuck_out_tongue_winking_eye".to_string()),
88 users: Some(vec!["U2U85N1RV".to_string()]),
89 }]),
90 timestamp: Some(1507850315),
91 title: Some("computer.gif".to_string()),
92 user: Some("U2U85N1RV".to_string()),
93 }),
94 type_filed: Some("file".to_string()),
95 ..Default::default()
96 };
97 let json = r##"{
98 "ok": true,
99 "file": {
100 "channels": [
101 "C2U7V2YA2"
102 ],
103 "comments_count": 1,
104 "created": 1507850315,
105 "groups": [],
106 "id": "F7H0D7ZA4",
107 "ims": [],
108 "name": "computer.gif",
109 "reactions": [
110 {
111 "count": 1,
112 "name": "stuck_out_tongue_winking_eye",
113 "users": [
114 "U2U85N1RV"
115 ]
116 }
117 ],
118 "timestamp": 1507850315,
119 "title": "computer.gif",
120 "user": "U2U85N1RV"
121 },
122 "type": "file"
123}"##;
124
125 let j = serde_json::to_string_pretty(&response).unwrap();
126 assert_eq!(json, j);
127
128 let s = serde_json::from_str::<GetResponse>(json).unwrap();
129 assert_eq!(response, s);
130 }
131
132 #[async_std::test]
133 async fn test_get() {
134 let param = GetRequest {
135 channel: Some("C0NF841BK".to_string()),
136 file: Some("F1234567890".to_string()),
137 file_comment: Some("Fc1234567890".to_string()),
138 full: Some(true),
139 timestamp: Some("1524523204.000192".to_string()),
140 };
141
142 let mut mock = MockSlackWebAPIClient::new();
143 mock.expect_post_json().returning(|_, _, _| {
144 Ok(r##"{
145 "ok": true,
146 "file": {
147 "channels": [
148 "C0NF841BK"
149 ],
150 "comments_count": 1,
151 "created": 1507850315,
152 "groups": [],
153 "id": "F1234567890",
154 "ims": [],
155 "name": "computer.gif",
156 "reactions": [
157 {
158 "count": 1,
159 "name": "stuck_out_tongue_winking_eye",
160 "users": [
161 "U2U85N1RV"
162 ]
163 }
164 ],
165 "timestamp": 1524523204,
166 "title": "computer.gif",
167 "user": "U2U85N1RV"
168 },
169 "type": "file"
170}"##
171 .to_string())
172 });
173
174 let response = get(&mock, ¶m, &"test_token".to_string()).await.unwrap();
175 let expect = GetResponse {
176 ok: true,
177 file: Some(File {
178 channels: Some(vec!["C0NF841BK".to_string()]),
179 comments_count: Some(1),
180 created: Some(1507850315),
181 groups: Some(vec![]),
182 id: Some("F1234567890".to_string()),
183 ims: Some(vec![]),
184 name: Some("computer.gif".to_string()),
185 reactions: Some(vec![Reaction {
186 count: Some(1),
187 name: Some("stuck_out_tongue_winking_eye".to_string()),
188 users: Some(vec!["U2U85N1RV".to_string()]),
189 }]),
190 timestamp: Some(1524523204),
191 title: Some("computer.gif".to_string()),
192 user: Some("U2U85N1RV".to_string()),
193 }),
194 type_filed: Some("file".to_string()),
195 ..Default::default()
196 };
197
198 assert_eq!(expect, response);
199 }
200}