Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Nov 20, 2023
1 parent bae042d commit a9ac7f2
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Channels/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function broadcast(Application $app, array $payload, Connection $except =
/**
* Get the data associated with the channel.
*/
public function data(Application $app): array
public function data(): array
{
return [];
}
Expand Down
14 changes: 12 additions & 2 deletions src/Channels/PresenceChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,24 @@ public function unsubscribe(Connection $connection): void
/**
* Get the data associated with the channel.
*/
public function data(Application $app): array
public function data(): array
{
$connections = collect($this->connections->all())
->map(fn ($connection) => $connection->data());

if ($connections->contains(fn ($connection) => ! isset($connection['user_id']))) {
return [
'presence' => [
'count' => 0,
'ids' => [],
'hash' => [],
],
];
}

return [
'presence' => [
'count' => $connections->count(),
'count' => $connections->count() ?? 0,
'ids' => $connections->map(fn ($connection) => $connection['user_id'])->all(),
'hash' => $connections->keyBy('user_id')->map->user_info->toArray(),
],
Expand Down
2 changes: 1 addition & 1 deletion src/Pusher/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function subscribe(Connection $connection, string $channel, string

$channel->subscribe($connection, $auth, $data);

self::sendInternally($connection, 'subscription_succeeded', $channel->name(), $channel->data($connection->app()));
self::sendInternally($connection, 'subscription_succeeded', $channel->name(), $channel->data());
}

/**
Expand Down
File renamed without changes.
10 changes: 4 additions & 6 deletions tests/Feature/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,24 @@
'event' => 'pusher:subscribe',
'data' => [
'channel' => 'private-test-channel',
'auth' => 'app-key:'.hash_hmac('sha256', '10000.00001:private-test-channel', 'pusher-secret'),
'auth' => 'app-key:'.hash_hmac('sha256', $connection->id().':private-test-channel', 'pusher-secret'),
],
]));

$connection->assertSent([
'event' => 'pusher_internal:subscription_succeeded',
'channel' => 'private-test-channel',
]);
})->todo();
});

it('can subscribe a user to a presence channel', function () {
$this->channelManager->shouldReceive('connections')->andReturn(Connections::make());
$this->channelManager->shouldReceive('connectionKeys')->andReturn(collect());
$this->server->message(
$connection = new Connection,
json_encode([
'event' => 'pusher:subscribe',
'data' => [
'channel' => 'presence-test-channel',
'auth' => 'app-key:'.hash_hmac('sha256', '10000.00001:presence-test-channel', 'pusher-secret'),
'auth' => 'app-key:'.hash_hmac('sha256', $connection->id().':presence-test-channel', 'pusher-secret'),
],
]));

Expand All @@ -152,7 +150,7 @@
]),
'channel' => 'presence-test-channel',
]);
})->todo();
});

it('unsubscribes a user from a channel on disconnection', function () {
$channelManager = Mockery::spy(ChannelManager::class);
Expand Down

0 comments on commit a9ac7f2

Please sign in to comment.