-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* perf: eager-load votes * Apply fixes from StyleCI * fix: sum value * fix: mostRelevantPost and update count action
- Loading branch information
Showing
6 changed files
with
78 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/gamification. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FoF\Gamification; | ||
|
||
use Flarum\Discussion\Discussion; | ||
use Flarum\Http\RequestUtil; | ||
use Flarum\Post\Post; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class LoadActorVoteRelationship | ||
{ | ||
public static function mutateRelation(HasMany $query, ServerRequestInterface $request): HasMany | ||
{ | ||
$actor = RequestUtil::getActor($request); | ||
|
||
return $query | ||
// So that we can tell if the current user has liked the post. | ||
->where('user_id', $actor->id); | ||
} | ||
|
||
public static function sumRelation($controller, $data): void | ||
{ | ||
$loadable = null; | ||
|
||
if ($data instanceof Discussion) { | ||
$loadable = $data->newCollection($data->posts)->filter(function ($post) { | ||
return $post instanceof Post; | ||
}); | ||
} elseif ($data instanceof Collection) { | ||
$loadable = $data->map(function ($model) { | ||
return $model instanceof Discussion ? ($model->mostRelevantPost ?? $model->firstPost) : $model; | ||
}); | ||
} elseif ($data instanceof Post) { | ||
$loadable = $data->newCollection([$data]); | ||
} | ||
|
||
if ($loadable) { | ||
$loadable->loadSum('actualvotes', 'value'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters