//! # Conversation message API //! //! API representation of a conversation message //! //! @author Pierre HUBERT use serde::Serialize; use crate::data::conversation_message::ConversationMessage; use crate::utils::user_data_utils::user_data_url; #[derive(Serialize)] #[allow(non_snake_case)] pub struct ConversationMessageAPI { ID: u64, convID: u64, ID_user: u64, time_insert: u64, message: String, image_path: Option, } impl ConversationMessageAPI { /// Turn a conversation message into an API entry pub fn new(msg: &ConversationMessage) -> ConversationMessageAPI { ConversationMessageAPI { ID: msg.id, convID: msg.conv_id, ID_user: msg.user_id.id(), time_insert: msg.time_sent, message: msg.message.clone().unwrap_or(String::new()), image_path: msg.image_path.as_ref().map(|f| user_data_url(f)), } } /// Turn a list of conversation messages into API entries pub fn for_list(l: &Vec) -> Vec { l.iter() .map(|m| ConversationMessageAPI::new(m)) .collect() } }