diff --git a/app/Http/Controllers/ChatController.php b/app/Http/Controllers/ChatController.php
index 0c437fd..8e58efb 100644
--- a/app/Http/Controllers/ChatController.php
+++ b/app/Http/Controllers/ChatController.php
@@ -362,7 +362,7 @@ class ChatController extends Controller
// 1. 过滤净化消息体
$pureContent = $this->filter->filter($data['content'] ?? '');
- if (empty($pureContent)) {
+ if ($pureContent === '') {
return response()->json(['status' => 'error', 'message' => '消息内容不能为空或不合法。'], 422);
}
diff --git a/app/Services/MessageFilterService.php b/app/Services/MessageFilterService.php
index 153e2dd..959be17 100644
--- a/app/Services/MessageFilterService.php
+++ b/app/Services/MessageFilterService.php
@@ -28,7 +28,7 @@ class MessageFilterService
*/
public function filter(string $content): string
{
- if (empty($content)) {
+ if ($content === '') {
return '';
}
diff --git a/resources/views/chat/partials/scripts.blade.php b/resources/views/chat/partials/scripts.blade.php
index c923d15..58ce407 100644
--- a/resources/views/chat/partials/scripts.blade.php
+++ b/resources/views/chat/partials/scripts.blade.php
@@ -725,7 +725,8 @@
const verbStr = msg.action ?
buildActionStr(msg.action, fromHtml, toHtml, '悄悄说') :
`${fromHtml}对${toHtml}悄悄说:`;
- html = `${headImg}${verbStr}${msg.content}`;
+ html =
+ `${headImg}${verbStr}${msg.content}`;
}
} else if (msg.to_user && msg.to_user !== '大家') {
// 对特定对象说话
diff --git a/tests/Feature/ChatControllerTest.php b/tests/Feature/ChatControllerTest.php
index 9a09e46..8d3e3c2 100644
--- a/tests/Feature/ChatControllerTest.php
+++ b/tests/Feature/ChatControllerTest.php
@@ -67,6 +67,39 @@ class ChatControllerTest extends TestCase
$this->assertTrue($found, 'Message not found in Redis');
}
+ public function test_can_send_zero_message_content(): void
+ {
+ $room = Room::create(['room_name' => 'test_send_zero']);
+ $user = User::factory()->create();
+
+ $this->actingAs($user)->get(route('chat.room', $room->id));
+
+ $response = $this->actingAs($user)->postJson(route('chat.send', $room->id), [
+ 'to_user' => '大家',
+ 'content' => '0',
+ 'is_secret' => false,
+ 'font_color' => '#000000',
+ 'action' => '',
+ ]);
+
+ $response->assertOk();
+ $response->assertJson(['status' => 'success']);
+
+ $messages = Redis::lrange("room:{$room->id}:messages", 0, -1);
+ $found = false;
+
+ foreach ($messages as $msgJson) {
+ $msg = json_decode($msgJson, true);
+
+ if ($msg['from_user'] === $user->username && $msg['content'] === '0') {
+ $found = true;
+ break;
+ }
+ }
+
+ $this->assertTrue($found, 'Zero message not found in Redis');
+ }
+
public function test_can_trigger_heartbeat()
{
$room = Room::create(['room_name' => 'test_hb']);