Don't allow empty chat messages

Max Brunsfeld created

Change summary

server/src/rpc.rs  | 40 ++++++++++++++++++++++++++++++----------
zed/src/channel.rs |  4 ++++
2 files changed, 34 insertions(+), 10 deletions(-)

Detailed changes

server/src/rpc.rs 🔗

@@ -654,6 +654,7 @@ impl Server {
         self: Arc<Self>,
         request: TypedEnvelope<proto::SendChannelMessage>,
     ) -> 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::<Vec<_>>(),
             &["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 {

zed/src/channel.rs 🔗

@@ -229,6 +229,10 @@ impl Channel {
         body: String,
         cx: &mut ModelContext<Self>,
     ) -> Result<Task<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;