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,
};
use super::list::Reply;
#[derive(Debug, Serialize, Default, Clone)]
pub struct ListRepliesRequest {
#[serde(skip)]
api_request: ApiRequest,
#[serde(skip)]
file_token: String,
#[serde(skip)]
file_type: String,
#[serde(skip)]
comment_id: String,
#[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 ListRepliesRequest {
pub fn builder() -> ListRepliesRequestBuilder {
ListRepliesRequestBuilder::default()
}
pub fn new(
file_token: impl ToString,
file_type: impl ToString,
comment_id: impl ToString,
) -> Self {
Self {
file_token: file_token.to_string(),
file_type: file_type.to_string(),
comment_id: comment_id.to_string(),
..Default::default()
}
}
}
#[derive(Default)]
pub struct ListRepliesRequestBuilder {
request: ListRepliesRequest,
}
impl ListRepliesRequestBuilder {
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 comment_id(mut self, comment_id: impl ToString) -> Self {
self.request.comment_id = comment_id.to_string();
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 with_open_id(mut self) -> Self {
self.request.user_id_type = Some("open_id".to_string());
self
}
pub fn with_user_id(mut self) -> Self {
self.request.user_id_type = Some("user_id".to_string());
self
}
pub fn with_union_id(mut self) -> Self {
self.request.user_id_type = Some("union_id".to_string());
self
}
pub fn build(mut self) -> ListRepliesRequest {
self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
self.request
}
}
impl_executable_builder_owned!(
ListRepliesRequestBuilder,
super::CommentsService,
ListRepliesRequest,
BaseResponse<ListRepliesResponse>,
list_replies
);
#[derive(Debug, Deserialize)]
pub struct ListRepliesResponse {
pub items: Vec<Reply>,
pub has_more: bool,
pub page_token: Option<String>,
}
impl ApiResponseTrait for ListRepliesResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn list_replies(
request: ListRepliesRequest,
config: &Config,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<ListRepliesResponse>> {
let mut api_req = request.api_request;
api_req.http_method = Method::GET;
api_req.api_path = format!(
"{}?file_type={}&file_token={}",
COMMENT_V1_COMMENT_REPLIES.replace("{}", &request.comment_id),
request.file_type,
request.file_token
);
let mut query_params = Vec::new();
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 ListRepliesResponse {
pub fn count(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub fn replies_by_user(&self, user_id: &str) -> Vec<&Reply> {
self.items
.iter()
.filter(|reply| reply.user_id == user_id)
.collect()
}
pub fn latest_reply(&self) -> Option<&Reply> {
self.items.iter().max_by_key(|reply| reply.create_time)
}
pub fn earliest_reply(&self) -> Option<&Reply> {
self.items.iter().min_by_key(|reply| reply.create_time)
}
pub fn sorted_by_time(&self) -> Vec<&Reply> {
let mut replies: Vec<&Reply> = self.items.iter().collect();
replies.sort_by_key(|reply| reply.create_time);
replies
}
pub fn sorted_by_time_desc(&self) -> Vec<&Reply> {
let mut replies: Vec<&Reply> = self.items.iter().collect();
replies.sort_by_key(|reply| std::cmp::Reverse(reply.create_time));
replies
}
pub fn get_all_text_content(&self) -> Vec<String> {
self.items
.iter()
.map(|reply| reply.get_text_content())
.collect()
}
pub fn summary(&self) -> String {
format!(
"回复总数: {}, 是否有更多: {}, 最新回复时间: {}",
self.count(),
self.has_more,
self.latest_reply()
.map(|r| r.create_time.to_string())
.unwrap_or_else(|| "无".to_string())
)
}
}
#[cfg(test)]
#[allow(unused_variables, unused_unsafe)]
mod tests {
use super::*;
#[test]
fn test_list_replies_request_builder() {
let request = ListRepliesRequest::builder()
.file_token("doccnxxxxxx")
.with_doc_type()
.comment_id("comment123")
.page_size(20)
.with_open_id()
.build();
assert_eq!(request.file_token, "doccnxxxxxx");
assert_eq!(request.file_type, "doc");
assert_eq!(request.comment_id, "comment123");
assert_eq!(request.page_size, Some(20));
assert_eq!(request.user_id_type, Some("open_id".to_string()));
}
#[test]
fn test_list_replies_new() {
let request = ListRepliesRequest::new("doccnxxxxxx", "doc", "comment123");
assert_eq!(request.file_token, "doccnxxxxxx");
assert_eq!(request.file_type, "doc");
assert_eq!(request.comment_id, "comment123");
}
}