Skip to content

Commit

Permalink
refactor: use eloquent relations directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Roardom committed Aug 17, 2024
1 parent 498c4ab commit 165f161
Show file tree
Hide file tree
Showing 20 changed files with 74 additions and 105 deletions.
4 changes: 2 additions & 2 deletions app/Helpers/TorrentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ public static function approveHelper(int $id): void
switch (true) {
case $torrent->category->movie_meta:
User::query()
->whereHas('wishes', fn ($query) => $query->where('movie_id', '=', $torrent->tmdb))
->whereRelation('wishes', 'movie_id', '=', $torrent->tmdb)
->get()
->each
->notify(new NewWishListNotice($torrent));

break;
case $torrent->category->tv_meta:
User::query()
->whereHas('wishes', fn ($query) => $query->where('tv_id', '=', $torrent->tmdb))
->whereRelation('wishes', 'tv_id', '=', $torrent->tmdb)
->get()
->each
->notify(new NewWishListNotice($torrent));
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Controllers/Staff/AuditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ class AuditController extends Controller
public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
{
return view('Staff.audit.index', ['staffActivities' => Audit::with(['user', 'user.group'])
->whereHas('user.group', function ($query): void {
$query->where('is_modo', true);
})
->whereRelation('user.group', 'is_modo', '=', true)
->where('action', '!=', 'create') // Exclude audits with action 'create'
->select('user_id')
->selectRaw('COUNT(*) as total_actions')
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Staff/InternalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\View\Vie
'internalGroups' => Internal::orderBy('name')->get(),
'internalUsers' => User::with(['group', 'internals'])
->withCount('torrents as total_uploads')
->whereIn('group_id', Group::select('id')->where('is_internal', '=', true))
->whereRelation('group', 'is_internal', '=', true)
->orWhereHas('internals')
// Count recent uploads for current user
->withCount(['torrents as recent_uploads' => fn ($query) => $query
Expand Down
3 changes: 1 addition & 2 deletions app/Http/Controllers/Staff/UploaderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
namespace App\Http\Controllers\Staff;

use App\Http\Controllers\Controller;
use App\Models\Group;
use App\Models\User;

class UploaderController extends Controller
Expand All @@ -30,7 +29,7 @@ public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\View\Vie
return view('Staff.uploader.index', [
'uploaders' => User::with(['group'])
->withCount('torrents as total_uploads')
->whereIn('group_id', Group::select('id')->where('is_uploader', '=', true))
->whereRelation('group', 'is_uploader', '=', true)
// Count recent uploads for current user
->withCount(['torrents as recent_uploads' => fn ($query) => $query
->where('created_at', '>', now()->subDays(60))
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/StatsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function uploaded(): \Illuminate\Contracts\View\Factory|\Illuminate\View\
{
return view('stats.users.uploaded', [
'uploaded' => User::orderByDesc('uploaded')
->whereNotIn('group_id', Group::select('id')->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->whereDoesntHave('group', fn ($query) => $query->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->take(100)
->get(),
]);
Expand All @@ -74,7 +74,7 @@ public function downloaded(): \Illuminate\Contracts\View\Factory|\Illuminate\Vie
{
return view('stats.users.downloaded', [
'downloaded' => User::orderByDesc('downloaded')
->whereNotIn('group_id', Group::select('id')->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->whereDoesntHave('group', fn ($query) => $query->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->take(100)
->get(),
]);
Expand Down Expand Up @@ -139,7 +139,7 @@ public function bankers(): \Illuminate\Contracts\View\Factory|\Illuminate\View\V
{
return view('stats.users.bankers', [
'bankers' => User::orderByDesc('seedbonus')
->whereNotIn('group_id', Group::select('id')->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->whereDoesntHave('group', fn ($query) => $query->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->take(100)
->get(),
]);
Expand Down
13 changes: 6 additions & 7 deletions app/Http/Controllers/YearlyOverviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Comment;
use App\Models\Group;
use App\Models\History;
Expand Down Expand Up @@ -76,7 +75,7 @@ public function show(int $year): \Illuminate\Contracts\View\View|\Illuminate\Fou
fn ($join) => $join->on('torrents.id', '=', 'h.torrent_id')
)
->where('tmdb', '!=', 0)
->whereIn('category_id', Category::select('id')->where('movie_meta', '=', true))
->whereRelation('category', 'movie_meta', '=', true)
->groupBy('tmdb')
->orderByDesc('download_count')
->take(10)
Expand All @@ -99,7 +98,7 @@ public function show(int $year): \Illuminate\Contracts\View\View|\Illuminate\Fou
fn ($join) => $join->on('torrents.id', '=', 'h.torrent_id')
)
->where('tmdb', '!=', 0)
->whereIn('category_id', Category::select('id')->where('movie_meta', '=', true))
->whereRelation('category', 'movie_meta', '=', true)
->groupBy('tmdb')
->orderBy('download_count')
->take(5)
Expand All @@ -122,7 +121,7 @@ public function show(int $year): \Illuminate\Contracts\View\View|\Illuminate\Fou
fn ($join) => $join->on('torrents.id', '=', 'h.torrent_id')
)
->where('tmdb', '!=', 0)
->whereIn('category_id', Category::select('id')->where('tv_meta', '=', true))
->whereRelation('category', 'tv_meta', '=', true)
->groupBy('tmdb')
->orderByDesc('download_count')
->take(10)
Expand All @@ -145,7 +144,7 @@ public function show(int $year): \Illuminate\Contracts\View\View|\Illuminate\Fou
fn ($join) => $join->on('torrents.id', '=', 'h.torrent_id')
)
->where('tmdb', '!=', 0)
->whereIn('category_id', Category::select('id')->where('tv_meta', '=', true))
->whereRelation('category', 'tv_meta', '=', true)
->groupBy('tmdb')
->orderBy('download_count')
->take(5)
Expand Down Expand Up @@ -243,15 +242,15 @@ public function show(int $year): \Illuminate\Contracts\View\View|\Illuminate\Fou
fn () => Torrent::query()
->where('created_at', '>=', $year.'-01-01 00:00:00')
->where('created_at', '<=', $year.'-12-31 23:59:59')
->whereIn('category_id', Category::select('id')->where('movie_meta', '=', true))
->whereRelation('category', 'movie_meta', '=', true)
->count()
),
'tvUploads' => cache()->rememberForever(
'yearly-overview:'.$year.':tv-uploads',
fn () => Torrent::query()
->where('created_at', '>=', $year.'-01-01 00:00:00')
->where('created_at', '<=', $year.'-12-31 23:59:59')
->whereIn('category_id', Category::select('id')->where('tv_meta', '=', true))
->whereRelation('category', 'tv_meta', '=', true)
->count()
),
'totalUploads' => cache()->rememberForever(
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Livewire/ConversationSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
namespace App\Http\Livewire;

use App\Models\Conversation;
use App\Models\User;
use App\Traits\LivewireSort;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Rule;
Expand Down Expand Up @@ -76,8 +75,7 @@ final public function conversations(): \Illuminate\Pagination\LengthAwarePaginat
fn ($query) => $query
->whereHas(
'messages',
fn ($query) => $query
->whereIn('sender_id', User::select('id')->where('username', 'LIKE', $this->username))
fn ($query) => $query->whereRelation('sender', 'username', 'LIKE', $this->username)
)
)
->when(
Expand Down
3 changes: 1 addition & 2 deletions app/Http/Livewire/ForumCategoryTopicSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

namespace App\Http\Livewire;

use App\Models\Forum;
use App\Models\ForumCategory;
use App\Models\Topic;
use Livewire\Attributes\Computed;
Expand Down Expand Up @@ -77,7 +76,7 @@ final public function topics(): \Illuminate\Pagination\LengthAwarePaginator
'forum',
'reads' => fn ($query) => $query->whereBelongsto(auth()->user()),
])
->whereIn('forum_id', Forum::where('forum_category_id', '=', $this->category->id)->select('id'))
->whereRelation('forum', 'forum_category_id', '=', $this->category->id)
->authorized(canReadTopic: true)
->when($this->search !== '', fn ($query) => $query->where('name', 'LIKE', '%'.$this->search.'%'))
->when($this->label !== '', fn ($query) => $query->where($this->label, '=', 1))
Expand Down
5 changes: 2 additions & 3 deletions app/Http/Livewire/PersonCredit.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
namespace App\Http\Livewire;

use App\Enums\Occupation;
use App\Models\Category;
use App\Models\Person;
use App\Models\Torrent;
use App\Models\User;
Expand Down Expand Up @@ -197,12 +196,12 @@ final public function medias(): \Illuminate\Support\Collection
fn ($query) => $query
->where(
fn ($query) => $query
->whereIn('category_id', Category::select('id')->where('movie_meta', '=', 1))
->whereRelation('category', 'movie_meta', '=', 1)
->whereIntegerInRaw('tmdb', $movieIds)
)
->orWhere(
fn ($query) => $query
->whereIn('category_id', Category::select('id')->where('tv_meta', '=', 1))
->whereRelation('category', 'tv_meta', '=', 1)
->whereIntegerInRaw('tmdb', $tvIds)
)
)
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Livewire/SimilarTorrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ final public function torrents(): \Illuminate\Support\Collection
])
->when(
$this->category->movie_meta,
fn ($query) => $query->whereHas('category', fn ($query) => $query->where('movie_meta', '=', 1)),
fn ($query) => $query->whereRelation('category', 'movie_meta', '=', 1),
)
->when(
$this->category->tv_meta,
fn ($query) => $query->whereHas('category', fn ($query) => $query->where('tv_meta', '=', 1)),
fn ($query) => $query->whereRelation('category', 'tv_meta', '=', 1),
)
->when(
$this->category->tv_meta || $this->category->movie_meta,
Expand Down
10 changes: 2 additions & 8 deletions app/Http/Livewire/SubtitleSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
namespace App\Http\Livewire;

use App\Models\Subtitle;
use App\Models\Torrent;
use App\Models\User;
use App\Traits\LivewireSort;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Url;
Expand Down Expand Up @@ -70,16 +68,12 @@ final public function subtitles(): \Illuminate\Pagination\LengthAwarePaginator
return Subtitle::with(['user.group', 'torrent.category', 'language'])
->whereHas('torrent')
->when($this->search, fn ($query) => $query->where('title', 'like', '%'.$this->search.'%'))
->when($this->categories, function ($query) {
$torrents = Torrent::whereIntegerInRaw('category_id', $this->categories)->pluck('id');

return $query->whereIntegerInRaw('torrent_id', $torrents);
})
->when($this->categories, fn ($query) => $query->whereHas('torrent', fn ($query) => $query->whereIn('category_id', $this->categories)))
->when($this->language, fn ($query) => $query->where('language_id', '=', $this->language))
->when(
$this->username,
fn ($query) => $query
->whereIn('user_id', User::select('id')->where('username', '=', $this->username))
->whereRelation('user', 'username', '=', $this->username)
->when(
!auth()->user()->group->is_modo,
fn ($query) => $query->where(fn ($query) => $query->where('anon', '=', false)->orWhere('user_id', '=', auth()->id()))
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Livewire/Top10.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ final public function works(): Collection

return cache()->remember(
'top10-'.$this->interval.'-'.($this->from ?? '').'-'.($this->until ?? '').'-'.$this->metaType,
3600,
0, //3600,
fn () => Torrent::query()
->when(
$this->metaType === 'tv_meta',
Expand All @@ -99,7 +99,7 @@ final public function works(): Collection
->when($this->interval === 'year', fn ($query) => $query->whereBetween('history.completed_at', [now()->subYear(), now()]))
->when($this->interval === 'all', fn ($query) => $query->whereNotNull('history.completed_at'))
->when($this->interval === 'custom', fn ($query) => $query->whereBetween('history.completed_at', [$this->from ?: now(), $this->until ?: now()]))
->whereIn('torrents.category_id', Category::select('id')->where($this->metaType, '=', true))
->whereRelation('category', $this->metaType, '=', true)
// Small torrents screw the stats since users download them only to farm bon.
->where('torrents.size', '>', 1024 * 1024 * 1024)
->groupBy('tmdb')
Expand Down
9 changes: 4 additions & 5 deletions app/Http/Livewire/TopUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace App\Http\Livewire;

use App\Models\Comment;
use App\Models\Group;
use App\Models\History;
use App\Models\Peer;
use App\Models\Post;
Expand Down Expand Up @@ -61,7 +60,7 @@ final public function uploaded(): \Illuminate\Support\Collection
return User::select(['id', 'group_id', 'username', 'uploaded', 'image'])
->with('group')
->where('id', '!=', User::SYSTEM_USER_ID)
->whereNotIn('group_id', Group::select('id')->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->whereDoesntHave('group', fn ($query) => $query->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->orderByDesc('uploaded')
->take(8)
->get();
Expand All @@ -76,7 +75,7 @@ final public function downloaded(): \Illuminate\Support\Collection
return User::select(['id', 'group_id', 'username', 'downloaded', 'image'])
->with('group')
->where('id', '!=', User::SYSTEM_USER_ID)
->whereNotIn('group_id', Group::select('id')->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->whereDoesntHave('group', fn ($query) => $query->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->orderByDesc('downloaded')
->take(8)
->get();
Expand Down Expand Up @@ -108,7 +107,7 @@ final public function seedtimes(): \Illuminate\Support\Collection
return User::withSum('history as seedtime', 'seedtime')
->with('group')
->where('id', '!=', User::SYSTEM_USER_ID)
->whereNotIn('group_id', Group::select('id')->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->whereDoesntHave('group', fn ($query) => $query->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->orderByDesc('seedtime')
->take(8)
->get();
Expand All @@ -123,7 +122,7 @@ final public function served(): \Illuminate\Support\Collection
return User::withCount('uploadSnatches')
->with('group')
->where('id', '!=', User::SYSTEM_USER_ID)
->whereNotIn('group_id', Group::select('id')->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->whereDoesntHave('group', fn ($query) => $query->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->orderByDesc('upload_snatches_count')
->take(8)
->get();
Expand Down
8 changes: 2 additions & 6 deletions app/Http/Livewire/TorrentRequestSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
use App\Models\Movie;
use App\Models\Resolution;
use App\Models\TorrentRequest;
use App\Models\TorrentRequestBounty;
use App\Models\TorrentRequestClaim;
use App\Models\Type;
use App\Traits\CastLivewireProperties;
use App\Traits\LivewireSort;
Expand Down Expand Up @@ -250,12 +248,10 @@ final public function torrentRequests(): \Illuminate\Pagination\LengthAwarePagin
$query->where('user_id', '=', $user->id);
})
->when($this->myClaims, function ($query) use ($user): void {
$requestClaims = TorrentRequestClaim::where('user_id', '=', $user->id)->pluck('request_id');
$query->whereIntegerInRaw('id', $requestClaims)->whereNull('torrent_id')->whereNull('approved_by');
$query->whereRelation('claim', 'user_id', '=', $user->id)->whereNull('approved_by');
})
->when($this->myVoted, function ($query) use ($user): void {
$requestVotes = TorrentRequestBounty::where('user_id', '=', $user->id)->pluck('requests_id');
$query->whereIntegerInRaw('id', $requestVotes);
$query->whereRelation('bounties', 'user_id', '=', $user->id);
})
->when($this->myFilled, function ($query) use ($user): void {
$query->where('filled_by', '=', $user->id);
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Livewire/TorrentSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,12 @@ final public function groupedTorrents()
fn ($query) => $query
->where(
fn ($query) => $query
->whereIn('category_id', Category::select('id')->where('movie_meta', '=', 1))
->whereRelation('category', 'movie_meta', '=', 1)
->whereIntegerInRaw('tmdb', $movieIds)
)
->orWhere(
fn ($query) => $query
->whereIn('category_id', Category::select('id')->where('tv_meta', '=', 1))
->whereRelation('category', 'tv_meta', '=', 1)
->whereIntegerInRaw('tmdb', $tvIds)
)
)
Expand Down
8 changes: 2 additions & 6 deletions app/Models/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,14 @@ public function recommendedMovies(): \Illuminate\Database\Eloquent\Relations\Bel
*/
public function torrents(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Torrent::class, 'tmdb', 'id')->whereHas('category', function ($q): void {
$q->where('movie_meta', '=', true);
});
return $this->hasMany(Torrent::class, 'tmdb', 'id')->whereRelation('category', 'movie_meta', '=', true);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany<TorrentRequest, $this>
*/
public function requests(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(TorrentRequest::class, 'tmdb', 'id')->whereHas('category', function ($q): void {
$q->where('movie_meta', '-', true);
});
return $this->hasMany(TorrentRequest::class, 'tmdb', 'id')->whereRelation('category', 'movie_meta', '=', true);
}
}
4 changes: 1 addition & 3 deletions app/Models/Season.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ class Season extends Model
*/
public function torrents(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Torrent::class, 'tmdb', 'tv_id')->whereHas('category', function ($q): void {
$q->where('tv_meta', '=', true);
});
return $this->hasMany(Torrent::class, 'tmdb', 'tv_id')->whereRelation('category', 'tv_meta', '=', true);
}

/**
Expand Down
Loading

0 comments on commit 165f161

Please sign in to comment.