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
|
|
|
|
2020-08-13 11:02:58 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel;
|
2018-11-22 21:20:23 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Collection;
|
2020-03-04 09:58:39 +00:00
|
|
|
use Illuminate\Support\Str;
|
2019-05-15 21:11:33 +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
|
|
|
{
|
2019-07-29 21:20:22 +00:00
|
|
|
/** @var ReplicationInterface */
|
2020-08-13 16:27:24 +00:00
|
|
|
protected $replicator;
|
2019-07-29 21:20:22 +00:00
|
|
|
|
2020-08-13 16:27:24 +00:00
|
|
|
public function __construct(ChannelManager $channelManager, ReplicationInterface $replicator)
|
2019-07-29 21:20:22 +00:00
|
|
|
{
|
|
|
|
|
parent::__construct($channelManager);
|
|
|
|
|
|
2020-08-13 16:27:24 +00:00
|
|
|
$this->replicator = $replicator;
|
2019-07-29 21:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-29 20:20:48 +00:00
|
|
|
// We want to get the channel user count all in one shot when
|
|
|
|
|
// using a replication backend rather than doing individual queries.
|
|
|
|
|
// To do so, we first collect the list of channel names.
|
2020-08-13 11:02:58 +00:00
|
|
|
$channelNames = $channels->map(function (PresenceChannel $channel) {
|
2019-07-29 20:20:48 +00:00
|
|
|
return $channel->getChannelName();
|
|
|
|
|
})->toArray();
|
2019-03-29 19:33:46 +00:00
|
|
|
|
2019-07-29 21:33:27 +00:00
|
|
|
// We ask the replication backend to get us the member count per channel.
|
|
|
|
|
// We get $counts back as a key-value array of channel names and their member count.
|
2020-08-13 16:27:24 +00:00
|
|
|
return $this->replicator
|
2019-07-29 21:33:27 +00:00
|
|
|
->channelMemberCounts($request->appId, $channelNames)
|
|
|
|
|
->then(function (array $counts) use ($channels, $attributes) {
|
|
|
|
|
return [
|
|
|
|
|
'channels' => $channels->map(function (PresenceChannel $channel) use ($counts, $attributes) {
|
|
|
|
|
$info = new \stdClass;
|
|
|
|
|
if (in_array('user_count', $attributes)) {
|
|
|
|
|
$info->user_count = $counts[$channel->getChannelName()];
|
|
|
|
|
}
|
2019-03-29 19:33:46 +00:00
|
|
|
|
2019-07-29 21:33:27 +00:00
|
|
|
return $info;
|
|
|
|
|
})->toArray() ?: new \stdClass,
|
|
|
|
|
];
|
2019-03-29 19:33:46 +00:00
|
|
|
});
|
2018-11-22 21:20:23 +00:00
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|