2021-04-09 00:22:17 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use postgres_types::FromSql;
|
|
|
|
use tokio_postgres::Row;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::errors::ValidationError;
|
|
|
|
use crate::models::attachments::types::DbMediaAttachment;
|
|
|
|
use crate::models::profiles::types::DbActorProfile;
|
|
|
|
use crate::utils::html::clean_html;
|
|
|
|
|
|
|
|
#[derive(FromSql)]
|
|
|
|
#[postgres(name = "post")]
|
|
|
|
pub struct DbPost {
|
|
|
|
pub id: Uuid,
|
|
|
|
pub author_id: Uuid,
|
|
|
|
pub content: String,
|
2021-09-22 16:32:44 +00:00
|
|
|
pub in_reply_to_id: Option<Uuid>,
|
2021-09-24 14:02:56 +00:00
|
|
|
pub reply_count: i32,
|
2021-10-08 22:39:40 +00:00
|
|
|
pub object_id: Option<String>,
|
2021-04-09 00:22:17 +00:00
|
|
|
pub ipfs_cid: Option<String>,
|
|
|
|
pub token_id: Option<i32>,
|
|
|
|
pub token_tx_id: Option<String>,
|
|
|
|
pub created_at: DateTime<Utc>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Post {
|
|
|
|
pub id: Uuid,
|
|
|
|
pub author: DbActorProfile,
|
|
|
|
pub content: String,
|
2021-09-22 16:32:44 +00:00
|
|
|
pub in_reply_to_id: Option<Uuid>,
|
2021-09-24 14:02:56 +00:00
|
|
|
pub reply_count: i32,
|
2021-04-09 00:22:17 +00:00
|
|
|
pub attachments: Vec<DbMediaAttachment>,
|
2021-10-08 22:39:40 +00:00
|
|
|
pub object_id: Option<String>,
|
2021-04-09 00:22:17 +00:00
|
|
|
pub ipfs_cid: Option<String>,
|
|
|
|
pub token_id: Option<i32>,
|
|
|
|
pub token_tx_id: Option<String>,
|
|
|
|
pub created_at: DateTime<Utc>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&Row> for Post {
|
|
|
|
|
|
|
|
type Error = tokio_postgres::Error;
|
|
|
|
|
|
|
|
fn try_from(row: &Row) -> Result<Self, Self::Error> {
|
|
|
|
let db_post: DbPost = row.try_get("post")?;
|
|
|
|
let db_profile: DbActorProfile = row.try_get("actor_profile")?;
|
|
|
|
let db_attachments: Vec<DbMediaAttachment> = row.try_get("attachments")?;
|
|
|
|
let post = Post {
|
|
|
|
id: db_post.id,
|
|
|
|
author: db_profile,
|
|
|
|
content: db_post.content,
|
2021-09-22 16:32:44 +00:00
|
|
|
in_reply_to_id: db_post.in_reply_to_id,
|
2021-09-24 14:02:56 +00:00
|
|
|
reply_count: db_post.reply_count,
|
2021-04-09 00:22:17 +00:00
|
|
|
attachments: db_attachments,
|
2021-10-08 22:39:40 +00:00
|
|
|
object_id: db_post.object_id,
|
2021-04-09 00:22:17 +00:00
|
|
|
ipfs_cid: db_post.ipfs_cid,
|
|
|
|
token_id: db_post.token_id,
|
|
|
|
token_tx_id: db_post.token_tx_id,
|
|
|
|
created_at: db_post.created_at,
|
|
|
|
};
|
|
|
|
Ok(post)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PostCreateData {
|
|
|
|
pub content: String,
|
2021-09-22 16:32:44 +00:00
|
|
|
pub in_reply_to_id: Option<Uuid>,
|
2021-04-09 00:22:17 +00:00
|
|
|
pub attachments: Vec<Uuid>,
|
2021-10-08 22:39:40 +00:00
|
|
|
pub object_id: Option<String>,
|
2021-04-09 00:22:17 +00:00
|
|
|
pub created_at: Option<DateTime<Utc>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PostCreateData {
|
|
|
|
/// Validate and clean post data.
|
|
|
|
pub fn validate(&mut self) -> Result<(), ValidationError> {
|
|
|
|
let content_safe = clean_html(&self.content);
|
|
|
|
let content_trimmed = content_safe.trim();
|
|
|
|
if content_trimmed == "" {
|
|
|
|
return Err(ValidationError("post can not be empty"));
|
|
|
|
}
|
|
|
|
self.content = content_trimmed.to_string();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_validate_post_data() {
|
|
|
|
let mut post_data_1 = PostCreateData {
|
|
|
|
content: " ".to_string(),
|
2021-09-22 16:32:44 +00:00
|
|
|
in_reply_to_id: None,
|
2021-04-09 00:22:17 +00:00
|
|
|
attachments: vec![],
|
2021-10-08 22:39:40 +00:00
|
|
|
object_id: None,
|
2021-04-09 00:22:17 +00:00
|
|
|
created_at: None,
|
|
|
|
};
|
|
|
|
assert_eq!(post_data_1.validate().is_ok(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_trimming() {
|
|
|
|
let mut post_data_2 = PostCreateData {
|
|
|
|
content: "test ".to_string(),
|
2021-09-22 16:32:44 +00:00
|
|
|
in_reply_to_id: None,
|
2021-04-09 00:22:17 +00:00
|
|
|
attachments: vec![],
|
2021-10-08 22:39:40 +00:00
|
|
|
object_id: None,
|
2021-04-09 00:22:17 +00:00
|
|
|
created_at: None,
|
|
|
|
};
|
|
|
|
assert_eq!(post_data_2.validate().is_ok(), true);
|
|
|
|
assert_eq!(post_data_2.content.as_str(), "test");
|
|
|
|
}
|
|
|
|
}
|