slack_rust/conversations/
set_topic.rs1use crate::error::Error;
2use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
3use serde::{Deserialize, Serialize};
4use serde_with::skip_serializing_none;
5
6#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
7pub struct SetTopicRequest {
8 pub channel: String,
9 pub topic: String,
10}
11
12#[skip_serializing_none]
13#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
14pub struct SetTopicResponse {
15 pub ok: bool,
16 pub error: Option<String>,
17 pub response_metadata: Option<ResponseMetadata>,
18 pub topic: Option<String>,
19}
20
21pub async fn set_topic<T>(
22 client: &T,
23 param: &SetTopicRequest,
24 bot_token: &str,
25) -> Result<SetTopicResponse, Error>
26where
27 T: SlackWebAPIClient,
28{
29 let url = get_slack_url("conversations.setTopic");
30 let json = serde_json::to_string(¶m)?;
31
32 client
33 .post_json(&url, &json, bot_token)
34 .await
35 .and_then(|result| {
36 serde_json::from_str::<SetTopicResponse>(&result).map_err(Error::SerdeJsonError)
37 })
38}
39
40#[cfg(test)]
41mod test {
42 use super::*;
43 use crate::http_client::MockSlackWebAPIClient;
44
45 #[test]
46 fn convert_request() {
47 let request = SetTopicRequest {
48 channel: "C1234567890".to_string(),
49 topic: "Apply topically for best effects".to_string(),
50 };
51 let json = r##"{
52 "channel": "C1234567890",
53 "topic": "Apply topically for best effects"
54}"##;
55
56 let j = serde_json::to_string_pretty(&request).unwrap();
57 assert_eq!(json, j);
58
59 let s = serde_json::from_str::<SetTopicRequest>(json).unwrap();
60 assert_eq!(request, s);
61 }
62
63 #[test]
64 fn convert_response() {
65 let response = SetTopicResponse {
66 ok: true,
67 topic: Some("Apply topically for best effects".to_string()),
68 ..Default::default()
69 };
70 let json = r##"{
71 "ok": true,
72 "topic": "Apply topically for best effects"
73}"##;
74
75 let j = serde_json::to_string_pretty(&response).unwrap();
76 assert_eq!(json, j);
77
78 let s = serde_json::from_str::<SetTopicResponse>(json).unwrap();
79 assert_eq!(response, s);
80 }
81
82 #[async_std::test]
83 async fn test_set_topic() {
84 let param = SetTopicRequest {
85 channel: "C1234567890".to_string(),
86 topic: "Apply topically for best effects".to_string(),
87 };
88
89 let mut mock = MockSlackWebAPIClient::new();
90 mock.expect_post_json().returning(|_, _, _| {
91 Ok(r##"{
92 "ok": true,
93 "topic": "Apply topically for best effects"
94}"##
95 .to_string())
96 });
97
98 let response = set_topic(&mock, ¶m, &"test_token".to_string())
99 .await
100 .unwrap();
101 let expect = SetTopicResponse {
102 ok: true,
103 topic: Some("Apply topically for best effects".to_string()),
104 ..Default::default()
105 };
106
107 assert_eq!(expect, response);
108 }
109}