1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Events;
7
8use App\Models\Chat\Channel as ChatChannel;
9use App\Models\User;
10use App\Transformers\Chat\ChannelTransformer;
11use Illuminate\Broadcasting\Channel;
12use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
13
14class ChatChannelEvent extends BroadcastableEventBase implements ShouldBroadcastNow
15{
16 public function __construct(public ChatChannel $channel, public User $user, public string $action)
17 {
18 }
19
20 public function broadcastAs()
21 {
22 return "chat.channel.{$this->action}";
23 }
24
25 /**
26 * Get the channels the event should broadcast on.
27 *
28 * @return Channel|array
29 */
30 public function broadcastOn()
31 {
32 return new Channel("private:user:{$this->user->getKey()}");
33 }
34
35 public function broadcastWith()
36 {
37 // TODO: parting channel only needs channel id.
38 return json_item($this->channel, ChannelTransformer::forUser($this->user), ChannelTransformer::LISTING_INCLUDES);
39 }
40}