Skip to content

Commit

Permalink
fix(galleries): implement eager loading, refactor queries (#1175)
Browse files Browse the repository at this point in the history
- refactor gallery index queries to take better advantage of eager loading
- add "with", "withCount" properties to relevant models
- add comment relation to gallery submissions
- only get site currency name for currency comms
- rename gallery submission collaboratorApproved attribute to not conflict with scope
- refactor gallery submission scopes
-move gallery submission vote data preprocessing to model function
  • Loading branch information
itinerare authored Jan 12, 2025
1 parent e54b742 commit 8b12dff
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 112 deletions.
37 changes: 20 additions & 17 deletions app/Http/Controllers/GalleryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Facades\Settings;
use App\Models\Character\Character;
use App\Models\Comment\Comment;
use App\Models\Currency\Currency;
use App\Models\Gallery\Gallery;
use App\Models\Gallery\GallerySubmission;
Expand All @@ -13,7 +12,7 @@
use App\Services\GalleryManager;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use View;
use Illuminate\Support\Facades\View;

class GalleryController extends Controller {
/*
Expand All @@ -39,10 +38,13 @@ public function __construct() {
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getGalleryIndex() {
$galleries = Gallery::whereNull('parent_id')->active()->sort()->with('children', 'children.submissions', 'submissions')->withCount('submissions', 'children');

return view('galleries.index', [
'galleries' => Gallery::sort()->active()->whereNull('parent_id')->paginate(10),
'galleryPage' => false,
'sideGallery' => null,
'galleries' => $galleries->paginate(10),
'galleryPage' => false,
'sideGallery' => null,
'submissionsOpen' => Settings::get('gallery_submissions_open'),
]);
}

Expand All @@ -54,12 +56,12 @@ public function getGalleryIndex() {
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getGallery($id, Request $request) {
$gallery = Gallery::visible()->where('id', $id)->first();
$gallery = Gallery::visible()->where('id', $id)->withCount('submissions')->first();
if (!$gallery) {
abort(404);
}

$query = GallerySubmission::where('gallery_id', $gallery->id)->visible(Auth::check() ? Auth::user() : null)->accepted();
$query = GallerySubmission::where('gallery_id', $gallery->id)->visible(Auth::check() ? Auth::user() : null);
$sort = $request->only(['sort']);

if ($request->get('title')) {
Expand Down Expand Up @@ -99,8 +101,8 @@ public function getGallery($id, Request $request) {
return view('galleries.gallery', [
'gallery' => $gallery,
'submissions' => $query->paginate(20)->appends($request->query()),
'prompts' => [0 => 'Any Prompt'] + Prompt::whereIn('id', GallerySubmission::where('gallery_id', $gallery->id)->visible(Auth::check() ? Auth::user() : null)->accepted()->whereNotNull('prompt_id')->select('prompt_id')->distinct()->pluck('prompt_id')->toArray())->orderBy('name')->pluck('name', 'id')->toArray(),
'childSubmissions' => GallerySubmission::whereIn('gallery_id', $gallery->children->pluck('id')->toArray())->where('is_visible', 1)->where('status', 'Accepted'),
'prompts' => [0 => 'Any Prompt'] + Prompt::whereIn('id', GallerySubmission::where('gallery_id', $gallery->id)->withOnly('prompt')->visible(Auth::user() ?? null)->whereNotNull('prompt_id')->select('prompt_id')->distinct()->pluck('prompt_id')->toArray())->orderBy('name')->pluck('name', 'id')->toArray(),
'childSubmissions' => $gallery->through('children')->has('submissions')->where('is_visible', 1)->where('status', 'Accepted'),
'galleryPage' => true,
'sideGallery' => $gallery,
]);
Expand Down Expand Up @@ -155,7 +157,7 @@ public function getAll(Request $request) {

return view('galleries.showall', [
'submissions' => $query->paginate(20)->appends($request->query()),
'prompts' => [0 => 'Any Prompt'] + Prompt::whereIn('id', GallerySubmission::visible(Auth::check() ? Auth::user() : null)->accepted()->whereNotNull('prompt_id')->pluck('prompt_id')->toArray())->orderBy('name')->pluck('name', 'id')->toArray(),
'prompts' => [0 => 'Any Prompt'] + Prompt::whereIn('id', GallerySubmission::visible(Auth::user() ?? null)->accepted()->withOnly('prompt')->whereNotNull('prompt_id')->pluck('prompt_id')->toArray())->orderBy('name')->pluck('name', 'id')->toArray(),
'galleryPage' => false,
]);
}
Expand All @@ -168,7 +170,7 @@ public function getAll(Request $request) {
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getSubmission($id) {
$submission = GallerySubmission::find($id);
$submission = GallerySubmission::where('id', $id)->with('gallery', 'participants', 'characters')->first();
if (!$submission) {
abort(404);
}
Expand All @@ -187,7 +189,6 @@ public function getSubmission($id) {

return view('galleries.submission', [
'submission' => $submission,
'commentCount' => Comment::where('commentable_type', 'App\Models\Gallery\GallerySubmission')->where('commentable_id', $submission->id)->where('type', 'User-User')->count(),
'galleryPage' => true,
'sideGallery' => $submission->gallery,
]);
Expand All @@ -201,10 +202,12 @@ public function getSubmission($id) {
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getSubmissionFavorites($id) {
$submission = GallerySubmission::find($id);
$submission = GallerySubmission::where('id', $id)->withOnly('favorites')->first();
$favorites = $submission->favorites()->with('user')->get();

return view('galleries._submission_favorites', [
'submission' => $submission,
'favorites' => $favorites,
]);
}

Expand All @@ -216,7 +219,7 @@ public function getSubmissionFavorites($id) {
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getSubmissionLog($id) {
$submission = GallerySubmission::find($id);
$submission = GallerySubmission::where('id', $id)->with('participants')->without('favorites', 'comments')->first();
if (!$submission) {
abort(404);
}
Expand All @@ -242,12 +245,12 @@ public function getSubmissionLog($id) {
/**
* Shows the user's gallery submission log.
*
* @param mixed $type
* @param string $type
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getUserSubmissions(Request $request, $type) {
$submissions = GallerySubmission::userSubmissions(Auth::user());
$submissions = GallerySubmission::userSubmissions(Auth::user())->with('gallery')->without('favorites', 'comments');
if (!$type) {
$type = 'Pending';
}
Expand Down Expand Up @@ -300,7 +303,7 @@ public function getEditGallerySubmission($id) {
if (!Auth::check()) {
abort(404);
}
$submission = GallerySubmission::find($id);
$submission = GallerySubmission::where('id', $id)->with('gallery')->with('participants', 'characters')->without('comments', 'favorites')->first();
if (!$submission || $submission->status == 'Rejected') {
abort(404);
}
Expand Down
6 changes: 3 additions & 3 deletions app/Models/Gallery/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Models\Gallery;

use App\Facades\Settings;
use App\Models\Model;
use Carbon\Carbon;

Expand Down Expand Up @@ -202,12 +201,13 @@ public function getAdminPowerAttribute() {
/**
* Gets whether or not the user can submit to the gallery.
*
* @param bool $submissionsOpen
* @param mixed|null $user
*
* @return string
*/
public function canSubmit($user = null) {
if (Settings::get('gallery_submissions_open')) {
public function canSubmit($submissionsOpen, $user = null) {
if ($submissionsOpen) {
if ((isset($this->start_at) && $this->start_at->isFuture()) || (isset($this->end_at) && $this->end_at->isPast())) {
return false;
} elseif ($user && $user->hasPower('manage_submissions')) {
Expand Down
9 changes: 9 additions & 0 deletions app/Models/Gallery/GalleryCollaborator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ class GalleryCollaborator extends Model {
*/
protected $table = 'gallery_submission_collaborators';

/**
* The relationships that should always be loaded.
*
* @var array
*/
protected $with = [
'user',
];

/**********************************************************************************************
RELATIONS
Expand Down
Loading

0 comments on commit 8b12dff

Please sign in to comment.