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

Commit 94722a7

Browse files
committed
Merge branch '2.x' of github.com:beyondcode/laravel-websockets into fix/fix-stale-data
2 parents 2d30edb + cbe4378 commit 94722a7

19 files changed

+840
-617
lines changed

src/API/Controller.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ protected function handleRequest(ConnectionInterface $connection)
176176

177177
$laravelRequest = Request::createFromBase((new HttpFoundationFactory)->createRequest($serverRequest));
178178

179-
$this->ensureValidAppId($laravelRequest->appId)
179+
$this
180+
->ensureValidAppId($laravelRequest->appId)
180181
->ensureValidSignature($laravelRequest);
181182

182183
// Invoke the controller action

src/API/FetchChannels.php

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,50 @@ public function __invoke(Request $request)
2828
}
2929
}
3030

31-
return $this->channelManager->getGlobalChannels($request->appId)->then(function ($channels) use ($request, $attributes) {
32-
$channels = collect($channels)->keyBy(function ($channel) {
33-
return $channel instanceof Channel
34-
? $channel->getName()
35-
: $channel;
36-
});
37-
38-
if ($request->has('filter_by_prefix')) {
39-
$channels = $channels->filter(function ($channel, $channelName) use ($request) {
40-
return Str::startsWith($channelName, $request->filter_by_prefix);
31+
return $this->channelManager
32+
->getGlobalChannels($request->appId)
33+
->then(function ($channels) use ($request, $attributes) {
34+
$channels = collect($channels)->keyBy(function ($channel) {
35+
return $channel instanceof Channel
36+
? $channel->getName()
37+
: $channel;
4138
});
42-
}
4339

44-
$channelNames = $channels->map(function ($channel) {
45-
return $channel instanceof Channel
46-
? $channel->getName()
47-
: $channel;
48-
})->toArray();
40+
if ($request->has('filter_by_prefix')) {
41+
$channels = $channels->filter(function ($channel, $channelName) use ($request) {
42+
return Str::startsWith($channelName, $request->filter_by_prefix);
43+
});
44+
}
4945

50-
return $this->channelManager
51-
->getChannelsMembersCount($request->appId, $channelNames)
52-
->then(function ($counts) use ($channels, $attributes) {
53-
$channels = $channels->map(function ($channel) use ($counts, $attributes) {
54-
$info = new stdClass;
46+
$channelNames = $channels->map(function ($channel) {
47+
return $channel instanceof Channel
48+
? $channel->getName()
49+
: $channel;
50+
})->toArray();
5551

56-
$channelName = $channel instanceof Channel
57-
? $channel->getName()
58-
: $channel;
52+
return $this->channelManager
53+
->getChannelsMembersCount($request->appId, $channelNames)
54+
->then(function ($counts) use ($channels, $attributes) {
55+
$channels = $channels->map(function ($channel) use ($counts, $attributes) {
56+
$info = new stdClass;
5957

60-
if (in_array('user_count', $attributes)) {
61-
$info->user_count = $counts[$channelName];
62-
}
58+
$channelName = $channel instanceof Channel
59+
? $channel->getName()
60+
: $channel;
6361

64-
return $info;
65-
})->sortBy(function ($content, $name) {
66-
return $name;
67-
})->all();
62+
if (in_array('user_count', $attributes)) {
63+
$info->user_count = $counts[$channelName];
64+
}
6865

69-
return [
70-
'channels' => $channels ?: new stdClass,
71-
];
72-
});
73-
});
66+
return $info;
67+
})->sortBy(function ($content, $name) {
68+
return $name;
69+
})->all();
70+
71+
return [
72+
'channels' => $channels ?: new stdClass,
73+
];
74+
});
75+
});
7476
}
7577
}

src/Apps/ConfigAppManager.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ public function __construct()
3030
*/
3131
public function all(): array
3232
{
33-
return $this->apps->map(function (array $appAttributes) {
34-
return $this->convertIntoApp($appAttributes);
35-
})->toArray();
33+
return $this->apps
34+
->map(function (array $appAttributes) {
35+
return $this->convertIntoApp($appAttributes);
36+
})
37+
->toArray();
3638
}
3739

3840
/**
@@ -104,7 +106,8 @@ protected function convertIntoApp(?array $appAttributes): ?App
104106
$app->setPath($appAttributes['path']);
105107
}
106108

107-
$app->enableClientMessages($appAttributes['enable_client_messages'])
109+
$app
110+
->enableClientMessages($appAttributes['enable_client_messages'])
108111
->enableStatistics($appAttributes['enable_statistics'])
109112
->setCapacity($appAttributes['capacity'] ?? null)
110113
->setAllowedOrigins($appAttributes['allowed_origins'] ?? []);

src/ChannelManagers/LocalChannelManager.php

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,16 @@ public function findOrCreate($appId, string $channel)
111111
*/
112112
public function getLocalConnections(): PromiseInterface
113113
{
114-
$connections = collect($this->channels)->map(function ($channelsWithConnections, $appId) {
115-
return collect($channelsWithConnections)->values();
116-
})->values()->collapse()
117-
->map(function ($channel) {
118-
return collect($channel->getConnections());
119-
})->values()->collapse()->toArray();
114+
$connections = collect($this->channels)
115+
->map(function ($channelsWithConnections, $appId) {
116+
return collect($channelsWithConnections)->values();
117+
})
118+
->values()->collapse()
119+
->map(function ($channel) {
120+
return collect($channel->getConnections());
121+
})
122+
->values()->collapse()
123+
->toArray();
120124

121125
return Helpers::createFulfilledPromise($connections);
122126
}
@@ -159,19 +163,23 @@ public function unsubscribeFromAllChannels(ConnectionInterface $connection): Pro
159163
return Helpers::createFulfilledPromise(false);
160164
}
161165

