This commit is contained in:
Marcel Pociot 2018-11-22 22:09:32 +01:00
parent 54e2287646
commit 9a2183f642
3 changed files with 46 additions and 1 deletions

View File

@ -0,0 +1,40 @@
<?php
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Http\Controllers;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\PresenceChannel;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\InvalidSignatureException;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Symfony\Component\HttpKernel\Exception\HttpException;
class FetchUsers extends EchoController
{
/** @var \BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager */
protected $channelManager;
public function __construct(ChannelManager $channelManager)
{
$this->channelManager = $channelManager;
}
public function __invoke(Request $request)
{
$channel = $this->channelManager->find($request->appId, $request->channelName);
if (is_null($channel)) {
throw new HttpException(404, 'Unknown channel "'.$request->channelName.'"');
}
if (! $channel instanceof PresenceChannel) {
throw new HttpException(400, 'Invalid presence channel "'.$request->channelName.'"');
}
return [
'users' => Collection::make($channel->getUsers())->map(function ($user) {
return ['id' => $user->user_id];
})->values()
];
}
}

View File

@ -8,6 +8,11 @@ class PresenceChannel extends Channel
{
protected $users = [];
public function getUsers(): array
{
return $this->users;
}
/*
* @link https://pusher.com/docs/pusher_protocol#presence-channel-events
*/

View File

@ -72,7 +72,7 @@ class Router
$this->get('/apps/{appId}/status', LaravelEcho\Http\Controllers\StatusController::class);
$this->get('/apps/{appId}/channels', LaravelEcho\Http\Controllers\StatusController::class);
$this->get('/apps/{appId}/channels/{channelName}', LaravelEcho\Http\Controllers\FetchChannel::class);
$this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\StatusController::class);
$this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\FetchUsers::class);
$this->post('/apps/{appId}/events', LaravelEcho\Http\Controllers\TriggerEvent::class);
}