Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Add) Filters On User History View #510

Merged
merged 22 commits into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 62 additions & 10 deletions app/Http/Controllers/TorrentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,30 @@ public function faceted(Request $request, Torrent $torrent)
$repository = $this->faceted;
$personal_freeleech = PersonalFreeleech::where('user_id', '=', $user->id)->first();
$collection = null;
$seedling = null;
$notdownloaded = null;
$downloaded = null;
$leeching = null;
$idling = null;

if ($request->has('view') && $request->input('view') == 'group') {
$collection = 1;
}
if ($request->has('seeding') && $request->input('seeding') != null) {
$seedling = 1;
}
if ($request->has('notdownloaded') && $request->input('notdownloaded') != null) {
$notdownloaded = 1;
}
if ($request->has('downloaded') && $request->input('downloaded') != null) {
$downloaded = 1;
}
if ($request->has('leeching') && $request->input('leeching') != null) {
$leeching = 1;
}
if ($request->has('idling') && $request->input('idling') != null) {
$idling = 1;
}

$search = $request->input('search');
$description = $request->input('description');
Expand Down Expand Up @@ -481,17 +501,49 @@ public function faceted(Request $request, Torrent $torrent)
$torrent->where('torrentsl.seeders', '=', $dead);
}
} else {
$torrent = $torrent->with(['user', 'category'])->withCount(['thanks', 'comments']);
if ($seedling == 1 || $notdownloaded == 1 || $downloaded == 1 || $leeching == 1 || $idling == 1) {
$torrent = $torrent->distinct()->select('torrents.id')->selectRaw('historyl.seeder as seeding,torrents.*,torrents.user_id as creator')->with(['uploader', 'category'])->withCount(['thanks', 'comments'])->leftJoin('history as historyl', 'torrents.info_hash', '=', 'historyl.info_hash');

$torrent->orWhere(function ($query) use ($user,$seedling,$notdownloaded,$downloaded,$leeching,$idling) {
if ($seedling == 1) {
$query->orWhere(function ($query) use ($user) {
$query->where('historyl.user_id', '=', $user->id)->where('historyl.seeder', '=', '1');
});
}
if ($notdownloaded == 1) {
$query->orWhere(function ($query) use ($user) {
$query->whereRaw('historyl.id is null');
});
}
if ($downloaded == 1) {
$query->orWhere(function ($query) use ($user) {
$query->where('historyl.user_id', '=', $user->id)->whereRaw('(historyl.seeder = ? OR historyl.completed_at is not null)', [1]);
});
}
if ($leeching == 1) {
$query->orWhere(function ($query) use ($user) {
$query->where('historyl.user_id', '=', $user->id)->whereRaw('(historyl.active = ? AND (historyl.completed_at is null OR historyl.seeder = 0))', [1]);
});
}
if ($idling == 1) {
$query->orWhere(function ($query) use ($user) {
$query->where('historyl.user_id', '=', $user->id)->whereRaw('(historyl.active = ? AND (historyl.completed_at is null OR historyl.seeder = ?))', [0, 1]);
});
}
});
} else {
$torrent = $torrent->distinct()->select('torrents.id')->selectRaw('historyl.seeder as seeding,torrents.*,torrents.user_id as creator')->with(['uploader', 'category'])->withCount(['thanks', 'comments'])->leftJoin('history as historyl', 'torrents.info_hash', '=', 'historyl.info_hash');
}

if ($request->has('search') && $request->input('search') != null) {
$torrent->where(function ($query) use ($search) {
$query->where('name', 'like', $search);
$query->where('torrents.name', 'like', $search);
});
}

if ($request->has('description') && $request->input('description') != null) {
$torrent->where(function ($query) use ($description) {
$query->where('description', 'like', $description)->orWhere('mediainfo', 'like', $description);
$query->where('torrents.description', 'like', $description)->orWhere('mediainfo', 'like', $description);
});
}

Expand All @@ -500,7 +552,7 @@ public function faceted(Request $request, Torrent $torrent)
if (null === $match) {
return ['result' => [], 'count' => 0];
}
$torrent->where('user_id', '=', $match->id)->where('anon', '=', 0);
$torrent->where('torrents.user_id', '=', $match->id)->where('anon', '=', 0);
}

if ($request->has('imdb') && $request->input('imdb') != null) {
Expand Down Expand Up @@ -529,7 +581,7 @@ public function faceted(Request $request, Torrent $torrent)

if ($request->has('genres') && $request->input('genres') != null) {
$genreID = TagTorrent::distinct()->select('torrent_id')->whereIn('tag_name', $genres)->get();
$torrent->whereIn('id', $genreID);
$torrent->whereIn('torrents.id', $genreID);
}

if ($request->has('freeleech') && $request->input('freeleech') != null) {
Expand Down Expand Up @@ -561,15 +613,15 @@ public function faceted(Request $request, Torrent $torrent)
}

if ($request->has('alive') && $request->input('alive') != null) {
$torrent->where('seeders', '>=', $alive);
$torrent->where('torrents.seeders', '>=', $alive);
}

if ($request->has('dying') && $request->input('dying') != null) {
$torrent->where('seeders', '=', $dying)->where('times_completed', '>=', 3);
$torrent->where('torrents.seeders', '=', $dying)->where('times_completed', '>=', 3);
}

if ($request->has('dead') && $request->input('dead') != null) {
$torrent->where('seeders', '=', $dead);
$torrent->where('torrents.seeders', '=', $dead);
}
}

