feat: add reply_to field to messages
This commit is contained in:
parent
b6df1e38ad
commit
f3760af1bb
6 changed files with 20 additions and 1 deletions
|
@ -0,0 +1,2 @@
|
|||
-- This file should undo anything in `up.sql`
|
||||
ALTER TABLE messages DROP COLUMN reply_to;
|
|
@ -0,0 +1,2 @@
|
|||
-- Your SQL goes here
|
||||
ALTER TABLE messages ADD COLUMN reply_to UUID REFERENCES messages(uuid) DEFAULT NULL;
|
|
@ -5,6 +5,7 @@ use actix_web::{
|
|||
};
|
||||
use actix_ws::AggregatedMessage;
|
||||
use futures_util::StreamExt as _;
|
||||
use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
|
@ -14,6 +15,12 @@ use crate::{
|
|||
utils::{get_ws_protocol_header, global_checks},
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct MessageBody {
|
||||
message: String,
|
||||
reply_to: Option<Uuid>,
|
||||
}
|
||||
|
||||
#[get("/{uuid}/socket")]
|
||||
pub async fn ws(
|
||||
req: HttpRequest,
|
||||
|
@ -74,7 +81,9 @@ pub async fn ws(
|
|||
Ok(AggregatedMessage::Text(text)) => {
|
||||
let mut conn = data.cache_pool.get_multiplexed_tokio_connection().await?;
|
||||
|
||||
let message = channel.new_message(&data, uuid, text.to_string()).await?;
|
||||
let message_body: MessageBody = serde_json::from_str(&text)?;
|
||||
|
||||
let message = channel.new_message(&data, uuid, message_body.message, message_body.reply_to).await?;
|
||||
|
||||
redis::cmd("PUBLISH")
|
||||
.arg(&[channel_uuid.to_string(), serde_json::to_string(&message)?])
|
||||
|
|
|
@ -270,6 +270,7 @@ impl Channel {
|
|||
data: &Data,
|
||||
user_uuid: Uuid,
|
||||
message: String,
|
||||
reply_to: Option<Uuid>,
|
||||
) -> Result<Message, Error> {
|
||||
let message_uuid = Uuid::now_v7();
|
||||
|
||||
|
@ -278,6 +279,7 @@ impl Channel {
|
|||
channel_uuid: self.uuid,
|
||||
user_uuid,
|
||||
message,
|
||||
reply_to,
|
||||
};
|
||||
|
||||
let mut conn = data.pool.get().await?;
|
||||
|
|
|
@ -14,6 +14,7 @@ pub struct MessageBuilder {
|
|||
pub channel_uuid: Uuid,
|
||||
pub user_uuid: Uuid,
|
||||
pub message: String,
|
||||
pub reply_to: Option<Uuid>,
|
||||
}
|
||||
|
||||
impl MessageBuilder {
|
||||
|
@ -25,6 +26,7 @@ impl MessageBuilder {
|
|||
channel_uuid: self.channel_uuid,
|
||||
user_uuid: self.user_uuid,
|
||||
message: self.message.clone(),
|
||||
reply_to: self.reply_to,
|
||||
user,
|
||||
})
|
||||
}
|
||||
|
@ -36,5 +38,6 @@ pub struct Message {
|
|||
channel_uuid: Uuid,
|
||||
user_uuid: Uuid,
|
||||
message: String,
|
||||
reply_to: Option<Uuid>,
|
||||
user: User,
|
||||
}
|
||||
|
|
|
@ -77,6 +77,7 @@ diesel::table! {
|
|||
user_uuid -> Uuid,
|
||||
#[max_length = 4000]
|
||||
message -> Varchar,
|
||||
reply_to -> Nullable<Uuid>,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue