Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 091f56e

Browse files
committed
Simplify controller logic due to PresenceChannel logic changes
1 parent 990a075 commit 091f56e

File tree

2 files changed

+22
-39
lines changed

2 files changed

+22
-39
lines changed

src/HttpApi/Controllers/FetchChannelsController.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,21 @@ public function __invoke(Request $request)
4949
return $channel->getChannelName();
5050
})->toArray();
5151

52-
// We ask the replication backend to get us the member count per channel
53-
$memberCounts = $this->replication->channelMemberCounts($request->appId, $channelNames);
52+
// We ask the replication backend to get us the member count per channel.
53+
// We get $counts back as a key-value array of channel names and their member count.
54+
return $this->replication
55+
->channelMemberCounts($request->appId, $channelNames)
56+
->then(function (array $counts) use ($channels, $attributes) {
57+
return [
58+
'channels' => $channels->map(function (PresenceChannel $channel) use ($counts, $attributes) {
59+
$info = new \stdClass;
60+
if (in_array('user_count', $attributes)) {
61+
$info->user_count = $counts[$channel->getChannelName()];
62+
}
5463

55-
// We return a promise since the backend runs async. We get $counts back
56-
// as a key-value array of channel names and their member count.
57-
return $memberCounts->then(function (array $counts) use ($channels, $attributes) {
58-
return $this->collectUserCounts($channels, $attributes, function (PresenceChannel $channel) use ($counts) {
59-
return $counts[$channel->getChannelName()];
64+
return $info;
65+
})->toArray() ?: new \stdClass,
66+
];
6067
});
61-
});
62-
}
63-
64-
protected function collectUserCounts(Collection $channels, array $attributes, callable $transformer)
65-
{
66-
return [
67-
'channels' => $channels->map(function (PresenceChannel $channel) use ($transformer, $attributes) {
68-
$info = new \stdClass;
69-
if (in_array('user_count', $attributes)) {
70-
$info->user_count = $transformer($channel);
71-
}
72-
73-
return $info;
74-
})->toArray() ?: new \stdClass,
75-
];
7668
}
7769
}

src/HttpApi/Controllers/FetchUsersController.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,14 @@ public function __invoke(Request $request)
2222
throw new HttpException(400, 'Invalid presence channel "'.$request->channelName.'"');
2323
}
2424

25-
$users = $channel->getUsers($request->appId);
26-
27-
if ($users instanceof PromiseInterface) {
28-
return $users->then(function (array $users) {
29-
return $this->collectUsers($users);
25+
return $channel
26+
->getUsers($request->appId)
27+
->then(function (array $users) {
28+
return [
29+
'users' => Collection::make($users)->map(function ($user) {
30+
return ['id' => $user->user_id];
31+
})->values(),
32+
];
3033
});
31-
}
32-
33-
return $this->collectUsers($users);
34-
}
35-
36-
protected function collectUsers(array $users)
37-
{
38-
return [
39-
'users' => Collection::make($users)->map(function ($user) {
40-
return ['id' => $user->user_id];
41-
})->values(),
42-
];
4334
}
4435
}

0 commit comments

Comments
 (0)