diff --git a/model/src/proto/c2s.rs b/model/src/proto/c2s.rs index f24f1bf..a7a3ee5 100644 --- a/model/src/proto/c2s.rs +++ b/model/src/proto/c2s.rs @@ -4,6 +4,7 @@ pub mod s { #[derive(Debug, serde::Serialize, serde::Deserialize)] pub enum Packet { Message(crate::messages::Model), + RequestChatHistory(uuid::Uuid) } } @@ -13,6 +14,7 @@ pub mod c { #[derive(Debug, serde::Serialize, serde::Deserialize)] pub enum Packet { Message(crate::messages::Model), + MessagesHistory(Vec), } } diff --git a/server/src/lib.rs b/server/src/lib.rs index 567f147..ad0531d 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -1,8 +1,8 @@ use std::sync::Arc; use futures_util::FutureExt; -use scct_model::{chats, proto::c2s, users}; -use sea_orm::{ActiveModelTrait, ConnectionTrait, DatabaseConnection, IntoActiveModel}; +use scct_model::{chats, messages, proto::c2s, users}; +use sea_orm::{ActiveModelTrait, ConnectionTrait, DatabaseConnection, EntityTrait, IntoActiveModel, ColumnTrait, QueryFilter, QueryOrder}; use tokio::sync::Mutex; use transport::{Transport, TransportSink, TransportStream}; use uuid; @@ -17,11 +17,17 @@ pub async fn serve(addr: &str, db_connection: DatabaseConnection) -> Result<(), tracing::info!("preparing C2S transport"); let mut listener = c2s.serve(addr.to_string()).await?; - let chat = chats::Model { - id: uuid::Uuid::nil(), // temporarily we have only one chat - name: String::from("global") - }; - chat.clone().into_active_model().insert(&db_connection).await.expect("failed to create global chat"); + // create the development chat if not exists + if let Ok(None) = chats::Entity::find_by_id(uuid::Uuid::nil()).one(&db_connection).await { + let chat = chats::Model { + id: uuid::Uuid::nil(), // temporarily we have only one chat + name: String::from("global") + }; + match chat.clone().into_active_model().insert(&db_connection).await { + Ok(_) => {}, + Err(_) => {}, + }; + } tracing::info!("listening for connections"); while let Some((tx, rx)) = listener.recv().await { @@ -39,7 +45,7 @@ pub async fn serve(addr: &str, db_connection: DatabaseConnection) -> Result<(), } /// Connection handler for handling a connecting client -async fn handle(active_client_peers: Arc>>, mut rx: impl TransportStream, db_connection: impl ConnectionTrait) { +async fn handle(active_client_peers: Arc>>, mut rx: impl TransportStream, db_connection: impl ConnectionTrait) { // for now we're creating a new user for each connection let new_client_id = uuid::Uuid::new_v4(); let user = users::Model{ @@ -82,6 +88,21 @@ async fn handle(active_client_peers: Arc>>, mut rx active_client_peers.lock().await.remove(i); } }, + c2s::s::Packet::RequestChatHistory(chat_uuid) => { + // send the user all messages in the history of this chat + match messages::Entity::find() + .filter(messages::Column::ChatId.eq(chat_uuid)) + .order_by_asc(messages::Column::Created) + .all(&db_connection) + .await { + Err(_) => { tracing::error!("failed to query all previously sent messages"); } + Ok(messages) => { + tracing::info!("responding to RequestChatHistory: {}", serde_json::to_string(&messages).unwrap()); + // how do i know which TransportSink is the sink of the handlee? (which tx is me?) + todo!(); + } + }; + }, }, } }