Files
UNIT3D-Community-Edition/app/Http/Controllers/RequestController.php
Roardom 0000838d8b refactor: cruddy torrents
Separate torrents into cruddy route names. Route model binding isn't possible due to the global scope on unapproved torrents. Use form requests. Simplify the controller flow where possible.
2023-07-07 03:18:13 +00:00

227 lines
7.7 KiB
PHP

<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Http\Controllers;
use App\Http\Requests\StoreTorrentRequestRequest;
use App\Http\Requests\UpdateTorrentRequestRequest;
use App\Models\BonTransactions;
use App\Models\Category;
use App\Models\Movie;
use App\Models\Resolution;
use App\Models\Torrent;
use App\Models\TorrentRequest;
use App\Models\TorrentRequestBounty;
use App\Models\Tv;
use App\Models\Type;
use App\Repositories\ChatRepository;
use App\Services\Tmdb\TMDBScraper;
use Illuminate\Http\Request;
use MarcReichel\IGDBLaravel\Models\Game;
use Exception;
/**
* @see \Tests\Todo\Feature\Http\Controllers\RequestControllerTest
*/
class RequestController extends Controller
{
/**
* RequestController Constructor.
*/
public function __construct(private readonly ChatRepository $chatRepository)
{
}
/**
* Display a listing of the resource.
*/
public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
{
return view('requests.index');
}
/**
* Display The Torrent Request.
*/
public function show(Request $request, TorrentRequest $torrentRequest): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
{
return view('requests.show', [
'torrentRequest' => $torrentRequest->load(['category', 'claim' => ['user'], 'bounties', 'torrent']),
'user' => $request->user(),
'meta' => match (true) {
($torrentRequest->category->tv_meta && ($torrentRequest->tmdb || $torrentRequest->tmdb != 0)) => Tv::with([
'genres',
'credits' => ['person', 'occupation'],
'networks',
'seasons'
])
->find($torrentRequest->tmdb),
($torrentRequest->category->movie_meta && ($torrentRequest->tmdb || $torrentRequest->tmdb != 0)) => Movie::with([
'genres',
'credits' => ['person', 'occupation'],
'companies',
'collection'
])
->find($torrentRequest->tmdb),
($torrentRequest->category->game_meta && ($torrentRequest->igdb || $torrentRequest->igdb != 0)) => Game::with([
'cover' => ['url', 'image_id'],
'artworks' => ['url', 'image_id'],
'genres' => ['name'],
'videos' => ['video_id', 'name'],
'involved_companies.company',
'involved_companies.company.logo',
'platforms',
])
->find($torrentRequest->igdb),
default => null,
},
]);
}
/**
* Torrent Request Add Form.
*/
public function create(Request $request): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
{
return view('requests.create', [
'categories' => Category::orderBy('position')->get(),
'types' => Type::orderBy('position')->get(),
'resolutions' => Resolution::orderBy('position')->get(),
'user' => $request->user(),
'category_id' => $request->category_id,
'title' => urldecode($request->title),
'imdb' => $request->imdb,
'tmdb' => $request->tmdb,
'mal' => $request->mal,
'tvdb' => $request->tvdb,
'igdb' => $request->igdb,
]);
}
/**
* Store A New Torrent Request.
*/
public function store(StoreTorrentRequestRequest $request): \Illuminate\Http\RedirectResponse
{
$user = $request->user();
$torrentRequest = TorrentRequest::create(['user_id' => $request->user()->id, 'votes' => 1] + $request->validated());
TorrentRequestBounty::create([
'user_id' => $user->id,
'seedbonus' => $request->bounty,
'requests_id' => $torrentRequest->id,
'anon' => $request->anon,
]);
BonTransactions::create([
'itemID' => 0,
'name' => 'request',
'cost' => $request->bounty,
'sender' => $user->id,
'comment' => sprintf('new request - %s', $request->name),
]);
$user->decrement('seedbonus', $request->bounty);
// Auto Shout
if ($torrentRequest->anon == 0) {
$this->chatRepository->systemMessage(
sprintf('[url=%s]%s[/url] has created a new request [url=%s]%s[/url]', href_profile($user), $user->username, href_request($torrentRequest), $torrentRequest->name)
);
} else {
$this->chatRepository->systemMessage(
sprintf('An anonymous user has created a new request [url=%s]%s[/url]', href_request($torrentRequest), $torrentRequest->name)
);
}
$category = $torrentRequest->category;
if ($torrentRequest->tmdb != 0) {
switch (true) {
case $category->tv_meta:
(new TMDBScraper())->tv($torrentRequest->tmdb);
break;
case $category->movie_meta:
(new TMDBScraper())->movie($torrentRequest->tmdb);
break;
}
}
return to_route('requests.index')
->withSuccess(trans('request.added-request'));
}
/**
* Torrent Request Edit Form.
*/
public function edit(Request $request, TorrentRequest $torrentRequest): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
{
return view('requests.edit', [
'categories' => Category::orderBy('position')->get(),
'types' => Type::orderBy('position')->get(),
'resolutions' => Resolution::orderBy('position')->get(),
'user' => $request->user(),
'torrentRequest' => $torrentRequest,
]);
}
/**
* Edit A Torrent Request.
*/
public function update(UpdateTorrentRequestRequest $request, TorrentRequest $torrentRequest): \Illuminate\Http\RedirectResponse
{
$user = $request->user();
abort_unless($user->group->is_modo || $user->id === $torrentRequest->user_id, 403);
$torrentRequest->update($request->validated());
if ($torrentRequest->tmdb != 0) {
switch (true) {
case $torrentRequest->category->tv_meta:
(new TMDBScraper())->tv($torrentRequest->tmdb);
break;
case $torrentRequest->category->movie_meta:
(new TMDBScraper())->movie($torrentRequest->tmdb);
break;
}
}
return to_route('requests.show', ['torrentRequest' => $torrentRequest])
->withSuccess(trans('request.edited-request'));
}
/**
* Delete A Torrent Request.
*
* @throws Exception
*/
public function destroy(Request $request, TorrentRequest $torrentRequest): \Illuminate\Http\RedirectResponse
{
$user = $request->user();
abort_unless($user->group->is_modo || $torrentRequest->user_id === $user->id, 403);
$torrentRequest->bounties()->delete();
$torrentRequest->delete();
return to_route('requests.index')
->withSuccess(sprintf(trans('request.deleted'), $torrentRequest->name));
}
}