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 PatchCommentRequest {
#[serde(skip)]
api_request: ApiRequest,
#[serde(skip)]
file_token: String,
#[serde(skip)]
file_type: String,
#[serde(skip)]
comment_id: String,
is_solved: bool,
#[serde(skip_serializing_if = "Option::is_none")]
user_id_type: Option<String>,
}
impl PatchCommentRequest {
pub fn builder() -> PatchCommentRequestBuilder {
PatchCommentRequestBuilder::default()
}
pub fn new(
file_token: impl ToString,
file_type: impl ToString,
comment_id: impl ToString,
is_solved: bool,
) -> Self {
Self {
file_token: file_token.to_string(),
file_type: file_type.to_string(),
comment_id: comment_id.to_string(),
is_solved,
..Default::default()
}
}
pub fn solve(
file_token: impl ToString,
file_type: impl ToString,
comment_id: impl ToString,
) -> Self {
Self::new(file_token, file_type, comment_id, true)
}
pub fn restore(
file_token: impl ToString,
file_type: impl ToString,
comment_id: impl ToString,
) -> Self {
Self::new(file_token, file_type, comment_id, false)
}
}
#[derive(Default)]
pub struct PatchCommentRequestBuilder {
request: PatchCommentRequest,
}
impl PatchCommentRequestBuilder {
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 set_solved(mut self, is_solved: bool) -> Self {
self.request.is_solved = is_solved;
self
}
pub fn solve_comment(mut self) -> Self {
self.request.is_solved = true;
self
}
pub fn restore_comment(mut self) -> Self {
self.request.is_solved = false;
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) -> PatchCommentRequest {
self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
self.request
}
}
impl_executable_builder_owned!(
PatchCommentRequestBuilder,
super::CommentsService,
PatchCommentRequest,
BaseResponse<PatchCommentResponse>,
patch
);
#[derive(Debug, Deserialize)]
pub struct PatchCommentResponse {
pub comment_id: String,
pub is_solved: bool,
pub solved_time: Option<i64>,
pub solver_user_id: Option<String>,
}
impl ApiResponseTrait for PatchCommentResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn patch_comment(
request: PatchCommentRequest,
config: &Config,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<PatchCommentResponse>> {
let mut api_req = request.api_request;
api_req.http_method = Method::PATCH;
api_req.api_path = format!(
"{}?file_type={}&file_token={}",
COMMENT_V1_COMMENT_GET.replace("{}", &request.comment_id),
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 PatchCommentResponse {
pub fn is_solved(&self) -> bool {
self.is_solved
}
pub fn is_restored(&self) -> bool {
!self.is_solved
}
pub fn has_solved_time(&self) -> bool {
self.solved_time.is_some()
}
pub fn has_solver(&self) -> bool {
self.solver_user_id.is_some()
}
pub fn solved_time_formatted(&self) -> Option<String> {
self.solved_time.map(|timestamp| {
format!("解决时间: {timestamp}")
})
}
}
#[cfg(test)]
#[allow(unused_variables, unused_unsafe)]
mod tests {
use super::*;
#[test]
fn test_patch_comment_request_builder() {
let request = PatchCommentRequest::builder()
.file_token("doccnxxxxxx")
.with_doc_type()
.comment_id("comment123")
.solve_comment()
.with_open_id()
.build();
assert_eq!(request.file_token, "doccnxxxxxx");
assert_eq!(request.file_type, "doc");
assert_eq!(request.comment_id, "comment123");
assert!(request.is_solved);
assert_eq!(request.user_id_type, Some("open_id".to_string()));
}
#[test]
fn test_patch_comment_convenience_methods() {
let solve_request = PatchCommentRequest::solve("doccnxxxxxx", "doc", "comment123");
assert!(solve_request.is_solved);
let restore_request = PatchCommentRequest::restore("doccnxxxxxx", "doc", "comment123");
assert!(!restore_request.is_solved);
}
}