use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{
core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
config::Config,
constants::AccessTokenType,
endpoints::cloud_docs::*,
http::Transport,
req_option::RequestOption,
SDKResult,
},
impl_executable_builder_owned,
};
#[derive(Debug, Serialize, Default, Clone)]
pub struct ListCommentsRequest {
#[serde(skip)]
api_request: ApiRequest,
#[serde(skip)]
file_token: String,
#[serde(skip)]
file_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
is_whole: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
is_solved: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
page_size: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
page_token: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
user_id_type: Option<String>,
}
impl ListCommentsRequest {
pub fn builder() -> ListCommentsRequestBuilder {
ListCommentsRequestBuilder::default()
}
pub fn new(file_token: impl ToString, file_type: impl ToString) -> Self {
Self {
file_token: file_token.to_string(),
file_type: file_type.to_string(),
..Default::default()
}
}
}
#[derive(Default)]
pub struct ListCommentsRequestBuilder {
request: ListCommentsRequest,
}
impl ListCommentsRequestBuilder {
pub fn file_token(mut self, file_token: impl ToString) -> Self {
self.request.file_token = file_token.to_string();
self
}
pub fn file_type(mut self, file_type: impl ToString) -> Self {
self.request.file_type = file_type.to_string();
self
}
pub fn with_doc_type(mut self) -> Self {
self.request.file_type = "doc".to_string();
self
}
pub fn with_docx_type(mut self) -> Self {
self.request.file_type = "docx".to_string();
self
}
pub fn with_sheet_type(mut self) -> Self {
self.request.file_type = "sheet".to_string();
self
}
pub fn with_bitable_type(mut self) -> Self {
self.request.file_type = "bitable".to_string();
self
}
pub fn set_whole(mut self, is_whole: bool) -> Self {
self.request.is_whole = Some(is_whole);
self
}
pub fn whole_comments_only(mut self) -> Self {
self.request.is_whole = Some(true);
self
}
pub fn all_comment_types(mut self) -> Self {
self.request.is_whole = None;
self
}
pub fn set_solved(mut self, is_solved: bool) -> Self {
self.request.is_solved = Some(is_solved);
self
}
pub fn solved_comments_only(mut self) -> Self {
self.request.is_solved = Some(true);
self
}
pub fn unsolved_comments_only(mut self) -> Self {
self.request.is_solved = Some(false);
self
}
pub fn all_comments(mut self) -> Self {
self.request.is_solved = None;
self
}
pub fn page_size(mut self, page_size: i32) -> Self {
self.request.page_size = Some(page_size);
self
}
pub fn page_token(mut self, page_token: impl ToString) -> Self {
self.request.page_token = Some(page_token.to_string());
self
}
pub fn user_id_type(mut self, user_id_type: impl ToString) -> Self {
self.request.user_id_type = Some(user_id_type.to_string());
self
}
pub fn build(mut self) -> ListCommentsRequest {
self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
self.request
}
}
impl_executable_builder_owned!(
ListCommentsRequestBuilder,
super::CommentsService,
ListCommentsRequest,
BaseResponse<ListCommentsResponse>,
list
);
#[derive(Debug, Deserialize)]
pub struct Comment {
pub comment_id: String,
pub user_id: String,
pub create_time: i64,
pub update_time: i64,
pub is_solved: bool,
pub solved_time: Option<i64>,
pub solver_user_id: Option<String>,
pub has_more: bool,
pub page_token: Option<String>,
pub reply_list: Option<Vec<Reply>>,
pub is_whole: Option<bool>,
pub quote: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct Reply {
pub reply_id: String,
pub user_id: String,
pub create_time: i64,
pub update_time: i64,
pub content: ReplyContent,
pub extra: Option<serde_json::Value>,
}
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
pub struct ReplyContent {
pub elements: Vec<ContentElement>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ContentElement {
#[serde(rename = "type")]
pub element_type: String,
pub text_run: Option<TextRun>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TextRun {
pub text: String,
pub style: Option<serde_json::Value>,
}
#[derive(Debug, Deserialize)]
pub struct ListCommentsResponse {
pub items: Vec<Comment>,
pub has_more: bool,
pub page_token: Option<String>,
}
impl ApiResponseTrait for ListCommentsResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn list_comments(
request: ListCommentsRequest,
config: &Config,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<ListCommentsResponse>> {
let mut api_req = request.api_request;
api_req.http_method = Method::GET;
api_req.api_path = format!(
"{}?file_type={}&file_token={}",
COMMENT_V1_COMMENTS, request.file_type, request.file_token
);
let mut query_params = Vec::new();
if let Some(is_whole) = request.is_whole {
query_params.push(format!("is_whole={is_whole}"));
}
if let Some(is_solved) = request.is_solved {
query_params.push(format!("is_solved={is_solved}"));
}
if let Some(page_size) = request.page_size {
query_params.push(format!("page_size={page_size}"));
}
if let Some(page_token) = request.page_token {
query_params.push(format!("page_token={page_token}"));
}
if let Some(user_id_type) = request.user_id_type {
query_params.push(format!("user_id_type={user_id_type}"));
}
if !query_params.is_empty() {
api_req.api_path = format!("{}&{}", api_req.api_path, query_params.join("&"));
}
api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
let api_resp = Transport::request(api_req, config, option).await?;
Ok(api_resp)
}
impl Comment {
pub fn get_text_content(&self) -> String {
if let Some(replies) = &self.reply_list {
replies
.iter()
.map(|reply| reply.get_text_content())
.collect::<Vec<_>>()
.join("\n")
} else {
String::new()
}
}
pub fn has_replies(&self) -> bool {
self.reply_list
.as_ref()
.is_some_and(|replies| !replies.is_empty())
}
pub fn reply_count(&self) -> usize {
self.reply_list.as_ref().map_or(0, |replies| replies.len())
}
}
impl Reply {
pub fn get_text_content(&self) -> String {
self.content
.elements
.iter()
.filter_map(|element| {
element
.text_run
.as_ref()
.map(|text_run| text_run.text.clone())
})
.collect::<Vec<_>>()
.join("")
}
}
#[cfg(test)]
#[allow(unused_variables, unused_unsafe)]
mod tests {
use super::*;
#[test]
fn test_list_comments_request_builder() {
let request = ListCommentsRequest::builder()
.file_token("doccnxxxxxx")
.with_doc_type()
.whole_comments_only()
.unsolved_comments_only()
.page_size(20)
.user_id_type("open_id")
.build();
assert_eq!(request.file_token, "doccnxxxxxx");
assert_eq!(request.file_type, "doc");
assert_eq!(request.is_whole, Some(true));
assert_eq!(request.is_solved, Some(false));
assert_eq!(request.page_size, Some(20));
assert_eq!(request.user_id_type, Some("open_id".to_string()));
}
}