Simplify controller logic due to PresenceChannel logic changes

This commit is contained in:
Francis Lavoie 2019-07-29 17:33:27 -04:00
parent 990a075b20
commit 091f56ea15
No known key found for this signature in database
GPG Key ID: B9E0E04A76AF4692
2 changed files with 22 additions and 39 deletions

View File

@ -49,29 +49,21 @@ class FetchChannelsController extends Controller
return $channel->getChannelName(); return $channel->getChannelName();
})->toArray(); })->toArray();
// We ask the replication backend to get us the member count per channel // We ask the replication backend to get us the member count per channel.
$memberCounts = $this->replication->channelMemberCounts($request->appId, $channelNames); // 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 return $info;
// as a key-value array of channel names and their member count. })->toArray() ?: new \stdClass,
return $memberCounts->then(function (array $counts) use ($channels, $attributes) { ];
return $this->collectUserCounts($channels, $attributes, function (PresenceChannel $channel) use ($counts) {
return $counts[$channel->getChannelName()];
}); });
});
}
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,
];
} }
} }

View File

@ -22,23 +22,14 @@ class FetchUsersController extends Controller
throw new HttpException(400, 'Invalid presence channel "'.$request->channelName.'"'); throw new HttpException(400, 'Invalid presence channel "'.$request->channelName.'"');
} }
$users = $channel->getUsers($request->appId); return $channel
->getUsers($request->appId)
if ($users instanceof PromiseInterface) { ->then(function (array $users) {
return $users->then(function (array $users) { return [
return $this->collectUsers($users); '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(),
];
} }
} }