sending chat history on request work in progress

This commit is contained in:
cqql 2024-08-12 20:50:45 +02:00
parent 495b409adf
commit d2985c3be8
2 changed files with 31 additions and 8 deletions

View file

@ -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<crate::messages::Model>),
}
}

View file

@ -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<Mutex<Vec<impl TransportSink>>>, mut rx: impl TransportStream, db_connection: impl ConnectionTrait) {
async fn handle(active_client_peers: Arc<Mutex<Vec<impl TransportSink>>>, 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<Mutex<Vec<impl TransportSink>>>, 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!();
}
};
},
},
}
}