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::{ContentElement, ReplyContent, TextRun};
#[derive(Debug, Serialize, Default, Clone)]
pub struct CreateCommentRequest {
#[serde(skip)]
api_request: ApiRequest,
#[serde(skip)]
file_token: String,
#[serde(skip)]
file_type: String,
content: ReplyContent,
#[serde(skip_serializing_if = "Option::is_none")]
user_id_type: Option<String>,
}
impl CreateCommentRequest {
pub fn builder() -> CreateCommentRequestBuilder {
CreateCommentRequestBuilder::default()
}
pub fn new(file_token: impl ToString, file_type: impl ToString, content: ReplyContent) -> Self {
Self {
file_token: file_token.to_string(),
file_type: file_type.to_string(),
content,
..Default::default()
}
}
pub fn with_text(
file_token: impl ToString,
file_type: impl ToString,
text: impl ToString,
) -> Self {
let content = ReplyContent {
elements: vec![ContentElement {
element_type: "text_run".to_string(),
text_run: Some(TextRun {
text: text.to_string(),
style: None,
}),
}],
};
Self::new(file_token, file_type, content)
}
}
#[derive(Default)]
pub struct CreateCommentRequestBuilder {
request: CreateCommentRequest,
}
impl CreateCommentRequestBuilder {
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 content(mut self, content: ReplyContent) -> Self {
self.request.content = content;
self
}
pub fn text(mut self, text: impl ToString) -> Self {
let element = ContentElement {
element_type: "text_run".to_string(),
text_run: Some(TextRun {
text: text.to_string(),
style: None,
}),
};
self.request.content.elements.push(element);
self
}
pub fn styled_text(mut self, text: impl ToString, style: serde_json::Value) -> Self {
let element = ContentElement {
element_type: "text_run".to_string(),
text_run: Some(TextRun {
text: text.to_string(),
style: Some(style),
}),
};
self.request.content.elements.push(element);
self
}
pub fn bold_text(self, text: impl ToString) -> Self {
let style = serde_json::json!({
"bold": true
});
self.styled_text(text, style)
}
pub fn italic_text(self, text: impl ToString) -> Self {
let style = serde_json::json!({
"italic": true
});
self.styled_text(text, style)
}
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) -> CreateCommentRequest {
self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
self.request
}
}
#[derive(Debug, Deserialize)]
pub struct CreatedComment {
pub comment_id: String,
pub user_id: String,
pub create_time: i64,
pub update_time: i64,
pub is_solved: bool,
pub is_whole: Option<bool>,
}
impl_executable_builder_owned!(
CreateCommentRequestBuilder,
super::CommentsService,
CreateCommentRequest,
BaseResponse<CreateCommentResponse>,
create
);
#[derive(Debug, Deserialize)]
pub struct CreateCommentResponse {
pub comment: CreatedComment,
}
impl ApiResponseTrait for CreateCommentResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn create_comment(
request: CreateCommentRequest,
config: &Config,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<CreateCommentResponse>> {
let mut api_req = request.api_request;
api_req.http_method = Method::POST;
api_req.api_path = format!(
"{}?file_type={}&file_token={}",
COMMENT_V1_COMMENTS, request.file_type, request.file_token
);
if let Some(user_id_type) = request.user_id_type {
api_req.api_path = format!("{}&user_id_type={}", api_req.api_path, user_id_type);
}
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 CreatedComment {
pub fn is_whole_comment(&self) -> bool {
self.is_whole.unwrap_or(false)
}
pub fn created_at_formatted(&self) -> String {
format!("创建时间: {}", self.create_time)
}
pub fn is_solved(&self) -> bool {
self.is_solved
}
}
pub struct ContentBuilder {
elements: Vec<ContentElement>,
}
impl ContentBuilder {
pub fn new() -> Self {
Self {
elements: Vec::new(),
}
}
pub fn add_text(mut self, text: impl ToString) -> Self {
self.elements.push(ContentElement {
element_type: "text_run".to_string(),
text_run: Some(TextRun {
text: text.to_string(),
style: None,
}),
});
self
}
pub fn add_styled_text(mut self, text: impl ToString, style: serde_json::Value) -> Self {
self.elements.push(ContentElement {
element_type: "text_run".to_string(),
text_run: Some(TextRun {
text: text.to_string(),
style: Some(style),
}),
});
self
}
pub fn add_bold(self, text: impl ToString) -> Self {
let style = serde_json::json!({ "bold": true });
self.add_styled_text(text, style)
}
pub fn add_italic(self, text: impl ToString) -> Self {
let style = serde_json::json!({ "italic": true });
self.add_styled_text(text, style)
}
pub fn add_underline(self, text: impl ToString) -> Self {
let style = serde_json::json!({ "underline": true });
self.add_styled_text(text, style)
}
pub fn build(self) -> ReplyContent {
ReplyContent {
elements: self.elements,
}
}
}
impl Default for ContentBuilder {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
#[allow(unused_variables, unused_unsafe)]
mod tests {
use super::*;
#[test]
fn test_create_comment_request_builder() {
let request = CreateCommentRequest::builder()
.file_token("doccnxxxxxx")
.with_doc_type()
.text("这是一条评论")
.bold_text("重要内容")
.with_open_id()
.build();
assert_eq!(request.file_token, "doccnxxxxxx");
assert_eq!(request.file_type, "doc");
assert_eq!(request.content.elements.len(), 2);
assert_eq!(request.user_id_type, Some("open_id".to_string()));
}
#[test]
fn test_create_comment_with_text() {
let request = CreateCommentRequest::with_text("doccnxxxxxx", "doc", "简单评论");
assert_eq!(request.file_token, "doccnxxxxxx");
assert_eq!(request.file_type, "doc");
assert_eq!(request.content.elements.len(), 1);
}
#[test]
fn test_content_builder() {
let content = ContentBuilder::new()
.add_text("普通文本 ")
.add_bold("粗体文本 ")
.add_italic("斜体文本")
.build();
assert_eq!(content.elements.len(), 3);
assert_eq!(content.elements[0].element_type, "text_run");
}
}