comment_app_frontend 0.1.3

A Comment App Front End Server
Documentation
// comment.rs
// comment functions are implemented here

use serde_derive::{Serialize, Deserialize};

use crate::models::reply::Reply;
use crate::models::user::User;
use crate::models::status::Status;


#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Comment {
    // attributes from server are:
    pub message: String,

    #[serde(default = "default_anonym")]
    pub anonymous: bool,

    #[serde(default = "String::new")]
    pub unique_id: String,

    #[serde(default = "String::new")]
    pub user_id: String,

    #[serde(default = "default_status")]
    pub status: Status,

    #[serde(default = "String::new")]
    pub created_on: String,

    // derived attributes (for UI purposes) are:
    #[serde(default = "default_user")]
    pub user: User,

    #[serde(default = "default_session_user")]
    pub session_user: User,

    #[serde(default ="Vec::new")]
    pub replies: Vec<Reply>,
     
    #[serde(default = "default_enable")]
    pub enabled: bool,  // is the comments enabled to add replies
    
    #[serde(default = "default_visibility")]
    pub visible: bool,  // is the button visible? It is visible to original author of the comment or admin

    #[serde(default = "default_editability")]
    pub editable: bool, // is the comment editable (by the commenter or original author or creator)

    #[serde(default = "default_trashability")]
    pub trashable: bool, // is the comment trashable (by the commenter or original author or creator)

    #[serde(default = "String::new")]
    pub more_message: String, // remaining message text

    #[serde(default = "default_has_more_message")]
    pub has_more_message: bool, // enable to show 'more' on the web page

    #[serde(default = "default_replies_count")]
    pub replies_count: usize, // initially show replies count only

    #[serde(default = "default_has_replies")]
    pub has_replies: bool, // enable to show replies on the web page

    #[serde(default = "default_upvotes_count")]
    pub upvotes_count: usize, 

    #[serde(default = "default_downvotes_count")]
    pub downvotes_count: usize, 

    #[serde(default = "String::new")]
    pub time_ago: String, 

    #[serde(default = "default_show_dropdown")]
    pub show_dropdown: bool, // enable to show replies on the web page

}

fn default_anonym() -> bool {
    false
}
fn default_user() -> User {
    User::default()
}
fn default_session_user() -> User {
    User::default()
}
fn default_status() -> Status {
    Status::Pending
}
fn default_enable() -> bool {
    true
}
fn default_visibility() -> bool {
    false
}
fn default_editability() -> bool {
    false
}
fn default_trashability() -> bool {
    false
}
fn default_has_more_message() -> bool {
    false
}
fn default_replies_count() -> usize {
    0usize
}
fn default_has_replies() -> bool {
    false
}
fn default_upvotes_count() -> usize {
    0usize
}
fn default_downvotes_count() -> usize {
    0usize
}
fn default_show_dropdown() -> bool {
    false
}

impl Comment {
    pub fn from(message: &str, is_anonymous: bool, user_id: &str) -> Comment {
        Comment {
            message: message.to_string(),
            anonymous: is_anonymous,
            unique_id: String::new(),
            user_id: user_id.to_string(),
            status: Status::Pending,
            created_on: String::new(),
            user: User::default(), // owner of the comment, user details are retrieved based on user_id field above; this user is used inside html template     
            session_user: User::default(), // user of the comment, may add reply to this comment
            replies: Vec::new(),
            enabled: true,  // if true, replies can be posted on this comment
            visible: false, // if false, comment's enable/disable checkbox won't be visible
            editable: false, // if false, current session user cannot edit the message on the comment
            trashable: false, // if false, current session user cannot trash a comment
            more_message: String::new(), // remaining message text
            has_more_message: false, // if false, more_message field is empty
            replies_count: 0, // default value is 0
            has_replies: false,
            upvotes_count: 0, // default upvote count is 0 
            downvotes_count: 0, // default downvote count is 0 
            time_ago: String::new(), // Difference in time between Local::now() and created_on
            show_dropdown: false,
        }
    }
}