1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-27 15:59:21 +00:00
comunicapiv3/src/api_data/conversation_message_api.rs

42 lines
1.2 KiB
Rust
Raw Normal View History

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,
ID_user: msg.user_id as u64,
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()
}
}