162-
$this->getLocalChannels($connection->app->id)->then(function ($channels) use ($connection) {
163-
collect($channels)->each->unsubscribe($connection);
166+
$this->getLocalChannels($connection->app->id)
167+
->then(function ($channels) use ($connection) {
168+
collect($channels)->each->unsubscribe($connection);
164169

165-
collect($channels)->reject->hasConnections()->each(function (Channel $channel, string $channelName) use ($connection) {
166-
unset($this->channels[$connection->app->id][$channelName]);
170+
collect($channels)
171+
->reject->hasConnections()
172+
->each(function (Channel $channel, string $channelName) use ($connection) {
173+
unset($this->channels[$connection->app->id][$channelName]);
174+
});
167175
});
168-
});
169176

170-
$this->getLocalChannels($connection->app->id)->then(function ($channels) use ($connection) {
171-
if (count($channels) === 0) {
172-
unset($this->channels[$connection->app->id]);
173-
}
174-
});
177+
$this->getLocalChannels($connection->app->id)
178+
->then(function ($channels) use ($connection) {
179+
if (count($channels) === 0) {
180+
unset($this->channels[$connection->app->id]);
181+
}
182+
});
175183

176184
return Helpers::createFulfilledPromise(true);
177185
}
@@ -244,15 +252,18 @@ public function unsubscribeFromApp($appId): PromiseInterface
244252
*/
245253
public function getLocalConnectionsCount($appId, string $channelName = null): PromiseInterface
246254
{
247-
return $this->getLocalChannels($appId)->then(function ($channels) use ($channelName) {
248-
return collect($channels)->when(! is_null($channelName), function ($collection) use ($channelName) {
249-
return $collection->filter(function (Channel $channel) use ($channelName) {
250-
return $channel->getName() === $channelName;
251-
});
252-
})->flatMap(function (Channel $channel) {
253-
return collect($channel->getConnections())->pluck('socketId');
254-
})->unique()->count();
255-
});
255+
return $this->getLocalChannels($appId)
256+
->then(function ($channels) use ($channelName) {
257+
return collect($channels)->when(! is_null($channelName), function ($collection) use ($channelName) {
258+
return $collection->filter(function (Channel $channel) use ($channelName) {
259+
return $channel->getName() === $channelName;
260+
});
261+
})
262+
->flatMap(function (Channel $channel) {
263+
return collect($channel->getConnections())->pluck('socketId');
264+
})
265+
->unique()->count();
266+
});
256267
}
257268

258269
/**
@@ -370,13 +381,14 @@ public function getChannelMember(ConnectionInterface $connection, string $channe
370381
*/
371382
public function getChannelsMembersCount($appId, array $channelNames): PromiseInterface
372383
{
373-
$results = collect($channelNames)->reduce(function ($results, $channel) use ($appId) {
374-
$results[$channel] = isset($this->users["{$appId}:{$channel}"])
375-
? count($this->users["{$appId}:{$channel}"])
376-
: 0;
384+
$results = collect($channelNames)
385+
->reduce(function ($results, $channel) use ($appId) {
386+
$results[$channel] = isset($this->users["{$appId}:{$channel}"])
387+
? count($this->users["{$appId}:{$channel}"])
388+
: 0;
377389

378-
return $results;
379-
}, []);
390+
return $results;
391+
}, []);
380392

381393
return Helpers::createFulfilledPromise($results);
382394
}
@@ -443,15 +455,16 @@ public function removeObsoleteConnections(): PromiseInterface
443455
*/
444456
public function updateConnectionInChannels($connection): PromiseInterface
445457
{
446-
return $this->getLocalChannels($connection->app->id)->then(function ($channels) use ($connection) {
447-
foreach ($channels as $channel) {
448-
if ($channel->hasConnection($connection)) {
449-
$channel->saveConnection($connection);
458+
return $this->getLocalChannels($connection->app->id)
459+
->then(function ($channels) use ($connection) {
460+
foreach ($channels as $channel) {
461+
if ($channel->hasConnection($connection)) {
462+
$channel->saveConnection($connection);
463+
}
450464
}
451-
}
452465

453-
return true;
454-
});
466+
return true;
467+
});
455468
}
456469

457470
/**

0 commit comments

Comments
 (0)