Skip to main content

openlark_workflow/v2/comment/
create.rs

1//! 创建评论
2//!
3//! docPath: <https://open.feishu.cn/document/task-v2/comment/create>
4
5use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use crate::v2::comment::models::{CreateCommentBody, CreateCommentResponse};
7use openlark_core::{
8    SDKResult,
9    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
10    config::Config,
11    validate_required,
12};
13use std::sync::Arc;
14
15/// 创建评论请求
16#[derive(Debug, Clone)]
17pub struct CreateCommentRequest {
18    /// 配置信息
19    config: Arc<Config>,
20    /// 请求体
21    body: CreateCommentBody,
22    /// 用户 ID 类型
23    user_id_type: Option<String>,
24}
25
26impl CreateCommentRequest {
27    /// 创建新的请求构建器。
28    pub fn new(config: Arc<Config>, task_guid: String) -> Self {
29        Self {
30            config,
31            body: CreateCommentBody {
32                resource_type: Some("task".to_string()),
33                resource_id: Some(task_guid),
34                ..CreateCommentBody::default()
35            },
36            user_id_type: None,
37        }
38    }
39
40    /// 设置评论内容
41    pub fn content(mut self, content: impl Into<String>) -> Self {
42        self.body.content = Some(content.into());
43        self
44    }
45
46    /// 设置被回复评论的 ID。
47    pub fn reply_to_comment_id(mut self, comment_id: impl Into<String>) -> Self {
48        self.body.reply_to_comment_id = Some(comment_id.into());
49        self
50    }
51
52    /// 设置资源类型。
53    pub fn resource_type(mut self, resource_type: impl Into<String>) -> Self {
54        self.body.resource_type = Some(resource_type.into());
55        self
56    }
57
58    /// 设置资源 ID。
59    pub fn resource_id(mut self, resource_id: impl Into<String>) -> Self {
60        self.body.resource_id = Some(resource_id.into());
61        self
62    }
63
64    /// 设置用户 ID 类型。
65    pub fn user_id_type(mut self, user_id_type: impl Into<String>) -> Self {
66        self.user_id_type = Some(user_id_type.into());
67        self
68    }
69
70    /// 执行请求
71    pub async fn execute(self) -> SDKResult<CreateCommentResponse> {
72        self.execute_with_options(openlark_core::req_option::RequestOption::default())
73            .await
74    }
75
76    /// 执行请求(带选项)
77    pub async fn execute_with_options(
78        self,
79        option: openlark_core::req_option::RequestOption,
80    ) -> SDKResult<CreateCommentResponse> {
81        // 验证必填字段
82        validate_required!(
83            self.body.content.as_deref().unwrap_or_default().trim(),
84            "评论内容不能为空"
85        );
86
87        let api_endpoint = TaskApiV2::CommentCreate;
88        let mut request = ApiRequest::<CreateCommentResponse>::post(api_endpoint.to_url());
89
90        let request_body = &self.body;
91        request = request.body(serialize_params(request_body, "创建评论")?);
92        if let Some(user_id_type) = &self.user_id_type {
93            request = request.query("user_id_type", user_id_type);
94        }
95
96        let response =
97            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
98        extract_response_data(response, "创建评论")
99    }
100}
101
102impl ApiResponseTrait for CreateCommentResponse {
103    fn data_format() -> ResponseFormat {
104        ResponseFormat::Data
105    }
106}
107
108#[cfg(test)]
109#[allow(unused_imports)]
110mod tests {
111    use std::sync::Arc;
112
113    use super::*;
114
115    #[test]
116    fn test_create_comment_builder() {
117        let config = Arc::new(
118            openlark_core::config::Config::builder()
119                .app_id("test")
120                .app_secret("test")
121                .build(),
122        );
123
124        let request =
125            CreateCommentRequest::new(config, "task_123".to_string()).content("这是一条评论");
126
127        assert_eq!(request.body.resource_id.as_deref(), Some("task_123"));
128        assert_eq!(request.body.content.as_deref(), Some("这是一条评论"));
129    }
130}