From 091f56ea15bb4ee361901e0967cc39d502d837ae Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Mon, 29 Jul 2019 17:33:27 -0400 Subject: [PATCH] Simplify controller logic due to PresenceChannel logic changes --- .../Controllers/FetchChannelsController.php | 36 ++++++++----------- .../Controllers/FetchUsersController.php | 25 +++++-------- 2 files changed, 22 insertions(+), 39 deletions(-) diff --git a/src/HttpApi/Controllers/FetchChannelsController.php b/src/HttpApi/Controllers/FetchChannelsController.php index ed44872..0a81520 100644 --- a/src/HttpApi/Controllers/FetchChannelsController.php +++ b/src/HttpApi/Controllers/FetchChannelsController.php @@ -49,29 +49,21 @@ class FetchChannelsController extends Controller return $channel->getChannelName(); })->toArray(); - // We ask the replication backend to get us the member count per channel - $memberCounts = $this->replication->channelMemberCounts($request->appId, $channelNames); + // 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. + return $this->replication + ->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()]; + } - // We return a promise since the backend runs async. We get $counts back - // as a key-value array of channel names and their member count. - return $memberCounts->then(function (array $counts) use ($channels, $attributes) { - return $this->collectUserCounts($channels, $attributes, function (PresenceChannel $channel) use ($counts) { - return $counts[$channel->getChannelName()]; + return $info; + })->toArray() ?: new \stdClass, + ]; }); - }); - } - - protected function collectUserCounts(Collection $channels, array $attributes, callable $transformer) - { - return [ - 'channels' => $channels->map(function (PresenceChannel $channel) use ($transformer, $attributes) { - $info = new \stdClass; - if (in_array('user_count', $attributes)) { - $info->user_count = $transformer($channel); - } - - return $info; - })->toArray() ?: new \stdClass, - ]; } } diff --git a/src/HttpApi/Controllers/FetchUsersController.php b/src/HttpApi/Controllers/FetchUsersController.php index 3d7ced7..9bae8c6 100644 --- a/src/HttpApi/Controllers/FetchUsersController.php +++ b/src/HttpApi/Controllers/FetchUsersController.php @@ -22,23 +22,14 @@ class FetchUsersController extends Controller throw new HttpException(400, 'Invalid presence channel "'.$request->channelName.'"'); } - $users = $channel->getUsers($request->appId); - - if ($users instanceof PromiseInterface) { - return $users->then(function (array $users) { - return $this->collectUsers($users); + return $channel + ->getUsers($request->appId) + ->then(function (array $users) { + return [ + 'users' => Collection::make($users)->map(function ($user) { + return ['id' => $user->user_id]; + })->values(), + ]; }); - } - - return $this->collectUsers($users); - } - - protected function collectUsers(array $users) - { - return [ - 'users' => Collection::make($users)->map(function ($user) { - return ['id' => $user->user_id]; - })->values(), - ]; } }