Skip to main content

ncm_api_rs/api/
comment.rs

1use super::Query;
2use crate::error::Result;
3/// 发送/删除/回复评论
4/// 对应 Node.js module/comment.js
5use crate::request::{ApiClient, ApiResponse, CryptoType};
6use serde_json::json;
7
8impl ApiClient {
9    /// 发送/删除/回复评论
10    /// 对应 /comment
11    /// t: 1=发送, 0=删除, 2=回复
12    pub async fn comment(&self, query: &Query) -> Result<ApiResponse> {
13        let t = query.get_or("t", "1");
14        let action = match t.as_str() {
15            "1" => "add",
16            "0" => "delete",
17            "2" => "reply",
18            _ => "add",
19        };
20        let resource_type = query.get_or("type", "0");
21        let thread_id = crate::util::config::RESOURCE_TYPE_MAP
22            .get(resource_type.as_str())
23            .map(|prefix| format!("{}{}", prefix, query.get_or("id", "0")))
24            .unwrap_or_default();
25        let mut data = json!({
26            "threadId": thread_id
27        });
28        match action {
29            "add" => {
30                data["content"] = json!(query.get_or("content", ""));
31            }
32            "delete" => {
33                data["commentId"] = json!(query.get_or("commentId", "0"));
34            }
35            "reply" => {
36                data["commentId"] = json!(query.get_or("commentId", "0"));
37                data["content"] = json!(query.get_or("content", ""));
38            }
39            _ => {}
40        }
41        self.request(
42            &format!("/api/resource/comments/{}", action),
43            data,
44            query.to_option(CryptoType::Weapi),
45        )
46        .await
47    }
48}