From 4811a7cecaa73be979259cd2a6a4bdcc74d47d3f Mon Sep 17 00:00:00 2001 From: Roardom Date: Sat, 27 Aug 2022 06:17:29 -0500 Subject: [PATCH] refactor: simplify user model Models shouldn't contain custom logic --- .../Commands/AutoDisableInactiveUsers.php | 2 +- app/Console/Commands/AutoGroup.php | 4 +- app/Http/Controllers/FollowController.php | 6 +- .../Controllers/SubscriptionController.php | 12 +- app/Models/Follow.php | 9 + app/Models/Subscription.php | 17 ++ app/Models/User.php | 154 ++---------------- app/Models/Warning.php | 9 + .../views/blocks/top_uploaders.blade.php | 4 +- resources/views/forum/display.blade.php | 2 +- resources/views/forum/subscriptions.blade.php | 4 +- resources/views/forum/topic.blade.php | 2 +- resources/views/partials/top_nav.blade.php | 8 +- resources/views/partials/userbar.blade.php | 106 ------------ .../views/stats/users/leechers.blade.php | 2 +- .../views/stats/users/uploaders.blade.php | 2 +- .../views/torrent/partials/general.blade.php | 4 +- resources/views/user/buttons/user.blade.php | 2 +- resources/views/user/profile.blade.php | 8 +- 19 files changed, 82 insertions(+), 275 deletions(-) delete mode 100644 resources/views/partials/userbar.blade.php diff --git a/app/Console/Commands/AutoDisableInactiveUsers.php b/app/Console/Commands/AutoDisableInactiveUsers.php index 92853868a..302c1a899 100644 --- a/app/Console/Commands/AutoDisableInactiveUsers.php +++ b/app/Console/Commands/AutoDisableInactiveUsers.php @@ -57,7 +57,7 @@ class AutoDisableInactiveUsers extends Command ->all(); foreach ($users as $user) { - if ($user->getSeeding() === 0) { + if ($user->seedingTorrents()->count() === 0) { $user->group_id = $disabledGroup[0]; $user->can_upload = 0; $user->can_download = 0; diff --git a/app/Console/Commands/AutoGroup.php b/app/Console/Commands/AutoGroup.php index fabec541b..7671ac5a7 100644 --- a/app/Console/Commands/AutoGroup.php +++ b/app/Console/Commands/AutoGroup.php @@ -96,7 +96,7 @@ class AutoGroup extends Command } // Seeder Seedsize >= 5TiB and account 1 month old and seedtime average 30 days or better - if ($user->getTotalSeedSize() >= $byteUnits->bytesFromUnit('5TiB') && $user->getRatio() >= \config('other.ratio') && \round($user->getTotalSeedTime() / \max(1, $hiscount)) > 2_592_000 && $user->created_at < $current->copy()->subDays(30)->toDateTimeString() && $user->group_id != UserGroups::SEEDER) { + if ($user->seedingTorrents()->sum('size') >= $byteUnits->bytesFromUnit('5TiB') && $user->getRatio() >= \config('other.ratio') && \round($user->history()->sum('seedtime') / \max(1, $hiscount)) > 2_592_000 && $user->created_at < $current->copy()->subDays(30)->toDateTimeString() && $user->group_id != UserGroups::SEEDER) { $user->group_id = UserGroups::SEEDER; $user->save(); } @@ -108,7 +108,7 @@ class AutoGroup extends Command } // Archivist Seedsize >= 10TiB and account 3 month old and seedtime average 60 days or better - if ($user->getTotalSeedSize() >= $byteUnits->bytesFromUnit('10TiB') && $user->getRatio() >= \config('other.ratio') && \round($user->getTotalSeedTime() / \max(1, $hiscount)) > 2_592_000 * 2 && $user->created_at < $current->copy()->subDays(90)->toDateTimeString() && $user->group_id != UserGroups::ARCHIVIST) { + if ($user->seedingTorrents()->sum('size') >= $byteUnits->bytesFromUnit('10TiB') && $user->getRatio() >= \config('other.ratio') && \round($user->history()->sum('seedtime') / \max(1, $hiscount)) > 2_592_000 * 2 && $user->created_at < $current->copy()->subDays(90)->toDateTimeString() && $user->group_id != UserGroups::ARCHIVIST) { $user->group_id = UserGroups::ARCHIVIST; $user->save(); } diff --git a/app/Http/Controllers/FollowController.php b/app/Http/Controllers/FollowController.php index 7570b088d..bb3a435bb 100644 --- a/app/Http/Controllers/FollowController.php +++ b/app/Http/Controllers/FollowController.php @@ -36,7 +36,7 @@ class FollowController extends Controller ->withErrors(\trans('user.follow-yourself')); } - if (! $request->user()->isFollowing($user->id)) { + if ($request->user()->follows()->following($user->id)->doesntExist()) { $follow = new Follow(); $follow->user_id = $request->user()->id; $follow->target_id = $user->id; @@ -60,8 +60,8 @@ class FollowController extends Controller { $user = User::where('username', '=', $username)->firstOrFail(); - if ($request->user()->isFollowing($user->id)) { - $follow = $request->user()->follows()->where('target_id', '=', $user->id)->first(); + if ($request->user()->follows()->following($user->id)->exists()) { + $follow = $request->user()->follows()->following($user->id)->first(); $follow->delete(); if ($user->acceptsNotification($request->user(), $user, 'account', 'show_account_unfollow')) { $user->notify(new NewUnfollow('user', $request->user(), $user, $follow)); diff --git a/app/Http/Controllers/SubscriptionController.php b/app/Http/Controllers/SubscriptionController.php index 09e7cbac0..2b1a7ed78 100644 --- a/app/Http/Controllers/SubscriptionController.php +++ b/app/Http/Controllers/SubscriptionController.php @@ -39,7 +39,7 @@ class SubscriptionController extends Controller $params = ['id' => $topic->id]; } - if (! $request->user()->isSubscribed('topic', $topic->id)) { + if ($request->user()->subscriptions()->ofTopic($topic->id)->doesntExist()) { $subscription = new Subscription(); $subscription->user_id = $request->user()->id; $subscription->topic_id = $topic->id; @@ -69,8 +69,8 @@ class SubscriptionController extends Controller $params = ['id' => $topic->id]; } - if ($request->user()->isSubscribed('topic', $topic->id)) { - $subscription = $request->user()->subscriptions()->where('topic_id', '=', $topic->id)->first(); + if ($request->user()->subscriptions()->ofTopic($topic->id)->exists()) { + $subscription = $request->user()->subscriptions()->ofTopic($topic->id)->first(); $subscription->delete(); return \to_route($logger, $params) @@ -97,7 +97,7 @@ class SubscriptionController extends Controller $params = ['id' => $forum->id]; } - if (! $request->user()->isSubscribed('forum', $forum->id)) { + if ($request->user()->subscriptions()->ofForum($forum->id)->doesntExist()) { $subscription = new Subscription(); $subscription->user_id = $request->user()->id; $subscription->forum_id = $forum->id; @@ -127,8 +127,8 @@ class SubscriptionController extends Controller $params = ['id' => $forum->id]; } - if ($request->user()->isSubscribed('forum', $forum->id)) { - $subscription = $request->user()->subscriptions()->where('forum_id', '=', $forum->id)->first(); + if ($request->user()->subscriptions()->ofForum($forum->id)->exists()) { + $subscription = $request->user()->subscriptions()->ofForum($forum->id)->first(); $subscription->delete(); return \to_route($logger, $params) diff --git a/app/Models/Follow.php b/app/Models/Follow.php index 21364ffbe..5f59a321a 100644 --- a/app/Models/Follow.php +++ b/app/Models/Follow.php @@ -14,6 +14,7 @@ namespace App\Models; use App\Traits\Auditable; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\Notifiable; @@ -45,4 +46,12 @@ class Follow extends Model 'id' => '1', ]); } + + /** + * Only included follows following a user + */ + public function scopeFollowing($query, $user_id): Builder + { + return $query->where('target_id', '=', $user_id); + } } diff --git a/app/Models/Subscription.php b/app/Models/Subscription.php index 45712504b..a65aea5c6 100644 --- a/app/Models/Subscription.php +++ b/app/Models/Subscription.php @@ -14,6 +14,7 @@ namespace App\Models; use App\Traits\Auditable; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -48,4 +49,20 @@ class Subscription extends Model { return $this->belongsTo(Forum::class); } + + /** + * Only include subscriptions of a forum + */ + public function scopeOfForum($query, $forum_id): Builder + { + return $query->where('forum_id', '=', $forum_id); + } + + /** + * Only include subscriptions of a topic + */ + public function scopeOfTopic($query, $topic_id): Builder + { + return $query->where('topic_id', '=', $topic_id); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 9b87289d8..6dd80b24c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -22,7 +22,6 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; -use Illuminate\Support\Carbon; use Illuminate\Support\Str; use voku\helper\AntiXSS; @@ -119,6 +118,22 @@ class User extends Authenticatable return $this->bookmarks()->where('torrent_id', '=', $torrentId)->first() !== null; } + /** + * Belongs To Many Seeding Torrents. + */ + public function seedingTorrents(): \Illuminate\Database\Eloquent\Relations\BelongsToMany + { + return $this->belongsToMany(Torrent::class, 'history')->wherePivot('active', '=', 1); + } + + /** + * Belongs To Many Seeding Torrents. + */ + public function leechingTorrents(): \Illuminate\Database\Eloquent\Relations\BelongsToMany + { + return $this->belongsToMany(Torrent::class, 'history')->wherePivot('active', '=', 0); + } + /** * Has Many Messages. */ @@ -596,26 +611,6 @@ class User extends Authenticatable return true; } - /** - * Does Subscription Exist. - */ - public function isSubscribed(string $type, int $topicId): bool - { - if ($type === 'topic') { - return (bool) $this->subscriptions()->where('topic_id', '=', $topicId)->first(['id']); - } - - return (bool) $this->subscriptions()->where('forum_id', '=', $topicId)->first(['id']); - } - - /** - * Get All Followers Of A User. - */ - public function isFollowing(int $targetId): bool - { - return (bool) $this->follows()->where('target_id', '=', $targetId)->first(['id']); - } - /** * Return Upload In Human Format. */ @@ -759,121 +754,4 @@ class User extends Authenticatable { return \number_format($this->seedbonus, 0, '.', ','); } - - /** - * @method getSeeding - * - * Gets the amount of torrents a user seeds - */ - public function getSeeding(): int - { - return Peer::where('user_id', '=', $this->id) - ->where('seeder', '=', '1') - ->distinct('torrent_id') - ->count(); - } - - /** - * @method getLast30Uploads - * - * Gets the amount of torrents a user seeds - */ - public function getLast30Uploads(): int - { - $current = Carbon::now(); - - return Torrent::withAnyStatus() - ->where('user_id', '=', $this->id) - ->where('created_at', '>', $current->copy()->subDays(30)->toDateTimeString()) - ->count(); - } - - /** - * @method getUploads - * - * Gets the amount of torrents a user seeds - */ - public function getUploads(): int - { - return Torrent::withAnyStatus() - ->where('user_id', '=', $this->id) - ->count(); - } - - /** - * @method getLeeching - * - * Gets the amount of torrents a user seeds - */ - public function getLeeching(): int - { - return Peer::where('user_id', '=', $this->id) - ->where('left', '>', '0') - ->distinct('torrent_id') - ->count(); - } - - /** - * @method getWarning - * - * Gets count on users active warnings - */ - public function getWarning(): int - { - return Warning::where('user_id', '=', $this->id) - ->whereNotNull('torrent') - ->where('active', '=', '1') - ->count(); - } - - /** - * @method getTotalSeedTime - * - * Gets the users total seedtime - */ - public function getTotalSeedTime(): int - { - return History::where('user_id', '=', $this->id) - ->sum('seedtime'); - } - - /** - * @method getTotalSeedSize - * - * Gets the users total seedsoze - */ - public function getTotalSeedSize(): int - { - $peers = Peer::where('user_id', '=', $this->id)->where('seeder', '=', 1)->pluck('torrent_id'); - - return Torrent::whereIntegerInRaw('id', $peers)->sum('size'); - } - - /** - * @method getCompletedSeeds - * - * Gets the users satisfied torrent count. - */ - public function getCompletedSeeds(): int - { - return History::where('user_id', '=', $this->id)->where('seedtime', '>=', \config('hitrun.seedtime'))->count(); - } - - /** - * @method getSpecialSeedingSize - * - * Gets the seeding size of torrents with at least 15 days seedtime in the past 30 days. - */ - public function getSpecialSeedingSize(): int - { - $current = Carbon::now(); - $seeding = History::where('user_id', '=', $this->id) - ->where('completed_at', '<=', $current->copy()->subDays(30)->toDateTimeString()) - ->where('active', '=', 1) - ->where('seeder', '=', 1) - ->where('seedtime', '>=', 1_296_000) - ->pluck('torrent_id'); - - return Torrent::whereIntergerIn('id', $seeding)->sum('size'); - } } diff --git a/app/Models/Warning.php b/app/Models/Warning.php index d828c324f..01380284d 100644 --- a/app/Models/Warning.php +++ b/app/Models/Warning.php @@ -14,6 +14,7 @@ namespace App\Models; use App\Traits\Auditable; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; @@ -64,4 +65,12 @@ class Warning extends Model 'id' => '1', ]); } + + /** + * Active Warnings + */ + public function scopeActive($query): Builder + { + return $query->where('active', '=', 1); + } } diff --git a/resources/views/blocks/top_uploaders.blade.php b/resources/views/blocks/top_uploaders.blade.php index 1463fcb7e..6be18d8a6 100644 --- a/resources/views/blocks/top_uploaders.blade.php +++ b/resources/views/blocks/top_uploaders.blade.php @@ -67,7 +67,7 @@ - {{ $uploader->user->getUploads() }} + {{ $uploader->value }} - {{ $past_uploader->user->getLast30Uploads() }} + {{ $past_uploader->value }} {{ __('forum.create-new-topic') }} @endif @if ($category->getPermission()->show_forum == true) - @if (auth()->user()->isSubscribed('forum',$forum->id)) + @if (auth()->user()->subscriptions()->ofForum($forum->id)->exists())
@csrf diff --git a/resources/views/forum/subscriptions.blade.php b/resources/views/forum/subscriptions.blade.php index 1ef425fbd..f09bf28da 100644 --- a/resources/views/forum/subscriptions.blade.php +++ b/resources/views/forum/subscriptions.blade.php @@ -86,7 +86,7 @@ -- - @if (auth()->user()->isSubscribed('forum', $r->id)) + @if (auth()->user()->subscriptions()->ofForum($r->id)->exists()) @csrf @@ -153,7 +153,7 @@ @endif - @if (auth()->user()->isSubscribed('topic',$t->id)) + @if (auth()->user()->subscriptions()->ofTopic($t->id)->exists()) {{ __('forum.unsubscribe') }} diff --git a/resources/views/forum/topic.blade.php b/resources/views/forum/topic.blade.php index d70d0d485..bee7fa55d 100644 --- a/resources/views/forum/topic.blade.php +++ b/resources/views/forum/topic.blade.php @@ -37,7 +37,7 @@ {{ date('M d Y H:m', strtotime($topic->created_at)) }} {{ $topic->num_post - 1 }} {{ strtolower(__('forum.replies')) }} {{ $topic->views - 1 }} {{ strtolower(__('forum.views')) }} - @if(auth()->user()->isSubscribed('topic', $topic->id)) + @if(auth()->user()->subscriptions()->ofTopic($topic->id)->exists()) @csrf diff --git a/resources/views/partials/top_nav.blade.php b/resources/views/partials/top_nav.blade.php index 46445107a..89f10350a 100755 --- a/resources/views/partials/top_nav.blade.php +++ b/resources/views/partials/top_nav.blade.php @@ -245,13 +245,13 @@
  • - {{ auth()->user()->getSeeding() }} + {{ auth()->user()->seedingTorrents()->count() }}
  • - {{ auth()->user()->getLeeching() }} + {{ auth()->user()->leechingTorrents()->count() }}
  • @@ -283,7 +283,7 @@ {{ auth()->user()->username }} - @if (auth()->user()->getWarning() > 0) + @if (auth()->user()->warnings()->active()->exists()) @endif @@ -353,7 +353,7 @@ {{ auth()->user()->username }} - @if (auth()->user()->getWarning() > 0) + @if (auth()->user()->warnings()->active()->exists()) @endif diff --git a/resources/views/partials/userbar.blade.php b/resources/views/partials/userbar.blade.php deleted file mode 100644 index 685e750ba..000000000 --- a/resources/views/partials/userbar.blade.php +++ /dev/null @@ -1,106 +0,0 @@ -
    -
    - -
    -
    diff --git a/resources/views/stats/users/leechers.blade.php b/resources/views/stats/users/leechers.blade.php index a75ca1d07..9290785aa 100644 --- a/resources/views/stats/users/leechers.blade.php +++ b/resources/views/stats/users/leechers.blade.php @@ -57,7 +57,7 @@ @endif - {{ $l->user->getLeeching() }} + {{ $l->user->leechingTorrents()->count() }} @endforeach diff --git a/resources/views/stats/users/uploaders.blade.php b/resources/views/stats/users/uploaders.blade.php index 1471abf69..6c26703ac 100644 --- a/resources/views/stats/users/uploaders.blade.php +++ b/resources/views/stats/users/uploaders.blade.php @@ -57,7 +57,7 @@ @endif - {{ $u->user->getUploads() }} + {{ $u->user->torrents()->count() }} @endforeach diff --git a/resources/views/torrent/partials/general.blade.php b/resources/views/torrent/partials/general.blade.php index d52ad1df1..2987649d6 100644 --- a/resources/views/torrent/partials/general.blade.php +++ b/resources/views/torrent/partials/general.blade.php @@ -210,7 +210,7 @@ @endif @if ($torrent->anon !== 1 && $uploader->private_profile !== 1) - @if (auth()->user()->isFollowing($uploader->id)) + @if (auth()->user()->follows()->following($uploader->id)->exists()) @@ -446,7 +446,7 @@ @endif @if ($torrent->anon !== 1 && $uploader->private_profile !== 1) - @if (auth()->user()->isFollowing($uploader->id)) + @if (auth()->user()->follows()->following($uploader->id)->exists()) diff --git a/resources/views/user/buttons/user.blade.php b/resources/views/user/buttons/user.blade.php index b1a76f35a..3b8806070 100755 --- a/resources/views/user/buttons/user.blade.php +++ b/resources/views/user/buttons/user.blade.php @@ -98,7 +98,7 @@ @endif @else