use crate::core::auth0::UserDetails;
#[derive(serde::Deserialize, serde::Serialize, Debug, validator::Validate)]
pub struct FeedsCommentFormData {
pub feed_id: String,
#[validate(length(max = 400))]
pub content: String,
pub reply_to: Option<String>,
pub reply_to_name: Option<String>,
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct FeedCommentSave {
pub user_id: String,
pub nick_name: String,
pub feed_id: String,
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub reply_to: Option<String>,
pub reply_to_name: Option<String>,
pub created_time: Option<i64>,
}
#[derive(serde::Deserialize, serde::Serialize, Debug, async_graphql::SimpleObject)]
pub struct FeedCommentVo {
pub id: String,
pub user_id: String,
pub nick_name: String,
pub feed_id: String,
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub reply_to: Option<String>,
pub reply_to_name: Option<String>,
pub reply_you: Option<String>,
}
#[derive(serde::Deserialize, serde::Serialize, Debug, async_graphql::SimpleObject)]
pub struct UserFeedsCommentsCountVo {
pub count: i32,
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct FeedCommentRecord {
#[serde(rename = "_id")]
pub id: mongodb::bson::oid::ObjectId,
pub user_id: String,
pub nick_name: String,
pub feed_id: String,
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub reply_to: Option<String>,
pub reply_to_name: Option<String>,
pub created_time: Option<i64>,
}
impl FeedCommentVo {
pub fn from(data: &FeedCommentRecord, curr_user_id: Option<&String>) -> Self {
let nick_name = match curr_user_id {
Some(user_id) => {
if user_id == &data.user_id {
"你".to_string()
} else {
data.nick_name.to_owned()
}
}
None => data.nick_name.to_owned(),
};
let reply_to_name = if let Some(reply_to_name) = &data.reply_to_name {
if reply_to_name.trim() == "" {
None
} else {
Some(reply_to_name.trim().to_string())
}
} else {
None
};
let (reply_to, reply_you) = if let Some(reply_to) = &data.reply_to {
if reply_to.trim() == "" {
(None, None)
} else {
match curr_user_id {
Some(user_id) => {
if user_id == reply_to.trim() {
(Some(reply_to.trim().to_string()), Some("你".to_string()))
} else {
(Some(reply_to.trim().to_string()), reply_to_name.clone())
}
}
None => (Some(reply_to.trim().to_string()), reply_to_name.clone()),
}
}
} else {
(None, None)
};
Self {
id: data.id.to_string(),
user_id: data.user_id.to_owned(),
nick_name,
feed_id: data.feed_id.to_owned(),
content: get_content(&data.content),
reply_to: reply_to.clone(),
reply_you,
reply_to_name: match reply_to {
Some(_) => reply_to_name,
None => None,
},
}
}
}
impl FeedCommentSave {
pub fn from(data: &FeedsCommentFormData, curr_user: &UserDetails) -> Self {
Self {
user_id: curr_user.user_id.to_owned(),
nick_name: curr_user.nick_name.to_owned(),
feed_id: data.feed_id.to_owned(),
content: get_content(&data.content),
reply_to: if let Some(reply_to) = &data.reply_to {
if reply_to.trim() == "" {
None
} else {
Some(reply_to.trim().to_string())
}
} else {
None
},
reply_to_name: if let Some(reply_to_name) = &data.reply_to_name {
if reply_to_name.trim() == "" {
None
} else {
Some(reply_to_name.trim().to_string())
}
} else {
None
},
created_time: Some(crate::commons::timestamp_millis()),
}
}
}
fn get_content(content: &str) -> String {
crate::commons::html_escape(content)
}