open_lark/service/cloud_docs/comments/
patch.rs

1use reqwest::Method;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    core::{
6        api_req::ApiRequest,
7        api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
8        config::Config,
9        constants::AccessTokenType,
10        http::Transport,
11        req_option::RequestOption,
12        SDKResult,
13    },
14    impl_executable_builder_owned,
15};
16
17/// 解决/恢复评论请求
18#[derive(Debug, Serialize, Default, Clone)]
19pub struct PatchCommentRequest {
20    #[serde(skip)]
21    api_request: ApiRequest,
22    /// 文档token
23    #[serde(skip)]
24    file_token: String,
25    /// 文档类型:doc、docx、sheet、bitable
26    #[serde(skip)]
27    file_type: String,
28    /// 评论ID
29    #[serde(skip)]
30    comment_id: String,
31    /// 是否解决
32    is_solved: bool,
33    /// 用户ID类型
34    #[serde(skip_serializing_if = "Option::is_none")]
35    user_id_type: Option<String>,
36}
37
38impl PatchCommentRequest {
39    pub fn builder() -> PatchCommentRequestBuilder {
40        PatchCommentRequestBuilder::default()
41    }
42
43    pub fn new(
44        file_token: impl ToString,
45        file_type: impl ToString,
46        comment_id: impl ToString,
47        is_solved: bool,
48    ) -> Self {
49        Self {
50            file_token: file_token.to_string(),
51            file_type: file_type.to_string(),
52            comment_id: comment_id.to_string(),
53            is_solved,
54            ..Default::default()
55        }
56    }
57
58    /// 创建解决评论的请求
59    pub fn solve(
60        file_token: impl ToString,
61        file_type: impl ToString,
62        comment_id: impl ToString,
63    ) -> Self {
64        Self::new(file_token, file_type, comment_id, true)
65    }
66
67    /// 创建恢复评论的请求
68    pub fn restore(
69        file_token: impl ToString,
70        file_type: impl ToString,
71        comment_id: impl ToString,
72    ) -> Self {
73        Self::new(file_token, file_type, comment_id, false)
74    }
75}
76
77#[derive(Default)]
78pub struct PatchCommentRequestBuilder {
79    request: PatchCommentRequest,
80}
81
82impl PatchCommentRequestBuilder {
83    /// 文档token
84    pub fn file_token(mut self, file_token: impl ToString) -> Self {
85        self.request.file_token = file_token.to_string();
86        self
87    }
88
89    /// 文档类型
90    pub fn file_type(mut self, file_type: impl ToString) -> Self {
91        self.request.file_type = file_type.to_string();
92        self
93    }
94
95    /// 设置为文档类型
96    pub fn with_doc_type(mut self) -> Self {
97        self.request.file_type = "doc".to_string();
98        self
99    }
100
101    /// 设置为docx类型
102    pub fn with_docx_type(mut self) -> Self {
103        self.request.file_type = "docx".to_string();
104        self
105    }
106
107    /// 设置为电子表格类型
108    pub fn with_sheet_type(mut self) -> Self {
109        self.request.file_type = "sheet".to_string();
110        self
111    }
112
113    /// 设置为多维表格类型
114    pub fn with_bitable_type(mut self) -> Self {
115        self.request.file_type = "bitable".to_string();
116        self
117    }
118
119    /// 评论ID
120    pub fn comment_id(mut self, comment_id: impl ToString) -> Self {
121        self.request.comment_id = comment_id.to_string();
122        self
123    }
124
125    /// 是否解决
126    pub fn set_solved(mut self, is_solved: bool) -> Self {
127        self.request.is_solved = is_solved;
128        self
129    }
130
131    /// 解决评论
132    pub fn solve_comment(mut self) -> Self {
133        self.request.is_solved = true;
134        self
135    }
136
137    /// 恢复评论
138    pub fn restore_comment(mut self) -> Self {
139        self.request.is_solved = false;
140        self
141    }
142
143    /// 用户ID类型
144    pub fn user_id_type(mut self, user_id_type: impl ToString) -> Self {
145        self.request.user_id_type = Some(user_id_type.to_string());
146        self
147    }
148
149    /// 使用OpenID
150    pub fn with_open_id(mut self) -> Self {
151        self.request.user_id_type = Some("open_id".to_string());
152        self
153    }
154
155    /// 使用UserID
156    pub fn with_user_id(mut self) -> Self {
157        self.request.user_id_type = Some("user_id".to_string());
158        self
159    }
160
161    /// 使用UnionID
162    pub fn with_union_id(mut self) -> Self {
163        self.request.user_id_type = Some("union_id".to_string());
164        self
165    }
166
167    pub fn build(mut self) -> PatchCommentRequest {
168        self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
169        self.request
170    }
171}
172
173// 应用ExecutableBuilder trait到PatchCommentRequestBuilder
174impl_executable_builder_owned!(
175    PatchCommentRequestBuilder,
176    super::CommentsService,
177    PatchCommentRequest,
178    BaseResponse<PatchCommentResponse>,
179    patch
180);
181
182/// 解决/恢复评论响应
183#[derive(Debug, Deserialize)]
184pub struct PatchCommentResponse {
185    /// 评论ID
186    pub comment_id: String,
187    /// 是否解决
188    pub is_solved: bool,
189    /// 解决时间(毫秒时间戳)
190    pub solved_time: Option<i64>,
191    /// 解决者用户ID
192    pub solver_user_id: Option<String>,
193}
194
195impl ApiResponseTrait for PatchCommentResponse {
196    fn data_format() -> ResponseFormat {
197        ResponseFormat::Data
198    }
199}
200
201/// 解决/恢复评论
202pub async fn patch_comment(
203    request: PatchCommentRequest,
204    config: &Config,
205    option: Option<RequestOption>,
206) -> SDKResult<BaseResponse<PatchCommentResponse>> {
207    let mut api_req = request.api_request;
208    api_req.http_method = Method::PATCH;
209    api_req.api_path = format!(
210        "/open-apis/comment/v1/comments/{}?file_type={}&file_token={}",
211        request.comment_id, request.file_type, request.file_token
212    );
213
214    // 添加用户ID类型查询参数
215    if let Some(user_id_type) = request.user_id_type {
216        api_req.api_path = format!("{}&user_id_type={}", api_req.api_path, user_id_type);
217    }
218
219    api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
220
221    let api_resp = Transport::request(api_req, config, option).await?;
222    Ok(api_resp)
223}
224
225impl PatchCommentResponse {
226    /// 是否已解决
227    pub fn is_solved(&self) -> bool {
228        self.is_solved
229    }
230
231    /// 是否被恢复
232    pub fn is_restored(&self) -> bool {
233        !self.is_solved
234    }
235
236    /// 是否有解决时间
237    pub fn has_solved_time(&self) -> bool {
238        self.solved_time.is_some()
239    }
240
241    /// 是否有解决者
242    pub fn has_solver(&self) -> bool {
243        self.solver_user_id.is_some()
244    }
245
246    /// 获取解决时间的格式化字符串
247    pub fn solved_time_formatted(&self) -> Option<String> {
248        self.solved_time.map(|timestamp| {
249            // 这里可以根据需要格式化时间戳
250            format!("解决时间: {timestamp}")
251        })
252    }
253}
254
255#[cfg(test)]
256mod tests {
257    use super::*;
258
259    #[test]
260    fn test_patch_comment_request_builder() {
261        let request = PatchCommentRequest::builder()
262            .file_token("doccnxxxxxx")
263            .with_doc_type()
264            .comment_id("comment123")
265            .solve_comment()
266            .with_open_id()
267            .build();
268
269        assert_eq!(request.file_token, "doccnxxxxxx");
270        assert_eq!(request.file_type, "doc");
271        assert_eq!(request.comment_id, "comment123");
272        assert!(request.is_solved);
273        assert_eq!(request.user_id_type, Some("open_id".to_string()));
274    }
275
276    #[test]
277    fn test_patch_comment_convenience_methods() {
278        let solve_request = PatchCommentRequest::solve("doccnxxxxxx", "doc", "comment123");
279        assert!(solve_request.is_solved);
280
281        let restore_request = PatchCommentRequest::restore("doccnxxxxxx", "doc", "comment123");
282        assert!(!restore_request.is_solved);
283    }
284}