Expand Down Expand Up @@ -601,7 +653,7 @@ public function faceted(Request $request, Torrent $torrent)
}
$totals = [];
$counts = [];
$launcher = Torrent::with(['user', 'category'])->withCount(['thanks', 'comments'])->whereIn('imdb', $fed)->orderBy($sorting, $order);
$launcher = Torrent::with(['uploader', 'category'])->withCount(['thanks', 'comments'])->whereIn('imdb', $fed)->orderBy($sorting, $order);
foreach ($launcher->cursor() as $chunk) {
if ($chunk->imdb) {
if (! array_key_exists($chunk->imdb, $totals)) {
Expand Down Expand Up @@ -650,7 +702,7 @@ public function faceted(Request $request, Torrent $torrent)
$torrents = null;
}
} else {
$torrents = $torrent->orderBy('sticky', 'desc')->orderBy($sorting, $order)->paginate($qty);
$torrents = $torrent->orderBy('torrents.sticky', 'desc')->orderBy('torrents.'.$sorting, $order)->paginate($qty);
}
if ($collection == 1 && is_array($torrents)) {
$client = new \App\Services\MovieScrapper(config('api-keys.tmdb'), config('api-keys.tvdb'), config('api-keys.omdb'));
Expand Down
85 changes: 85 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,91 @@ public function myActive($username, $id)
return view('user.active', ['user' => $user, 'active' => $active]);
}

/**
* Uses Input's To Put Together A Filtered View.
*
* @param \Illuminate\Http\Request $request
* @param $username
* @param $id
*
* @return array
*/
public function myFilter(Request $request, $username, $id)
{
$user = User::findOrFail($id);
abort_unless(auth()->user()->group->is_modo || auth()->user()->id == $user->id, 403);

if ($request->has('view') && $request->input('view') == 'history') {
$his_upl = History::where('user_id', '=', $id)->sum('actual_uploaded');
$his_upl_cre = History::where('user_id', '=', $id)->sum('uploaded');
$his_downl = History::where('user_id', '=', $id)->sum('actual_downloaded');
$his_downl_cre = History::where('user_id', '=', $id)->sum('downloaded');
$history = History::with(['torrent' => function ($query) {
$query->withAnyStatus();
}])->leftJoin('torrents as torrents', 'torrents.info_hash', '=', 'history.info_hash');

$order = null;
$sorting = null;
if ($request->has('sorting') && $request->input('sorting') != null) {
$sorting = $request->input('sorting');
}
if ($request->has('direction') && $request->input('direction') != null) {
$order = $request->input('direction');
}
if (! $sorting || $sorting == null || ! $order || $order == null) {
$sorting = 'created_at';
$order = 'desc';
// $order = 'asc';
}
if ($order == 'asc') {
$direction = 1;
} else {
$direction = 2;
}

if ($request->has('completed') && $request->input('completed') != null) {
$history->where('completed_at', '>', 0);
}

if ($request->has('active') && $request->input('active') != null) {
$history->where('active', '=', 1);
}

if ($request->has('seeding') && $request->input('seeding') != null) {
$history->where('seeder', '=', 1);
}

if ($request->has('prewarned') && $request->input('prewarned') != null) {
$history->where('prewarn', '=', 1);
}

if ($request->has('hr') && $request->input('hr') != null) {
$history->where('hitrun', '=', 1);
}

if ($request->has('immune') && $request->input('immune') != null) {
$history->where('immune', '=', 1);
}

if ($sorting != 'name') {
$table = $history->where('history.user_id', '=', $user->id)->orderBy('history.'.$sorting, $order)->paginate(50);
} else {
$table = $history->where('history.user_id', '=', $user->id)->orderBy('torrents.'.$sorting, $order)->paginate(50);
}

return view('user.filters', [
'user' => $user,
'history' => $table,
'his_upl' => $his_upl,
'his_upl_cre' => $his_upl_cre,
'his_downl' => $his_downl,
'his_downl_cre' => $his_downl_cre,
])->render();
}

return false;
}

/**
* Get A Users History Table.
*
Expand Down
13 changes: 13 additions & 0 deletions app/Torrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ public function user()
]);
}

/**
* Belongs To A Uploader.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function uploader()
{
return $this->belongsTo(User::class, 'creator')->withDefault([
'username' => 'System',
'id' => '1',
]);
}

/**
* Belongs To A Category.
*
Expand Down
Loading