Skip to content

Commit 9a2183f

Browse files
committed
wip
1 parent 54e2287 commit 9a2183f

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Http\Controllers;
4+
5+
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
6+
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\PresenceChannel;
7+
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\InvalidSignatureException;
8+
use Illuminate\Http\Request;
9+
use Illuminate\Support\Collection;
10+
use Symfony\Component\HttpKernel\Exception\HttpException;
11+
12+
class FetchUsers extends EchoController
13+
{
14+
/** @var \BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager */
15+
protected $channelManager;
16+
17+
public function __construct(ChannelManager $channelManager)
18+
{
19+
$this->channelManager = $channelManager;
20+
}
21+
22+
public function __invoke(Request $request)
23+
{
24+
$channel = $this->channelManager->find($request->appId, $request->channelName);
25+
26+
if (is_null($channel)) {
27+
throw new HttpException(404, 'Unknown channel "'.$request->channelName.'"');
28+
}
29+
30+
if (! $channel instanceof PresenceChannel) {
31+
throw new HttpException(400, 'Invalid presence channel "'.$request->channelName.'"');
32+
}
33+
34+
return [
35+
'users' => Collection::make($channel->getUsers())->map(function ($user) {
36+
return ['id' => $user->user_id];
37+
})->values()
38+
];
39+
}
40+
}

src/LaravelEcho/Pusher/Channels/PresenceChannel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ class PresenceChannel extends Channel
88
{
99
protected $users = [];
1010

11+
public function getUsers(): array
12+
{
13+
return $this->users;
14+
}
15+
1116
/*
1217
* @link https://pusher.com/docs/pusher_protocol#presence-channel-events
1318
*/

src/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function echo()
7272
$this->get('/apps/{appId}/status', LaravelEcho\Http\Controllers\StatusController::class);
7373
$this->get('/apps/{appId}/channels', LaravelEcho\Http\Controllers\StatusController::class);
7474
$this->get('/apps/{appId}/channels/{channelName}', LaravelEcho\Http\Controllers\FetchChannel::class);
75-
$this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\StatusController::class);
75+
$this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\FetchUsers::class);
7676

7777
$this->post('/apps/{appId}/events', LaravelEcho\Http\Controllers\TriggerEvent::class);
7878
}

0 commit comments

Comments
 (0)