From 9a2183f642e84c7f41e6a94966a11d0b720de556 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Thu, 22 Nov 2018 22:09:32 +0100 Subject: [PATCH] wip --- .../Http/Controllers/FetchUsers.php | 40 +++++++++++++++++++ .../Pusher/Channels/PresenceChannel.php | 5 +++ src/Router.php | 2 +- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/LaravelEcho/Http/Controllers/FetchUsers.php diff --git a/src/LaravelEcho/Http/Controllers/FetchUsers.php b/src/LaravelEcho/Http/Controllers/FetchUsers.php new file mode 100644 index 0000000..8a6feda --- /dev/null +++ b/src/LaravelEcho/Http/Controllers/FetchUsers.php @@ -0,0 +1,40 @@ +channelManager = $channelManager; + } + + 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() + ]; + } +} \ No newline at end of file diff --git a/src/LaravelEcho/Pusher/Channels/PresenceChannel.php b/src/LaravelEcho/Pusher/Channels/PresenceChannel.php index 160254e..d7002db 100644 --- a/src/LaravelEcho/Pusher/Channels/PresenceChannel.php +++ b/src/LaravelEcho/Pusher/Channels/PresenceChannel.php @@ -8,6 +8,11 @@ class PresenceChannel extends Channel { protected $users = []; + public function getUsers(): array + { + return $this->users; + } + /* * @link https://pusher.com/docs/pusher_protocol#presence-channel-events */ diff --git a/src/Router.php b/src/Router.php index a2f2150..8eba03d 100644 --- a/src/Router.php +++ b/src/Router.php @@ -72,7 +72,7 @@ class Router $this->get('/apps/{appId}/status', LaravelEcho\Http\Controllers\StatusController::class); $this->get('/apps/{appId}/channels', LaravelEcho\Http\Controllers\StatusController::class); $this->get('/apps/{appId}/channels/{channelName}', LaravelEcho\Http\Controllers\FetchChannel::class); - $this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\StatusController::class); + $this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\FetchUsers::class); $this->post('/apps/{appId}/events', LaravelEcho\Http\Controllers\TriggerEvent::class); }