Simplify controller logic due to PresenceChannel logic changes
This commit is contained in:
parent
990a075b20
commit
091f56ea15
|
|
@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue