mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-30 17:26:28 +00:00
42 lines
1.2 KiB
Rust
42 lines
1.2 KiB
Rust
//! # 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()
|
|
}
|
|
} |