修复:历史消息服务端过滤,只加载与当前用户相关的记录,避免他人私聊和系统通知混入包厢窗

This commit is contained in:
2026-02-28 11:54:29 +08:00
parent 2219d7e26e
commit ffe35c048d

View File

@@ -121,9 +121,28 @@ class ChatController extends Controller
broadcast(new MessageSent($id, $welcomeMsg));
}
// 5. 获取历史消息用于初次渲染
// 获取最近的所有消息(由于 Redis list 已限制保留条数,传 0 为拉取所有缓存的记录)
$historyMessages = $this->chatState->getNewMessages($id, 0);
// 5. 获取历史消息并过滤,只保留和当前用户相关的消息用于初次渲染
// 规则公众发言to_user=大家 或空)保留;私聊/系统通知只保留涉及本人的
$allHistory = $this->chatState->getNewMessages($id, 0);
$username = $user->username;
$historyMessages = array_values(array_filter($allHistory, function ($msg) use ($username) {
$toUser = $msg['to_user'] ?? '';
$fromUser = $msg['from_user'] ?? '';
$isSecret = !empty($msg['is_secret']);
// 公众发言(对大家说):所有人都可以看到
if ($toUser === '大家' || $toUser === '') {
return true;
}
// 私信 / 悄悄话:只显示发给自己或自己发出的
if ($isSecret) {
return $fromUser === $username || $toUser === $username;
}
// 对特定人说话:只显示发给自己或自己发出的(含系统通知)
return $fromUser === $username || $toUser === $username;
}));
// 渲染主聊天框架视图
return view('chat.frame', [