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