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::*, EndpointBuilder},
http::Transport,
req_option::RequestOption,
SDKResult,
},
impl_executable_builder_owned,
};
use super::list::ReplyContent;
#[derive(Debug, Serialize, Default, Clone)]
pub struct UpdateReplyRequest {
#[serde(skip)]
api_request: ApiRequest,
#[serde(skip)]
file_token: String,
#[serde(skip)]
file_type: String,
#[serde(skip)]
comment_id: String,
#[serde(skip)]
reply_id: String,
content: ReplyContent,
#[serde(skip_serializing_if = "Option::is_none")]
user_id_type: Option<String>,
}
impl UpdateReplyRequest {
pub fn builder() -> UpdateReplyRequestBuilder {
UpdateReplyRequestBuilder::default()
}
pub fn new(
file_token: impl ToString,
file_type: impl ToString,
comment_id: impl ToString,
reply_id: impl ToString,
content: ReplyContent,
) -> Self {
Self {
file_token: file_token.to_string(),
file_type: file_type.to_string(),
comment_id: comment_id.to_string(),
reply_id: reply_id.to_string(),
content,
..Default::default()
}
}
}
#[derive(Default)]
pub struct UpdateReplyRequestBuilder {
request: UpdateReplyRequest,
}
impl UpdateReplyRequestBuilder {
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 reply_id(mut self, reply_id: impl ToString) -> Self {
self.request.reply_id = reply_id.to_string();
self
}
pub fn content(mut self, content: ReplyContent) -> Self {
self.request.content = content;
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) -> UpdateReplyRequest {
self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
self.request
}
}
#[derive(Debug, Deserialize)]
pub struct UpdatedReply {
pub reply_id: String,
pub user_id: String,
pub create_time: i64,
pub update_time: i64,
pub content: ReplyContent,
}
impl_executable_builder_owned!(
UpdateReplyRequestBuilder,
super::CommentsService,
UpdateReplyRequest,
BaseResponse<UpdateReplyResponse>,
update_reply
);
#[derive(Debug, Deserialize)]
pub struct UpdateReplyResponse {
pub reply: UpdatedReply,
}
impl ApiResponseTrait for UpdateReplyResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub async fn update_reply(
request: UpdateReplyRequest,
config: &Config,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<UpdateReplyResponse>> {
let mut api_req = request.api_request;
api_req.http_method = Method::PUT;
api_req.api_path = format!(
"{}?file_type={}&file_token={}",
EndpointBuilder::replace_params_from_array(
COMMENT_V1_COMMENT_REPLY_UPDATE,
&[
("comment_id", &request.comment_id),
("reply_id", &request.reply_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 UpdatedReply {
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("")
}
pub fn is_updated(&self) -> bool {
self.update_time > self.create_time
}
pub fn time_since_creation(&self) -> i64 {
self.update_time - self.create_time
}
pub fn updated_at_formatted(&self) -> String {
format!("更新时间: {}", self.update_time)
}
pub fn summary(&self) -> String {
format!(
"回复ID: {}, 用户: {}, 内容: {}, 更新时间: {}",
self.reply_id,
self.user_id,
self.get_text_content(),
self.update_time
)
}
}
impl UpdateReplyResponse {
pub fn reply_id(&self) -> &str {
&self.reply.reply_id
}
pub fn user_id(&self) -> &str {
&self.reply.user_id
}
pub fn get_text_content(&self) -> String {
self.reply.get_text_content()
}
pub fn is_updated(&self) -> bool {
self.reply.is_updated()
}
pub fn create_time(&self) -> i64 {
self.reply.create_time
}
pub fn update_time(&self) -> i64 {
self.reply.update_time
}
pub fn update_summary(&self) -> String {
format!(
"回复更新成功 - ID: {}, 新内容: \"{}\"",
self.reply_id(),
self.get_text_content()
)
}
}
#[cfg(test)]
#[allow(unused_variables, unused_unsafe)]
mod tests {
use super::*;
use crate::service::comments::create::ContentBuilder;
#[test]
fn test_update_reply_request_builder() {
let content = ContentBuilder::new().add_text("更新后的回复内容").build();
let request = UpdateReplyRequest::builder()
.file_token("doccnxxxxxx")
.with_doc_type()
.comment_id("comment123")
.reply_id("reply456")
.content(content)
.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.reply_id, "reply456");
assert_eq!(request.user_id_type, Some("open_id".to_string()));
}
}