From 18d175a2401b3cc7199468213f0edb08098f424b Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 27 Aug 2021 17:21:53 -0700 Subject: [PATCH] Don't allow empty chat messages --- server/src/rpc.rs | 40 ++++++++++++++++++++++++++++++---------- zed/src/channel.rs | 4 ++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/server/src/rpc.rs b/server/src/rpc.rs index 696487fdca..5a0199b725 100644 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -654,6 +654,7 @@ impl Server { self: Arc, request: TypedEnvelope, ) -> tide::Result<()> { + let receipt = request.receipt(); let channel_id = ChannelId::from_proto(request.payload.channel_id); let user_id; let connection_ids; @@ -667,7 +668,7 @@ impl Server { } } - let receipt = request.receipt(); + // Validate the message body. let body = request.payload.body.trim().to_string(); if body.len() > MAX_MESSAGE_LEN { self.peer @@ -680,6 +681,17 @@ impl Server { .await?; return Ok(()); } + if body.is_empty() { + self.peer + .respond_with_error( + receipt, + proto::Error { + message: "message can't be blank".to_string(), + }, + ) + .await?; + return Ok(()); + } let timestamp = OffsetDateTime::now_utc(); let message_id = self @@ -1632,6 +1644,23 @@ mod tests { this.get_channel(channel_id.to_proto(), cx).unwrap() }); + // Messages aren't allowed to be too long. + channel_a + .update(&mut cx_a, |channel, cx| { + let long_body = "this is long.\n".repeat(1024); + channel.send_message(long_body, cx).unwrap() + }) + .await + .unwrap_err(); + + // Messages aren't allowed to be blank. + channel_a + .update(&mut cx_a, |channel, cx| { + channel.send_message(String::new(), cx).unwrap() + }) + .await + .unwrap_err(); + // Leading and trailing whitespace are trimmed. channel_a .update(&mut cx_a, |channel, cx| { @@ -1650,15 +1679,6 @@ mod tests { .collect::>(), &["surrounded by whitespace"] ); - - // Messages aren't allowed to be too long. - channel_a - .update(&mut cx_a, |channel, cx| { - let long_body = "this is long.\n".repeat(1024); - channel.send_message(long_body, cx).unwrap() - }) - .await - .unwrap_err(); } struct TestServer { diff --git a/zed/src/channel.rs b/zed/src/channel.rs index 6bac81fa19..858bbb5ce1 100644 --- a/zed/src/channel.rs +++ b/zed/src/channel.rs @@ -229,6 +229,10 @@ impl Channel { body: String, cx: &mut ModelContext, ) -> Result>> { + if body.is_empty() { + Err(anyhow!("message body can't be empty"))?; + } + let channel_id = self.details.id; let current_user_id = self.current_user_id()?; let local_id = self.next_local_message_id;