2026-03-01 00:48:51 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 文件功能:好友删除广播事件
|
|
|
|
|
|
*
|
2026-03-01 01:41:04 +08:00
|
|
|
|
* 当用户 A 删除用户 B 为好友时,向 B 的私有频道广播此事件。
|
|
|
|
|
|
* 频道名使用数字 ID(user.{id}),避免中文用户名导致 Pusher 频道名验证失败。
|
2026-03-01 00:48:51 +08:00
|
|
|
|
*
|
|
|
|
|
|
* @author ChatRoom Laravel
|
|
|
|
|
|
*
|
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Events;
|
|
|
|
|
|
|
|
|
|
|
|
use Illuminate\Broadcasting\Channel;
|
|
|
|
|
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
|
|
|
|
|
use Illuminate\Broadcasting\PrivateChannel;
|
2026-03-01 01:21:33 +08:00
|
|
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
2026-03-01 00:48:51 +08:00
|
|
|
|
use Illuminate\Foundation\Events\Dispatchable;
|
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
|
2026-03-01 01:21:33 +08:00
|
|
|
|
class FriendRemoved implements ShouldBroadcastNow
|
2026-03-01 00:48:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 构造好友删除事件。
|
|
|
|
|
|
*
|
2026-03-01 00:54:10 +08:00
|
|
|
|
* @param string $fromUsername 发起删除的用户名(A)
|
2026-03-01 01:41:04 +08:00
|
|
|
|
* @param string $toUsername 被删除的用户名(B,用于消息显示)
|
|
|
|
|
|
* @param int $toUserId 被删除用户的数字 ID(用于私有频道,避免中文名非法)
|
2026-03-01 00:54:10 +08:00
|
|
|
|
* @param bool $hadAddedBack B 之前是否也将 A 加为好友(互相好友=true)
|
2026-03-01 00:48:51 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
|
public readonly string $fromUsername,
|
|
|
|
|
|
public readonly string $toUsername,
|
2026-03-01 01:41:04 +08:00
|
|
|
|
public readonly int $toUserId,
|
2026-03-01 00:54:10 +08:00
|
|
|
|
public readonly bool $hadAddedBack = false,
|
2026-03-01 00:48:51 +08:00
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-01 01:41:04 +08:00
|
|
|
|
* 广播到被删除用户的私有频道(用数字 ID 命名,避免中文频道名不合法)。
|
2026-03-01 00:48:51 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function broadcastOn(): Channel
|
|
|
|
|
|
{
|
2026-03-01 01:41:04 +08:00
|
|
|
|
return new PrivateChannel('user.'.$this->toUserId);
|
2026-03-01 00:48:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-01 01:13:50 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 指定广播事件名称(短名),供前端 listen('.FriendRemoved') 匹配。
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function broadcastAs(): string
|
|
|
|
|
|
{
|
|
|
|
|
|
return 'FriendRemoved';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-01 00:48:51 +08:00
|
|
|
|
/**
|
2026-03-01 00:54:10 +08:00
|
|
|
|
* 广播负载:包含发起人信息和之前互相好友状态,供前端弹窗使用。
|
2026-03-01 00:48:51 +08:00
|
|
|
|
*
|
2026-03-01 00:54:10 +08:00
|
|
|
|
* @return array<string, mixed>
|
2026-03-01 00:48:51 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function broadcastWith(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return [
|
|
|
|
|
|
'from_username' => $this->fromUsername,
|
|
|
|
|
|
'to_username' => $this->toUsername,
|
|
|
|
|
|
'type' => 'friend_removed',
|
2026-03-01 00:54:10 +08:00
|
|
|
|
'had_added_back' => $this->hadAddedBack,
|
2026-03-01 00:48:51 +08:00
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|