2018-11-22 21:20:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-11-27 15:42:19 +00:00
|
|
|
namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers;
|
2018-11-22 21:20:23 +00:00
|
|
|
|
2019-02-27 14:27:21 +00:00
|
|
|
use Illuminate\Support\Str;
|
2018-11-22 21:20:23 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Collection;
|
2019-05-11 06:52:04 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
2018-11-22 21:20:23 +00:00
|
|
|
|
2018-11-27 20:15:29 +00:00
|
|
|
class FetchChannelsController extends Controller
|
2018-11-22 21:20:23 +00:00
|
|
|
{
|
|
|
|
|
public function __invoke(Request $request)
|
|
|
|
|
{
|
2019-05-11 06:52:04 +00:00
|
|
|
$attributes = [];
|
|
|
|
|
|
|
|
|
|
if ($request->has('info')) {
|
|
|
|
|
$attributes = explode(',', trim($request->info));
|
|
|
|
|
|
|
|
|
|
if (in_array('user_count', $attributes) && ! Str::startsWith($request->filter_by_prefix, 'presence-')) {
|
|
|
|
|
throw new HttpException(400, 'Request must be limited to presence channels in order to fetch user_count');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$channels = Collection::make($this->channelManager->getChannels($request->appId));
|
2018-11-22 21:20:23 +00:00
|
|
|
|
|
|
|
|
if ($request->has('filter_by_prefix')) {
|
|
|
|
|
$channels = $channels->filter(function ($channel, $channelName) use ($request) {
|
2019-02-27 14:27:21 +00:00
|
|
|
return Str::startsWith($channelName, $request->filter_by_prefix);
|
2018-11-22 21:20:23 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
2019-05-11 06:52:04 +00:00
|
|
|
'channels' => $channels->map(function ($channel) use ($attributes) {
|
|
|
|
|
$info = new \stdClass;
|
|
|
|
|
if (in_array('user_count', $attributes)) {
|
|
|
|
|
$info->user_count = count($channel->getUsers());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $info;
|
2018-12-07 19:38:17 +00:00
|
|
|
})->toArray() ?: new \stdClass,
|
2018-11-22 21:20:23 +00:00
|
|
|
];
|
|
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|