diff --git a/src/ChannelManagers/LocalChannelManager.php b/src/ChannelManagers/LocalChannelManager.php index 7ff689b..ffe02f8 100644 --- a/src/ChannelManagers/LocalChannelManager.php +++ b/src/ChannelManagers/LocalChannelManager.php @@ -302,7 +302,7 @@ class LocalChannelManager implements ChannelManager $members = collect($members)->map(function ($user) { return json_decode($user); - })->toArray(); + })->unique('id')->toArray(); return new FulfilledPromise($members); } diff --git a/src/ChannelManagers/RedisChannelManager.php b/src/ChannelManagers/RedisChannelManager.php index beb51e8..01ec084 100644 --- a/src/ChannelManagers/RedisChannelManager.php +++ b/src/ChannelManagers/RedisChannelManager.php @@ -347,6 +347,7 @@ class RedisChannelManager extends LocalChannelManager ->map(function ($user) { return json_decode($user); }) + ->unique('id') ->toArray(); }); } diff --git a/tests/FetchUsersTest.php b/tests/FetchUsersTest.php index bda1e20..0a5fc09 100644 --- a/tests/FetchUsersTest.php +++ b/tests/FetchUsersTest.php @@ -116,4 +116,34 @@ class FetchUsersTest extends TestCase 'users' => [['id' => 1]], ], json_decode($response->getContent(), true)); } + + public function test_multiple_clients_with_same_id_gets_counted_once() + { + $rick = $this->newPresenceConnection('presence-channel', ['user_id' => 1]); + $morty = $this->newPresenceConnection('presence-channel', ['user_id' => 1]); + + $connection = new Mocks\Connection; + + $requestPath = '/apps/1234/channel/presence-channel/users'; + + $routeParams = [ + 'appId' => '1234', + 'channelName' => 'presence-channel', + ]; + + $queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath); + + $request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams)); + + $controller = app(FetchUsers::class); + + $controller->onOpen($connection, $request); + + /** @var \Illuminate\Http\JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([ + 'users' => [['id' => 1]], + ], json_decode($response->getContent(), true)); + } }