From b698b951477776fd7dcb0612f4e5aa9b79fc2ce6 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Thu, 22 Nov 2018 22:20:23 +0100 Subject: [PATCH] wip --- .../Http/Controllers/FetchChannels.php | 42 +++++++++++++++++++ .../Pusher/Channels/ChannelManager.php | 5 +++ src/Router.php | 2 +- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/LaravelEcho/Http/Controllers/FetchChannels.php diff --git a/src/LaravelEcho/Http/Controllers/FetchChannels.php b/src/LaravelEcho/Http/Controllers/FetchChannels.php new file mode 100644 index 0000000..e4ee51b --- /dev/null +++ b/src/LaravelEcho/Http/Controllers/FetchChannels.php @@ -0,0 +1,42 @@ +channelManager = $channelManager; + } + + public function __invoke(Request $request) + { + $channels = Collection::make($this->channelManager->getChannels($request->appId))->filter(function ($channel) { + return $channel instanceof PresenceChannel; + }); + + if ($request->has('filter_by_prefix')) { + $channels = $channels->filter(function ($channel, $channelName) use ($request) { + return starts_with($channelName, $request->filter_by_prefix); + }); + } + + return [ + 'channels' => $channels->map(function ($channel) { + return [ + 'user_count' => count($channel->getUsers()), + ]; + })->toArray() + ]; + } +} \ No newline at end of file diff --git a/src/LaravelEcho/Pusher/Channels/ChannelManager.php b/src/LaravelEcho/Pusher/Channels/ChannelManager.php index 435f9fc..5bf37cb 100644 --- a/src/LaravelEcho/Pusher/Channels/ChannelManager.php +++ b/src/LaravelEcho/Pusher/Channels/ChannelManager.php @@ -42,6 +42,11 @@ class ChannelManager return Channel::class; } + public function getChannels(string $appId): array + { + return $this->channels[$appId] ?? []; + } + public function removeFromAllChannels(ConnectionInterface $connection) { collect($this->channels[$connection->appId])->each->unsubscribe($connection); diff --git a/src/Router.php b/src/Router.php index c06e545..d536e2a 100644 --- a/src/Router.php +++ b/src/Router.php @@ -68,7 +68,7 @@ class Router //TODO: add origin checker middleware $this->get('/app/{appKey}', LaravelEcho\WebSocket\PusherServer::class); - $this->get('/apps/{appId}/channels', LaravelEcho\Http\Controllers\StatusController::class); + $this->get('/apps/{appId}/channels', LaravelEcho\Http\Controllers\FetchChannels::class); $this->get('/apps/{appId}/channels/{channelName}', LaravelEcho\Http\Controllers\FetchChannel::class); $this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\FetchUsers::class);