Added sendable and recievable impls for servermessagein & out
This commit is contained in:
parent
23a93418d0
commit
5fbaaa78b1
|
@ -1,4 +1,4 @@
|
|||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
|
||||
pub struct TimeSensitiveAction {
|
||||
|
@ -11,7 +11,7 @@ pub enum MessageContentType {
|
|||
Text,
|
||||
Image,
|
||||
Audio,
|
||||
Video
|
||||
Video,
|
||||
}
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
|
||||
pub struct ChatMessage {
|
||||
|
@ -62,15 +62,27 @@ impl From<crate::domain::chat_message::ChatMessage> for ChatMessage {
|
|||
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::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(),
|
||||
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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
|
||||
|
@ -23,4 +22,4 @@ impl From<crate::domain::chat_room::ChatRoom> for ChatRoom {
|
|||
last_updated: value.last_updated.timestamp_millis(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
pub mod chat_message;
|
||||
pub mod chat_room;
|
||||
pub mod chat_room;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use chrono::{DateTime, Utc, TimeZone};
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{
|
||||
mysql::{MySqlTypeInfo, MySqlValueRef},
|
||||
|
@ -52,7 +52,9 @@ impl TimeSensitiveAction {
|
|||
}
|
||||
|
||||
/// Base message for chat rooms.
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, FromRow, Default)]
|
||||
#[derive(
|
||||
Serialize, Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, FromRow, Default,
|
||||
)]
|
||||
pub struct ChatMessage {
|
||||
pub id: u32,
|
||||
/// User id
|
||||
|
@ -177,14 +179,40 @@ impl From<crate::client_types::chat_message::ChatMessage> for ChatMessage {
|
|||
from_id: value.from_id,
|
||||
to_id: value.to_id,
|
||||
message: match value.message_content {
|
||||
crate::client_types::chat_message::MessageContentType::Text => ChatMessageContent::Text(String::from_utf8(value.message).unwrap()),
|
||||
crate::client_types::chat_message::MessageContentType::Image => ChatMessageContent::Image(value.message),
|
||||
crate::client_types::chat_message::MessageContentType::Video => ChatMessageContent::Video(value.message),
|
||||
crate::client_types::chat_message::MessageContentType::Audio => ChatMessageContent::Audio(value.message),
|
||||
crate::client_types::chat_message::MessageContentType::Text => {
|
||||
ChatMessageContent::Text(String::from_utf8(value.message).unwrap())
|
||||
}
|
||||
crate::client_types::chat_message::MessageContentType::Image => {
|
||||
ChatMessageContent::Image(value.message)
|
||||
}
|
||||
crate::client_types::chat_message::MessageContentType::Video => {
|
||||
ChatMessageContent::Video(value.message)
|
||||
}
|
||||
crate::client_types::chat_message::MessageContentType::Audio => {
|
||||
ChatMessageContent::Audio(value.message)
|
||||
}
|
||||
},
|
||||
time_sent: Utc.timestamp_millis_opt(value.time_sent).unwrap(),
|
||||
time_delivered: TimeSensitiveActionVec { list: value.time_delivered.into_iter().map(|time_delivered| TimeSensitiveAction { time: Utc.timestamp_millis_opt(time_delivered.time).unwrap(), by: time_delivered.by }).collect() },
|
||||
time_seen: TimeSensitiveActionVec { list: value.time_seen.into_iter().map(|time_seen| TimeSensitiveAction { time: Utc.timestamp_millis_opt(time_seen.time).unwrap(), by: time_seen.by }).collect() },
|
||||
time_delivered: TimeSensitiveActionVec {
|
||||
list: value
|
||||
.time_delivered
|
||||
.into_iter()
|
||||
.map(|time_delivered| TimeSensitiveAction {
|
||||
time: Utc.timestamp_millis_opt(time_delivered.time).unwrap(),
|
||||
by: time_delivered.by,
|
||||
})
|
||||
.collect(),
|
||||
},
|
||||
time_seen: TimeSensitiveActionVec {
|
||||
list: value
|
||||
.time_seen
|
||||
.into_iter()
|
||||
.map(|time_seen| TimeSensitiveAction {
|
||||
time: Utc.timestamp_millis_opt(time_seen.time).unwrap(),
|
||||
by: time_seen.by,
|
||||
})
|
||||
.collect(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,12 @@ pub struct ChatRoom {
|
|||
|
||||
impl ChatRoom {
|
||||
pub fn new(title: String, owner_id: u32) -> Self {
|
||||
Self { id: 0, title, owner_id, time_created: Utc::now(), last_updated: Utc::now() }
|
||||
Self {
|
||||
id: 0,
|
||||
title,
|
||||
owner_id,
|
||||
time_created: Utc::now(),
|
||||
last_updated: Utc::now(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
|
||||
pub struct ChatUser {
|
||||
pub chat_room_id: u32,
|
||||
pub user_id: u32,
|
||||
pub time_joined: DateTime<Utc>,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,4 +2,4 @@ pub mod chat_message;
|
|||
pub mod chat_message_update;
|
||||
pub mod chat_room;
|
||||
pub mod chat_user;
|
||||
pub mod error;
|
||||
pub mod error;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct ChatRoomParticipants {
|
||||
pub participants: Vec<u32>,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
|
||||
|
||||
/// This is what gets sent across a socket. No matter if it comes from the client or the
|
||||
/// Server. This is what gets put in Message::Text(HERE).
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
|
||||
|
@ -10,4 +8,4 @@ pub struct ClientMessage {
|
|||
pub head: String,
|
||||
#[serde(skip_serializing_if = "Value::is_null")]
|
||||
pub body: Value,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
pub mod chat;
|
||||
pub mod message;
|
||||
pub mod server_in;
|
||||
pub mod server_out;
|
||||
pub mod server_in;
|
|
@ -1,9 +1,13 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use crate::{domain::{chat_message::ChatMessageSender, error::SocketError}, dto::message::ClientMessage};
|
||||
use crate::{
|
||||
domain::{chat_message::ChatMessageSender, error::SocketError},
|
||||
dto::message::ClientMessage,
|
||||
};
|
||||
use dev_dtos::dtos::user::user_dtos::UserForAuthenticationDto;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::server_out::Sendable;
|
||||
|
||||
pub trait Receivable {
|
||||
fn from_message(
|
||||
|
@ -64,3 +68,39 @@ impl Receivable for ServerMessageIn {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Sendable for ServerMessageIn {
|
||||
fn into_message(self) -> Result<ClientMessage, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let head = self.to_string();
|
||||
match self {
|
||||
ServerMessageIn::Login(user) => Ok(ClientMessage {
|
||||
head,
|
||||
body: serde_json::to_value(user)?,
|
||||
}),
|
||||
ServerMessageIn::Logout => Ok(ClientMessage {
|
||||
head,
|
||||
body: serde_json::Value::Null,
|
||||
}),
|
||||
ServerMessageIn::SeeMessages(message_ids) => Ok(ClientMessage {
|
||||
head,
|
||||
body: serde_json::to_value(message_ids)?,
|
||||
}),
|
||||
ServerMessageIn::SendMessage(messages) => Ok(ClientMessage {
|
||||
head,
|
||||
body: serde_json::to_value(messages)?,
|
||||
}),
|
||||
ServerMessageIn::FetchMessages() => Ok(ClientMessage {
|
||||
head,
|
||||
body: serde_json::Value::Null,
|
||||
}),
|
||||
ServerMessageIn::JoinGroup() => Ok(ClientMessage {
|
||||
head,
|
||||
body: serde_json::Value::Null,
|
||||
}),
|
||||
ServerMessageIn::LeaveGroup() => Ok(ClientMessage {
|
||||
head,
|
||||
body: serde_json::Value::Null,
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,15 @@
|
|||
use crate::{domain::chat_message::{ChatMessage, TimeSensitiveAction}, dto::message::ClientMessage};
|
||||
use crate::{
|
||||
domain::{
|
||||
chat_message::{ChatMessage, TimeSensitiveAction},
|
||||
error::SocketError,
|
||||
},
|
||||
dto::message::ClientMessage,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::fmt::Display;
|
||||
|
||||
use super::server_in::Receivable;
|
||||
pub trait Sendable {
|
||||
fn into_message(self) -> Result<ClientMessage, Box<dyn std::error::Error + Send + Sync>>;
|
||||
}
|
||||
|
@ -81,3 +89,36 @@ impl Sendable for ServerMessageOut {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Receivable for ServerMessageOut {
|
||||
fn from_message(
|
||||
message: ClientMessage,
|
||||
) -> Result<Self, Box<dyn std::error::Error + Send + Sync>>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let head = message.head.as_str();
|
||||
match head {
|
||||
"ACKNOWLEDGE" => Ok(ServerMessageOut::Acknowledge),
|
||||
"MESSAGE SENT" => Ok(ServerMessageOut::MessageSent),
|
||||
"MESSAGE RECIEVED" => Ok(ServerMessageOut::MessageRecieved(serde_json::from_value(
|
||||
message.body,
|
||||
)?)),
|
||||
"MESSAGE DELIVERED" => Ok(ServerMessageOut::MessageDelivered(serde_json::from_value(
|
||||
message.body,
|
||||
)?)),
|
||||
"MESSAGE SEEN" => Ok(ServerMessageOut::MessageSeen(serde_json::from_value(
|
||||
message.body,
|
||||
)?)),
|
||||
"ERROR" => Ok(ServerMessageOut::Error(serde_json::from_value(
|
||||
message.body,
|
||||
)?)),
|
||||
_ => {
|
||||
return Err(SocketError::boxed_error(format!(
|
||||
"Error decoding recieved clientMessage: {:?}",
|
||||
message
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
pub mod client_types;
|
||||
pub mod domain;
|
||||
pub mod dto;
|
||||
pub mod client_types;
|
Loading…
Reference in New Issue