Skip to content

Commit

Permalink
Merge pull request #3895 from Roardom/top-users-livewire
Browse files Browse the repository at this point in the history
(Add) Livewire top users
  • Loading branch information
HDVinnie committed Jun 3, 2024
2 parents 3b67659 + 80b9aa3 commit 348ac11
Show file tree
Hide file tree
Showing 6 changed files with 666 additions and 577 deletions.
142 changes: 6 additions & 136 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,15 @@

use App\Models\Article;
use App\Models\Bookmark;
use App\Models\Comment;
use App\Models\FeaturedTorrent;
use App\Models\FreeleechToken;
use App\Models\Group;
use App\Models\History;
use App\Models\Peer;
use App\Models\Poll;
use App\Models\Post;
use App\Models\Thank;
use App\Models\Topic;
use App\Models\Torrent;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Exception;

/**
Expand Down Expand Up @@ -75,7 +69,7 @@ public function index(Request $request): \Illuminate\Contracts\View\Factory|\Ill
->where('last_action', '>', now()->subMinutes(60))
->orderByRaw('(select position from `groups` where `groups`.id = users.group_id), group_id, username')
->get()
->sortBy(fn ($user) => $user->privacy?->hidden || !$user->isVisible($user, 'other', 'show_online'))
->sortBy(fn ($user) => $user->privacy?->hidden || ! $user->isVisible($user, 'other', 'show_online')),
),
'groups' => cache()->remember(
'user-groups',
Expand All @@ -85,7 +79,7 @@ public function index(Request $request): \Illuminate\Contracts\View\Factory|\Ill
'name',
'color',
'effect',
'icon'
'icon',
])
->oldest('position')
->get()
Expand All @@ -111,141 +105,17 @@ public function index(Request $request): \Illuminate\Contracts\View\Factory|\Ill
->authorized(canReadTopic: true)
->latest()
->take(5)
->get()
->get(),
),
'featured' => cache()->remember(
'latest_featured',
$expiresAt,
fn () => FeaturedTorrent::with([
'torrent' => ['resolution', 'type', 'category'],
'user.group'
])->get()
),
'poll' => cache()->remember('latest_poll', $expiresAt, fn () => Poll::latest()->first()),
'uploaders' => cache()->remember(
'top-users:uploaders',
3_600,
fn () => Torrent::with(['user' , 'user.group'])
->select(DB::raw('user_id, COUNT(user_id) as value'))
->where('user_id', '!=', User::SYSTEM_USER_ID)
->where('anon', '=', false)
->groupBy('user_id')
->orderByDesc('value')
->take(8)
->get()
),
'downloaders' => cache()->remember(
'top-users:downloaders',
3_600,
fn () => History::with(['user' , 'user.group'])
->select(DB::raw('user_id, count(distinct torrent_id) as value'))
->whereNotNull('completed_at')
->where('user_id', '!=', User::SYSTEM_USER_ID)
->groupBy('user_id')
->orderByDesc('value')
->take(8)
->get()
),
'uploaded' => cache()->remember(
'top-users:uploaded',
3_600,
fn () => User::select(['id', 'group_id', 'username', 'uploaded', 'image'])
->where('id', '!=', User::SYSTEM_USER_ID)
->whereNotIn('group_id', Group::select('id')->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->orderByDesc('uploaded')
->take(8)
->get(),
),
'downloaded' => cache()->remember(
'top-users:downloaded',
3_600,
fn () => User::select(['id', 'group_id', 'username', 'downloaded', 'image'])
->where('id', '!=', User::SYSTEM_USER_ID)
->whereNotIn('group_id', Group::select('id')->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->orderByDesc('downloaded')
->take(8)
->get(),
),
'seeders' => cache()->remember(
'top-users:seeders',
3_600,
fn () => Peer::with(['user' , 'user.group'])
->select(DB::raw('user_id, count(distinct torrent_id) as value'))
->where('user_id', '!=', User::SYSTEM_USER_ID)
->where('seeder', '=', 1)
->where('active', '=', 1)
->groupBy('user_id')
->orderByDesc('value')
->take(8)
->get(),
),
'seedtimes' => cache()->remember(
'top-users:seedtimes',
3_600,
fn () => User::withSum('history as seedtime', 'seedtime')
->where('id', '!=', User::SYSTEM_USER_ID)
->whereNotIn('group_id', Group::select('id')->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->orderByDesc('seedtime')
->take(8)
->get(),
),
'served' => cache()->remember(
'top-users:served',
3_600,
fn () => User::withCount('uploadSnatches')
->where('id', '!=', User::SYSTEM_USER_ID)
->whereNotIn('group_id', Group::select('id')->whereIn('slug', ['banned', 'validating', 'disabled', 'pruned']))
->orderByDesc('upload_snatches_count')
->take(8)
->get(),
),
'commenters' => cache()->remember(
'top-users:commenters',
3_600,
fn () => Comment::with(['user' , 'user.group'])
->select(DB::raw('user_id, COUNT(user_id) as value'))
->where('user_id', '!=', User::SYSTEM_USER_ID)
->where('anon', '=', false)
->groupBy('user_id')
->orderByRaw('COALESCE(value, 0) DESC')
->take(8)
->get()
),
'posters' => cache()->remember(
'top-users:posters',
3_600,
fn () => Post::with(['user' , 'user.group'])
->select(DB::raw('user_id, COUNT(user_id) as value'))
->where('user_id', '!=', User::SYSTEM_USER_ID)
->groupBy('user_id')
->orderByRaw('COALESCE(value, 0) DESC')
->take(8)
->get()
),
'thankers' => cache()->remember(
'top-users:thankers',
3_600,
fn () => Thank::with(['user' , 'user.group'])
->select(DB::raw('user_id, COUNT(user_id) as value'))
->where('user_id', '!=', User::SYSTEM_USER_ID)
->groupBy('user_id')
->orderByRaw('COALESCE(value, 0) DESC')
->take(8)
->get()
),
'personals' => cache()->remember(
'top-users:personals',
3_600,
fn () => Torrent::with(['user' , 'user.group'])
->select(DB::raw('user_id, COUNT(user_id) as value'))
->where('user_id', '!=', User::SYSTEM_USER_ID)
->where('anon', '=', false)
->where('personal_release', '=', 1)
->groupBy('user_id')
->orderByDesc('value')
->take(8)
->get()
'user.group',
])->get(),
),
'poll' => cache()->remember('latest_poll', $expiresAt, fn () => Poll::latest()->first()),
'freeleech_tokens' => FreeleechToken::where('user_id', $user->id)->get(),
'bookmarks' => Bookmark::where('user_id', $user->id)->get(),
]);
Expand Down
199 changes: 199 additions & 0 deletions app/Http/Livewire/TopUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php

declare(strict_types=1);

namespace App\Http\Livewire;

use App\Models\Comment;
use App\Models\Group;
use App\Models\History;
use App\Models\Peer;
use App\Models\Post;
use App\Models\Thank;
use App\Models\Torrent;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Livewire\Attributes\Computed;
use Livewire\Component;

class TopUsers extends Component
{
public string $tab = 'uploaders';

/**
* @return \Illuminate\Support\Collection<int, Torrent>
*/
#[Computed(cache: true)]
final public function uploaders(): \Illuminate\Support\Collection
{
return Torrent::with(['user.group'])
->select(DB::raw('user_id, COUNT(user_id) as value'))
->where('user_id', '!=', User::SYSTEM_USER_ID)
->where('anon', '=', false)
->groupBy('user_id')
->orderByDesc('value')
->take(8)
->get();
}

/**
* @return \Illuminate\Support\Collection<int, History>
*/
#[Computed(cache: true)]
final public function downloaders(): \Illuminate\Support\Collection
{
return History::with(['user.group'])
->select(DB::raw('user_id, count(distinct torrent_id) as value'))
->whereNotNull('completed_at')
->where('user_id', '!=', User::SYSTEM_USER_ID)
->groupBy('user_id')
->orderByDesc('value')
->take(8)
->get();
}

/**
* @return \Illuminate\Support\Collection<int, User>
*/
#[Computed(cache: true)]
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']))
->orderByDesc('uploaded')
->take(8)
->get();
}

/**
* @return \Illuminate\Support\Collection<int, User>
*/
#[Computed(cache: true)]
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']))
->orderByDesc('downloaded')
->take(8)
->get();
}

/**
* @return \Illuminate\Support\Collection<int, Peer>
*/
#[Computed(cache: true)]
final public function seeders(): \Illuminate\Support\Collection
{
return Peer::with(['user.group'])
->select(DB::raw('user_id, count(distinct torrent_id) as value'))
->where('user_id', '!=', User::SYSTEM_USER_ID)
->where('seeder', '=', 1)
->where('active', '=', 1)
->groupBy('user_id')
->orderByDesc('value')
->take(8)
->get();
}

/**
* @return \Illuminate\Support\Collection<int, User>
*/
#[Computed(cache: true)]
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']))
->orderByDesc('seedtime')
->take(8)
->get();
}

/**
* @return \Illuminate\Support\Collection<int, User>
*/
#[Computed(cache: true)]
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']))
->orderByDesc('upload_snatches_count')
->take(8)
->get();
}

/**
* @return \Illuminate\Support\Collection<int, Comment>
*/
#[Computed(cache: true)]
final public function commenters(): \Illuminate\Support\Collection
{
return Comment::with(['user.group'])
->select(DB::raw('user_id, COUNT(user_id) as value'))
->where('user_id', '!=', User::SYSTEM_USER_ID)
->where('anon', '=', false)
->groupBy('user_id')
->orderByRaw('COALESCE(value, 0) DESC')
->take(8)
->get();
}

/**
* @return \Illuminate\Support\Collection<int, Post>
*/
#[Computed(cache: true)]
final public function posters(): \Illuminate\Support\Collection
{
return Post::with(['user.group'])
->select(DB::raw('user_id, COUNT(user_id) as value'))
->where('user_id', '!=', User::SYSTEM_USER_ID)
->groupBy('user_id')
->orderByRaw('COALESCE(value, 0) DESC')
->take(8)
->get();
}

/**
* @return \Illuminate\Support\Collection<int, Thank>
*/
#[Computed(cache: true)]
final public function thankers(): \Illuminate\Support\Collection
{
return Thank::with(['user.group'])
->select(DB::raw('user_id, COUNT(user_id) as value'))
->where('user_id', '!=', User::SYSTEM_USER_ID)
->groupBy('user_id')
->orderByRaw('COALESCE(value, 0) DESC')
->take(8)
->get();
}

/**
* @return \Illuminate\Support\Collection<int, Torrent>
*/
#[Computed(cache: true)]
final public function personals(): \Illuminate\Support\Collection
{
return Torrent::with(['user.group'])
->select(DB::raw('user_id, COUNT(user_id) as value'))
->where('user_id', '!=', User::SYSTEM_USER_ID)
->where('anon', '=', false)
->where('personal_release', '=', 1)
->groupBy('user_id')
->orderByDesc('value')
->take(8)
->get();
}

public function render(): \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application
{
return view('livewire.top-users');
}
}
Loading

0 comments on commit 348ac11

Please sign in to comment.