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        openlark_core::http::Transport::request_typed(
97            request,
98            &self.config,
99            Some(option),
100            "创建评论",
101        )
102        .await
103    }
104}
105
106impl ApiResponseTrait for CreateCommentResponse {
107    fn data_format() -> ResponseFormat {
108        ResponseFormat::Data
109    }
110}
111
112#[cfg(test)]
113#[allow(unused_imports)]
114mod tests {
115    use std::sync::Arc;
116
117    use super::*;
118
119    #[test]
120    fn test_create_comment_builder() {
121        let config = Arc::new(
122            openlark_core::config::Config::builder()
123                .app_id("test")
124                .app_secret("test")
125                .build(),
126        );
127
128        let request =
129            CreateCommentRequest::new(config, "task_123".to_string()).content("这是一条评论");
130
131        assert_eq!(request.body.resource_id.as_deref(), Some("task_123"));
132        assert_eq!(request.body.content.as_deref(), Some("这是一条评论"));
133    }
134}