Don't allow empty chat messages

This commit is contained in:
Max Brunsfeld 2021-08-27 17:21:53 -07:00
parent a98d293f54
commit 18d175a240
2 changed files with 34 additions and 10 deletions

View File

@ -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 {

View File

@ -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;