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