use chrono::{DateTime, Utc};
use crate::core::auth0::UserDetails;
use crate::core::serde::date_format_opt;
#[derive(serde::Deserialize, serde::Serialize, Debug, validator::Validate)]
pub struct FeedsCreateFormData {
#[validate(length(max = 400))]
pub content: String,
}
impl FeedsCreateFormData {
fn get(&self) -> Self {
Self {
content: crate::commons::html_escape(&self.content),
}
}
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct UserFeeds {
#[serde(rename = "_id")]
pub id: Option<mongodb::bson::oid::ObjectId>,
pub user_id: String,
pub resource_id: String,
pub user_name: String,
pub nick_name: String,
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default = "date_format_opt::default_time", with = "date_format_opt")]
pub created_at: Option<DateTime<Utc>>,
pub created_time: i64,
}
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct UserFeedsSave {
pub user_id: String,
pub resource_id: String,
pub user_name: String,
pub nick_name: String,
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default = "date_format_opt::default_time", with = "date_format_opt")]
pub created_at: Option<DateTime<Utc>>,
pub created_time: Option<i64>,
}
#[derive(serde::Deserialize, serde::Serialize, Debug, async_graphql::SimpleObject)]
pub struct UserFeedsVo {
pub id: String,
pub user_name: String,
pub nick_name: String,
pub head_image: String,
pub content: String,
pub time_line: String,
pub created_time: i64,
}
#[derive(serde::Deserialize, serde::Serialize, Debug, async_graphql::SimpleObject)]
pub struct UserFeedsThumbUpCountVo {
pub count: i32,
}
#[derive(async_graphql::InputObject, serde::Serialize, serde::Deserialize)]
pub struct FetchFeeds {
#[graphql(validator(minimum = 0))]
pub skip: Option<u32>,
#[graphql(validator(minimum = 0, maximum = 100))]
pub limit: Option<u32>,
}
impl UserFeedsSave {
pub fn from(data: &FeedsCreateFormData, curr_user: &UserDetails) -> Self {
Self {
user_id: curr_user.user_id.to_owned(),
resource_id: curr_user.resource_id.to_owned(),
user_name: curr_user.user_name.to_owned(),
nick_name: curr_user.nick_name.to_owned(),
content: data.get().content.to_owned(),
created_at: Some(crate::commons::now()),
created_time: Some(crate::commons::timestamp_millis()),
}
}
}
impl UserFeedsVo {
pub fn from(record: &UserFeeds) -> Self {
Self {
id: match record.id {
Some(id) => id.to_string(),
None => {
log::warn!("UserFeeds={:?}", record);
"".to_string()
}
},
user_name: record.user_name.to_owned(),
nick_name: record.nick_name.to_owned(),
content: record.content.to_owned(),
time_line: crate::commons::timestamp_text_line(record.created_time),
created_time: record.created_time,
head_image: format!("/m/pp/{}", record.resource_id),
}
}
}