open_lark/service/cloud_docs/comments/
patch.rs1use 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#[derive(Debug, Serialize, Default, Clone)]
20pub struct PatchCommentRequest {
21 #[serde(skip)]
22 api_request: ApiRequest,
23 #[serde(skip)]
25 file_token: String,
26 #[serde(skip)]
28 file_type: String,
29 #[serde(skip)]
31 comment_id: String,
32 is_solved: bool,
34 #[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 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 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 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 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 pub fn with_doc_type(mut self) -> Self {
98 self.request.file_type = "doc".to_string();
99 self
100 }
101
102 pub fn with_docx_type(mut self) -> Self {
104 self.request.file_type = "docx".to_string();
105 self
106 }
107
108 pub fn with_sheet_type(mut self) -> Self {
110 self.request.file_type = "sheet".to_string();
111 self
112 }
113
114 pub fn with_bitable_type(mut self) -> Self {
116 self.request.file_type = "bitable".to_string();
117 self
118 }
119
120 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 pub fn set_solved(mut self, is_solved: bool) -> Self {
128 self.request.is_solved = is_solved;
129 self
130 }
131
132 pub fn solve_comment(mut self) -> Self {
134 self.request.is_solved = true;
135 self
136 }
137
138 pub fn restore_comment(mut self) -> Self {
140 self.request.is_solved = false;
141 self
142 }
143
144 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 pub fn with_open_id(mut self) -> Self {
152 self.request.user_id_type = Some("open_id".to_string());
153 self
154 }
155
156 pub fn with_user_id(mut self) -> Self {
158 self.request.user_id_type = Some("user_id".to_string());
159 self
160 }
161
162 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
174impl_executable_builder_owned!(
176 PatchCommentRequestBuilder,
177 super::CommentsService,
178 PatchCommentRequest,
179 BaseResponse<PatchCommentResponse>,
180 patch
181);
182
183#[derive(Debug, Deserialize)]
185pub struct PatchCommentResponse {
186 pub comment_id: String,
188 pub is_solved: bool,
190 pub solved_time: Option<i64>,
192 pub solver_user_id: Option<String>,
194}
195
196impl ApiResponseTrait for PatchCommentResponse {
197 fn data_format() -> ResponseFormat {
198 ResponseFormat::Data
199 }
200}
201
202pub 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 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 pub fn is_solved(&self) -> bool {
231 self.is_solved
232 }
233
234 pub fn is_restored(&self) -> bool {
236 !self.is_solved
237 }
238
239 pub fn has_solved_time(&self) -> bool {
241 self.solved_time.is_some()
242 }
243
244 pub fn has_solver(&self) -> bool {
246 self.solver_user_id.is_some()
247 }
248
249 pub fn solved_time_formatted(&self) -> Option<String> {
251 self.solved_time.map(|timestamp| {
252 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}