2020-06-18 12:15:17 +00:00
|
|
|
//! # 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<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConversationMessageAPI {
|
|
|
|
/// Turn a conversation message into an API entry
|
|
|
|
pub fn new(msg: &ConversationMessage) -> ConversationMessageAPI {
|
|
|
|
ConversationMessageAPI {
|
|
|
|
ID: msg.id,
|
|
|
|
convID: msg.conv_id,
|
2020-06-25 08:08:34 +00:00
|
|
|
ID_user: msg.user_id.id(),
|
2020-06-18 12:15:17 +00:00
|
|
|
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<ConversationMessage>) -> Vec<ConversationMessageAPI> {
|
|
|
|
l.iter()
|
|
|
|
.map(|m| ConversationMessageAPI::new(m))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|