use openlark_core::{SDKResult, api::ApiRequest, config::Config, http::Transport};
use serde::{Deserialize, Serialize};
use crate::common::{api_endpoints::DriveApi, api_utils::*};
use super::models::{Comment, CreateCommentReplyList};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateCommentRequest {
pub file_token: String,
pub file_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id_type: Option<String>,
pub reply_list: CreateCommentReplyList,
}
impl CreateCommentRequest {
pub fn new(
file_token: impl Into<String>,
file_type: impl Into<String>,
reply_list: CreateCommentReplyList,
) -> Self {
Self {
file_token: file_token.into(),
file_type: file_type.into(),
user_id_type: None,
reply_list,
}
}
pub fn user_id_type(mut self, user_id_type: impl Into<String>) -> Self {
self.user_id_type = Some(user_id_type.into());
self
}
}
#[derive(Debug, Serialize)]
struct CreateCommentRequestBody {
reply_list: CreateCommentReplyList,
}
pub async fn create_comment(
request: CreateCommentRequest,
config: &Config,
option: Option<openlark_core::req_option::RequestOption>,
) -> SDKResult<Comment> {
if request.file_token.trim().is_empty() {
return Err(openlark_core::error::validation_error(
"file_token",
"file_token 不能为空",
));
}
if request.file_type.trim().is_empty() {
return Err(openlark_core::error::validation_error(
"file_type",
"file_type 不能为空",
));
}
super::validate_comment_file_type_for_create(&request.file_type)?;
if request.reply_list.replies.is_empty() {
return Err(openlark_core::error::validation_error(
"reply_list",
"reply_list.replies 不能为空",
));
}
let api_endpoint = DriveApi::CreateComment(request.file_token.clone());
let mut api_request: ApiRequest<Comment> =
ApiRequest::post(&api_endpoint.to_url()).body(serialize_params(
&CreateCommentRequestBody {
reply_list: request.reply_list,
},
"添加全文评论",
)?);
api_request = api_request.query("file_type", &request.file_type);
if let Some(user_id_type) = &request.user_id_type {
api_request = api_request.query("user_id_type", user_id_type);
}
let response = Transport::request(api_request, config, option).await?;
extract_response_data(response, "添加全文评论")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_comment_request_builder() {
let reply_list = CreateCommentReplyList {
replies: vec![super::super::CreateCommentReply {
content: super::super::models::CommentContent { elements: vec![] },
}],
};
let request = CreateCommentRequest::new("file_token", "docx", reply_list);
assert_eq!(request.file_token, "file_token");
assert_eq!(request.file_type, "docx");
assert!(request.user_id_type.is_none());
}
#[test]
fn test_create_comment_request_with_user_id_type() {
let reply_list = CreateCommentReplyList {
replies: vec![super::super::CreateCommentReply {
content: super::super::models::CommentContent { elements: vec![] },
}],
};
let request =
CreateCommentRequest::new("file_token", "docx", reply_list).user_id_type("union_id");
assert_eq!(request.user_id_type, Some("union_id".to_string()));
}
#[test]
fn test_create_comment_request_empty_fields() {
let reply_list = CreateCommentReplyList {
replies: vec![super::super::CreateCommentReply {
content: super::super::models::CommentContent { elements: vec![] },
}],
};
let request = CreateCommentRequest::new("", "docx", reply_list.clone());
assert!(request.file_token.is_empty());
let request2 = CreateCommentRequest::new("token", "", reply_list);
assert!(request2.file_type.is_empty());
}
}