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

30 lines
955 B
PHP
Raw Normal View History

2018-11-22 21:09:32 +00:00
<?php
2018-11-27 14:55:30 +00:00
namespace BeyondCode\LaravelWebSockets\WebSocketServer\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 14:55:30 +00:00
use BeyondCode\LaravelWebSockets\WebSocketServer\Pusher\Channels\PresenceChannel;
2018-11-22 21:09:32 +00:00
class FetchUsers extends EchoController
{
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()
];
}
}