slack_rust/reactions/
remove.rs

1use crate::error::Error;
2use crate::http_client::{get_slack_url, DefaultResponse, 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 RemoveRequest {
9    pub name: String,
10    pub channel: Option<String>,
11    pub file: Option<String>,
12    pub file_comment: Option<String>,
13    pub timestamp: Option<String>,
14}
15
16pub async fn remove<T>(
17    client: &T,
18    param: &RemoveRequest,
19    bot_token: &str,
20) -> Result<DefaultResponse, Error>
21where
22    T: SlackWebAPIClient,
23{
24    let url = get_slack_url("reactions.remove");
25    let json = serde_json::to_string(&param)?;
26
27    client
28        .post_json(&url, &json, bot_token)
29        .await
30        .and_then(|result| {
31            serde_json::from_str::<DefaultResponse>(&result).map_err(Error::SerdeJsonError)
32        })
33}
34
35#[cfg(test)]
36mod test {
37    use super::*;
38    use crate::http_client::MockSlackWebAPIClient;
39
40    #[test]
41    fn convert_request() {
42        let request = RemoveRequest {
43            name: "thumbsup".to_string(),
44            channel: Some("C1234567890".to_string()),
45            file: Some("F1234567890".to_string()),
46            file_comment: Some("Fc1234567890".to_string()),
47            timestamp: Some("1234567890.123456".to_string()),
48        };
49        let json = r##"{
50  "name": "thumbsup",
51  "channel": "C1234567890",
52  "file": "F1234567890",
53  "file_comment": "Fc1234567890",
54  "timestamp": "1234567890.123456"
55}"##;
56
57        let j = serde_json::to_string_pretty(&request).unwrap();
58        assert_eq!(json, j);
59
60        let s = serde_json::from_str::<RemoveRequest>(json).unwrap();
61        assert_eq!(request, s);
62    }
63
64    #[async_std::test]
65    async fn test_remove() {
66        let param = RemoveRequest {
67            name: "thumbsup".to_string(),
68            channel: Some("C1234567890".to_string()),
69            file: Some("F1234567890".to_string()),
70            file_comment: Some("Fc1234567890".to_string()),
71            timestamp: Some("1234567890.123456".to_string()),
72        };
73
74        let mut mock = MockSlackWebAPIClient::new();
75        mock.expect_post_json().returning(|_, _, _| {
76            Ok(r##"{
77  "ok": true
78}"##
79            .to_string())
80        });
81
82        let response = remove(&mock, &param, &"test_token".to_string())
83            .await
84            .unwrap();
85        let expect = DefaultResponse {
86            ok: true,
87            ..Default::default()
88        };
89
90        assert_eq!(expect, response);
91    }
92}