Added chatmessage type for client

This commit is contained in:
Franklin 2023-02-09 15:35:15 -04:00
parent 1ee28e57d4
commit bbbbec7b85
4 changed files with 80 additions and 1 deletions

View File

@ -0,0 +1,76 @@
use serde::{Serialize, Deserialize};
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct TimeSensitiveAction {
pub by: u32,
pub time: i64,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum MessageContentType {
#[default]
Text,
Image,
Audio,
Video
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
pub struct ChatMessage {
pub id: u32,
/// User id
#[serde(rename = "fromId")]
pub from_id: u32,
/// ChatRoom id (Not a user id)
#[serde(rename = "toId")]
pub to_id: u32,
pub message: Vec<u8>,
pub message_content: MessageContentType,
/// This must always be there. Since its created.
#[serde(rename = "timeSent")]
pub time_sent: i64,
/// This is a Vec because there can be many recipients.
#[serde(rename = "timeDelivered")]
pub time_delivered: Vec<TimeSensitiveAction>,
/// This is a Vec because there can be many recipients.
#[serde(rename = "timeSeen")]
pub time_seen: Vec<TimeSensitiveAction>,
}
impl From<crate::domain::chat_message::TimeSensitiveAction> for TimeSensitiveAction {
fn from(value: crate::domain::chat_message::TimeSensitiveAction) -> Self {
Self {
by: value.by,
time: value.time.timestamp_millis(),
}
}
}
impl From<crate::domain::chat_message::ChatMessageContent> for MessageContentType {
fn from(value: crate::domain::chat_message::ChatMessageContent) -> Self {
match value {
crate::domain::chat_message::ChatMessageContent::Text(_) => MessageContentType::Text,
crate::domain::chat_message::ChatMessageContent::Image(_) => MessageContentType::Image,
crate::domain::chat_message::ChatMessageContent::Video(_) => MessageContentType::Video,
crate::domain::chat_message::ChatMessageContent::Audio(_) => MessageContentType::Audio,
}
}
}
impl From<crate::domain::chat_message::ChatMessage> for ChatMessage {
fn from(value: crate::domain::chat_message::ChatMessage) -> Self {
Self {
id: value.id,
from_id: value.from_id,
to_id: value.to_id,
message: match value.message.clone() {
crate::domain::chat_message::ChatMessageContent::Text(text) => text.as_bytes().to_vec(),
crate::domain::chat_message::ChatMessageContent::Image(bytes) => bytes,
crate::domain::chat_message::ChatMessageContent::Video(bytes) => bytes,
crate::domain::chat_message::ChatMessageContent::Audio(bytes) => bytes,
},
message_content: value.message.into(),
time_sent: value.time_sent.timestamp_millis(),
time_delivered: value.time_delivered.list.into_iter().map(|time_delivered| time_delivered.into()).collect(),
time_seen: value.time_seen.list.into_iter().map(|time_seen| time_seen.into()).collect(),
}
}
}

View File

2
src/client_types/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod chat_message;
pub mod chat_room;

View File

@ -1,2 +1,3 @@
pub mod domain;
pub mod dto;
pub mod dto;
pub mod client_types;