feat: add message storing in DB
UNTESTED! Should work but might be really slow
This commit is contained in:
parent
95964e6fec
commit
c5d14ac063
2 changed files with 22 additions and 0 deletions
|
@ -97,6 +97,7 @@ pub async fn echo(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, stream: web::
|
|||
Ok(AggregatedMessage::Text(text)) => {
|
||||
// echo text message
|
||||
redis::cmd("PUBLISH").arg(&[channel_uuid.to_string(), text.to_string()]).exec_async(&mut conn).await.unwrap();
|
||||
channel.new_message(&data.pool, uuid, text.to_string()).await.unwrap();
|
||||
}
|
||||
|
||||
Ok(AggregatedMessage::Binary(bin)) => {
|
||||
|
|
|
@ -186,6 +186,27 @@ impl Channel {
|
|||
|
||||
Ok(message_builders.iter().map(|b| b.build()).collect())
|
||||
}
|
||||
|
||||
pub async fn new_message(&self, pool: &Pool<Postgres>, user_uuid: Uuid, message: String) -> Result<Message, HttpResponse> {
|
||||
let message_uuid = Uuid::now_v7();
|
||||
|
||||
let row = sqlx::query(&format!("INSERT INTO messages (uuid, channel_uuid, user_uuid, message) VALUES ('{}', '{}', '{}', $1", message_uuid, self.uuid, user_uuid))
|
||||
.bind(&message)
|
||||
.execute(pool)
|
||||
.await;
|
||||
|
||||
if let Err(error) = row {
|
||||
error!("{}", error);
|
||||
return Err(HttpResponse::InternalServerError().finish());
|
||||
}
|
||||
|
||||
Ok(Message {
|
||||
uuid: message_uuid,
|
||||
channel_uuid: self.uuid,
|
||||
user_uuid,
|
||||
message,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue