parent
f153625d1a
commit
6419b6357c
@ -1,54 +1,31 @@
|
|||||||
use chrono::NaiveDateTime;
|
use chrono::serde::ts_seconds_option;
|
||||||
use serde::{Deserialize, Serialize};
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde::{Deserialize, Serialize, Serializer};
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
|
||||||
// Master comment type that is stored in database
|
// Master comment type that is stored in database
|
||||||
|
#[derive(Serialize, Deserialize, Validate)]
|
||||||
pub struct Comment {
|
pub struct Comment {
|
||||||
pub author: Option<String>, // None/null is Anonymous
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub email: Option<String>,
|
pub author: Option<String>, // None is Anonymous
|
||||||
pub text: String,
|
#[serde(rename(serialize = "gravatar"))]
|
||||||
pub timestamp: Option<NaiveDateTime>,
|
#[serde(serialize_with = "serialize_gravatar")]
|
||||||
}
|
|
||||||
|
|
||||||
impl Comment {
|
|
||||||
pub fn send(&self) -> CommentSend {
|
|
||||||
CommentSend {
|
|
||||||
author: self.author.clone(),
|
|
||||||
gravatar: match self.email.clone() {
|
|
||||||
Some(email) => Some(format!("{:x}", md5::compute(email.to_lowercase()))),
|
|
||||||
None => None,
|
|
||||||
},
|
|
||||||
text: self.text.clone(),
|
|
||||||
timestamp: self.timestamp.unwrap().timestamp(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Comment type for API responses
|
|
||||||
#[derive(Serialize)]
|
|
||||||
pub struct CommentSend {
|
|
||||||
pub author: Option<String>,
|
|
||||||
pub gravatar: Option<String>,
|
|
||||||
pub text: String,
|
|
||||||
pub timestamp: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Comment type received containing new comment data
|
|
||||||
#[derive(Deserialize, Validate)]
|
|
||||||
pub struct CommentReceive {
|
|
||||||
pub author: Option<String>,
|
|
||||||
#[validate(email)]
|
#[validate(email)]
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub email: Option<String>,
|
pub email: Option<String>,
|
||||||
|
#[validate(length(min = 1))]
|
||||||
pub text: String,
|
pub text: String,
|
||||||
|
#[serde(default)]
|
||||||
|
#[serde(with = "ts_seconds_option")]
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub timestamp: Option<DateTime<Utc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommentReceive {
|
fn serialize_gravatar<S>(email: &Option<String>, s: S) -> Result<S::Ok, S::Error>
|
||||||
pub fn to_master(&self) -> Comment {
|
where S: Serializer,
|
||||||
Comment {
|
{
|
||||||
author: self.author.clone(),
|
match email {
|
||||||
email: self.email.clone(),
|
Some(email) => s.serialize_some(&format!("{:x}", md5::compute(email.to_lowercase()))),
|
||||||
text: self.text.clone(),
|
None => s.serialize_none(),
|
||||||
timestamp: None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue