44
55use BookStack \Activity \Models \View ;
66use BookStack \Entities \EntityProvider ;
7- use BookStack \Entities \Models \BookChild ;
8- use BookStack \Entities \Models \Entity ;
7+ use BookStack \Entities \Tools \MixedEntityListLoader ;
98use BookStack \Permissions \PermissionApplicator ;
10- use Illuminate \Database \Eloquent \Relations \BelongsTo ;
119use Illuminate \Support \Collection ;
1210use Illuminate \Support \Facades \DB ;
1311
@@ -16,10 +14,11 @@ class QueryPopular
1614 public function __construct (
1715 protected PermissionApplicator $ permissions ,
1816 protected EntityProvider $ entityProvider ,
17+ protected MixedEntityListLoader $ listLoader ,
1918 ) {
2019 }
2120
22- public function run (int $ count , int $ page , array $ filterModels = null )
21+ public function run (int $ count , int $ page , array $ filterModels = null ): Collection
2322 {
2423 $ query = $ this ->permissions
2524 ->restrictEntityRelationQuery (View::query (), 'views ' , 'viewable_id ' , 'viewable_type ' )
@@ -31,24 +30,13 @@ public function run(int $count, int $page, array $filterModels = null)
3130 $ query ->whereIn ('viewable_type ' , $ this ->entityProvider ->getMorphClasses ($ filterModels ));
3231 }
3332
34- $ entities = $ query-> with ( ' viewable ' )
33+ $ views = $ query
3534 ->skip ($ count * ($ page - 1 ))
3635 ->take ($ count )
37- ->get ()
38- ->pluck ('viewable ' )
39- ->filter ();
36+ ->get ();
4037
41- $ this ->loadBooksForChildren ( $ entities );
38+ $ this ->listLoader -> loadIntoRelations ( $ views -> all (), ' viewable ' , false );
4239
43- return $ entities ;
44- }
45-
46- protected function loadBooksForChildren (Collection $ entities ): void
47- {
48- $ bookChildren = $ entities ->filter (fn (Entity $ entity ) => $ entity instanceof BookChild);
49- $ eloquent = (new \Illuminate \Database \Eloquent \Collection ($ bookChildren ));
50- $ eloquent ->load (['book ' => function (BelongsTo $ query ) {
51- $ query ->scopes ('visible ' );
52- }]);
40+ return $ views ->pluck ('viewable ' )->filter ();
5341 }
5442}
0 commit comments