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

Commit 5808a66

Browse files
committed
Avoid displaying twice the same-id channel members
1 parent b41f8b7 commit 5808a66

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/ChannelManagers/LocalChannelManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public function getChannelMembers($appId, string $channel): PromiseInterface
302302

303303
$members = collect($members)->map(function ($user) {
304304
return json_decode($user);
305-
})->toArray();
305+
})->unique('id')->toArray();
306306

307307
return new FulfilledPromise($members);
308308
}

src/ChannelManagers/RedisChannelManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ public function getChannelMembers($appId, string $channel): PromiseInterface
347347
->map(function ($user) {
348348
return json_decode($user);
349349
})
350+
->unique('id')
350351
->toArray();
351352
});
352353
}

tests/FetchUsersTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,34 @@ public function test_it_returns_connected_user_information()
116116
'users' => [['id' => 1]],
117117
], json_decode($response->getContent(), true));
118118
}
119+
120+
public function test_multiple_clients_with_same_id_gets_counted_once()
121+
{
122+
$rick = $this->newPresenceConnection('presence-channel', ['user_id' => 1]);
123+
$morty = $this->newPresenceConnection('presence-channel', ['user_id' => 1]);
124+
125+
$connection = new Mocks\Connection;
126+
127+
$requestPath = '/apps/1234/channel/presence-channel/users';
128+
129+
$routeParams = [
130+
'appId' => '1234',
131+
'channelName' => 'presence-channel',
132+
];
133+
134+
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
135+
136+
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
137+
138+
$controller = app(FetchUsers::class);
139+
140+
$controller->onOpen($connection, $request);
141+
142+
/** @var \Illuminate\Http\JsonResponse $response */
143+
$response = array_pop($connection->sentRawData);
144+
145+
$this->assertSame([
146+
'users' => [['id' => 1]],
147+
], json_decode($response->getContent(), true));
148+
}
119149
}

0 commit comments

Comments
 (0)