laravel-websockets/src/WebSocket/Controllers/FetchUsers.php

30 lines
939 B
PHP
Raw Normal View History

2018-11-22 21:09:32 +00:00
<?php
2018-11-27 15:07:29 +00:00
namespace BeyondCode\LaravelWebSockets\WebSocket\Controllers;
2018-11-22 21:09:32 +00:00
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Symfony\Component\HttpKernel\Exception\HttpException;
2018-11-27 15:07:29 +00:00
use BeyondCode\LaravelWebSockets\WebSocket\Pusher\Channels\PresenceChannel;
2018-11-22 21:09:32 +00:00
2018-11-27 14:59:02 +00:00
class FetchUsers extends Controller
2018-11-22 21:09:32 +00:00
{
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()
];
}
}