refactor: request controller

This commit is contained in:
Roardom
2023-04-23 03:56:45 +00:00
parent 1520180a08
commit 34c5abf019
5 changed files with 174 additions and 174 deletions
+146 -174
View File
@@ -143,24 +143,7 @@ class RequestController extends Controller
{
$user = $request->user();
$category = Category::findOrFail($request->input('category_id'));
$torrentRequest = new TorrentRequest();
$torrentRequest->name = $request->input('name');
$torrentRequest->description = $request->input('description');
$torrentRequest->category_id = $category->id;
$torrentRequest->user_id = $user->id;
$torrentRequest->imdb = $request->input('imdb');
$torrentRequest->tvdb = $request->input('tvdb');
$torrentRequest->tmdb = $request->input('tmdb');
$torrentRequest->mal = $request->input('mal');
$torrentRequest->igdb = $request->input('igdb');
$torrentRequest->type_id = $request->input('type_id');
$torrentRequest->resolution_id = $request->input('resolution_id');
$torrentRequest->bounty = $request->input('bounty');
$torrentRequest->votes = 1;
$torrentRequest->anon = $request->input('anon');
$v = validator($torrentRequest->toArray(), [
$request->validate([
'name' => 'required|max:180',
'imdb' => 'required|numeric',
'tvdb' => 'required|numeric',
@@ -175,48 +158,63 @@ class RequestController extends Controller
'anon' => 'required',
]);
if ($v->fails()) {
return to_route('requests.index')
->withErrors($v->errors())->withInput();
}
$category = Category::findOrFail($request->category_id);
$torrentRequest->save();
$torrentRequest = TorrentRequest::create([
'name' => $request->name,
'description' => $request->description,
'category_id' => $category->id,
'user_id' => $user->id,
'imdb' => $request->imdb,
'tvdb' => $request->tvdb,
'tmdb' => $request->tmdb,
'mal' => $request->mal,
'igdb' => $request->igdb,
'type_id' => $request->type_id,
'resolution_id' => $request->resolution_id,
'bounty' => $request->bounty,
'votes' => 1,
'anon' => $request->anon,
]);
$tmdbScraper = new TMDBScraper();
if ($torrentRequest->category->tv_meta !== 0 && ($torrentRequest->tmdb || $torrentRequest->tmdb != 0)) {
if ($torrentRequest->category->tv_meta !== 0 && $torrentRequest->tmdb != 0) {
$tmdbScraper->tv($torrentRequest->tmdb);
}
if ($torrentRequest->category->movie_meta !== 0 && ($torrentRequest->tmdb || $torrentRequest->tmdb != 0)) {
if ($torrentRequest->category->movie_meta !== 0 && $torrentRequest->tmdb != 0) {
$tmdbScraper->movie($torrentRequest->tmdb);
}
$torrentRequestBounty = new TorrentRequestBounty();
$torrentRequestBounty->user_id = $user->id;
$torrentRequestBounty->seedbonus = $request->input('bounty');
$torrentRequestBounty->requests_id = $torrentRequest->id;
$torrentRequestBounty->anon = $request->input('anon');
$torrentRequestBounty->save();
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);
$requestUrl = href_request($torrentRequest);
$userUrl = href_profile($user);
$BonTransactions = new BonTransactions();
$BonTransactions->itemID = 0;
$BonTransactions->name = 'request';
$BonTransactions->cost = $request->input('bounty');
$BonTransactions->sender = $user->id;
$BonTransactions->comment = sprintf('new request - %s', $request->input('name'));
$BonTransactions->save();
$user->seedbonus -= $request->input('bounty');
$user->save();
$trUrl = href_request($torrentRequest);
$profileUrl = href_profile($user);
// Auto Shout
if ($torrentRequest->anon == 0) {
$this->chatRepository->systemMessage(
sprintf('[url=%s]%s[/url] has created a new request [url=%s]%s[/url]', $profileUrl, $user->username, $trUrl, $torrentRequest->name)
sprintf('[url=%s]%s[/url] has created a new request [url=%s]%s[/url]', $userUrl, $user->username, $requestUrl, $torrentRequest->name)
);
} else {
$this->chatRepository->systemMessage(
sprintf('An anonymous user has created a new request [url=%s]%s[/url]', $trUrl, $torrentRequest->name)
sprintf('An anonymous user has created a new request [url=%s]%s[/url]', $requestUrl, $torrentRequest->name)
);
}
@@ -247,34 +245,10 @@ class RequestController extends Controller
{
$user = $request->user();
$torrentRequest = TorrentRequest::findOrFail($id);
abort_unless($user->group->is_modo || $user->id === $torrentRequest->user_id, 403);
// Find the right category
$name = $request->input('name');
$imdb = $request->input('imdb');
$tvdb = $request->input('tvdb');
$tmdb = $request->input('tmdb');
$mal = $request->input('mal');
$igdb = $request->input('igdb');
$category = $request->input('category_id');
$type = $request->input('type_id');
$resolution = $request->input('resolution_id');
$description = $request->input('description');
$anon = $request->input('anon');
$torrentRequest->name = $name;
$torrentRequest->imdb = $imdb;
$torrentRequest->tvdb = $tvdb;
$torrentRequest->tmdb = $tmdb;
$torrentRequest->mal = $mal;
$torrentRequest->igdb = $igdb;
$torrentRequest->category_id = $category;
$torrentRequest->type_id = $type;
$torrentRequest->resolution_id = $resolution;
$torrentRequest->description = $description;
$torrentRequest->anon = $anon;
$v = validator($torrentRequest->toArray(), [
$request->validate([
'name' => 'required|max:180',
'imdb' => 'required|numeric',
'tvdb' => 'required|numeric',
@@ -288,19 +262,27 @@ class RequestController extends Controller
'anon' => 'required',
]);
if ($v->fails()) {
return to_route('requests.index')
->withErrors($v->errors());
}
$torrentRequest->save();
$torrentRequest->update([
'name' => $request->name,
'imdb' => $request->imdb,
'tvdb' => $request->tvdb,
'tmdb' => $request->tmdb,
'mal' => $request->mal,
'igdb' => $request->igdb,
'category_id' => $request->category_id,
'type_id' => $request->type_id,
'resolution_id' => $request->resolution_id,
'description' => $request->description,
'anon' => $request->anon,
]);
$tmdbScraper = new TMDBScraper();
if ($torrentRequest->category->tv_meta && ($torrentRequest->tmdb || $torrentRequest->tmdb != 0)) {
if ($torrentRequest->category->tv_meta && $torrentRequest->tmdb != 0) {
$tmdbScraper->tv($torrentRequest->tmdb);
}
if ($torrentRequest->category->movie_meta && ($torrentRequest->tmdb || $torrentRequest->tmdb != 0)) {
if ($torrentRequest->category->movie_meta && $torrentRequest->tmdb != 0) {
$tmdbScraper->movie($torrentRequest->tmdb);
}
@@ -315,35 +297,35 @@ class RequestController extends Controller
{
$user = $request->user();
$torrentRequest = TorrentRequest::findOrFail($id);
$torrentRequest->filled_by = $user->id;
$torrentRequest->torrent_id = basename($request->input('torrent_id'));
$torrentRequest->filled_when = Carbon::now();
$torrentRequest->filled_anon = $request->input('filled_anon');
$v = validator($request->all(), [
$request->validate([
'request_id' => 'required|exists:requests,id',
'torrent_id' => 'required|exists:torrents,id',
'torrent_id' => 'required',
'filled_anon' => 'required',
]);
if ($v->fails()) {
return to_route('requests.show', ['id' => $request->input('request_id')])
->withErrors($v->errors());
}
$torrent_id = basename($request->torrent_id);
$torrent = Torrent::withAnyStatus()->where('id', '=', $torrent_id)->first();
$torrent = Torrent::withAnyStatus()->where('id', '=', $torrentRequest->torrent_id)->first();
if ($torrent->isApproved() === false) {
return to_route('requests.show', ['id' => $request->input('request_id')])
->withErrors(trans('request.pending-moderation'));
}
$torrentRequest->save();
$torrentRequest = TorrentRequest::findOrFail($id);
$torrentRequest->update([
'filled_by' => $user->id,
'torrent_id' => $torrent_id,
'filled_when' => Carbon::now(),
'filled_anon' => $request->filled_anon,
]);
// Send Private Message
$sender = $request->input('filled_anon') == 1 ? 'Anonymous' : $user->username;
$senderUsername = $request->filled_anon ? 'Anonymous' : $user->username;
$requester = $torrentRequest->user;
if ($requester->acceptsNotification($request->user(), $requester, 'request', 'show_request_fill')) {
$requester->notify(new NewRequestFill('torrent', $sender, $torrentRequest));
$requester->notify(new NewRequestFill('torrent', $senderUsername, $torrentRequest));
}
return to_route('requests.show', ['id' => $request->input('request_id')])
@@ -355,71 +337,65 @@ class RequestController extends Controller
*/
public function approve(Request $request, int $id): \Illuminate\Http\RedirectResponse
{
$user = $request->user();
$approver = $request->user();
$tr = TorrentRequest::findOrFail($id);
if ($user->id == $tr->user_id || $request->user()->group->is_modo) {
if ($tr->approved_by != null) {
return to_route('requests.show', ['id' => $id])
->withErrors(trans('request.already-approved'));
}
abort_unless($approver->id == $tr->user_id || $request->user()->group->is_modo, 403);
$tr->approved_by = $user->id;
$tr->approved_when = Carbon::now();
$tr->save();
if ($tr->approved_by !== null) {
return to_route('requests.show', ['id' => $id])
->withErrors(trans('request.already-approved'));
}
//BON and torrent request hash code below
$fillUser = User::findOrFail($tr->filled_by);
$fillAmount = $tr->bounty;
$tr->update([
'approved_by' => $approver->id,
'approved_when' => Carbon::now(),
]);
$BonTransactions = new BonTransactions();
$BonTransactions->itemID = 0;
$BonTransactions->name = 'request';
$BonTransactions->cost = $fillAmount;
$BonTransactions->receiver = $fillUser->id;
$BonTransactions->comment = sprintf('%s has filled %s and has been awarded %s BONUS.', $fillUser->username, $tr->name, $fillAmount);
$BonTransactions->save();
$filler = User::findOrFail($tr->filled_by);
$fillAmount = $tr->bounty;
$fillUser->seedbonus += $fillAmount;
$fillUser->save();
BonTransactions::create([
'itemID' => 0,
'name' => 'request',
'cost' => $fillAmount,
'receiver' => $filler->id,
'comment' => sprintf('%s has filled %s and has been awarded %s BONUS.', $filler->username, $tr->name, $fillAmount),
]);
// Achievements
$fillUser->addProgress(new UserFilled25Requests(), 1);
$fillUser->addProgress(new UserFilled50Requests(), 1);
$fillUser->addProgress(new UserFilled75Requests(), 1);
$fillUser->addProgress(new UserFilled100Requests(), 1);
$filler->increment('seedbonus', $fillAmount);
$trUrl = href_request($tr);
$profileUrl = href_profile($fillUser);
// Achievements
$filler->addProgress(new UserFilled25Requests(), 1);
$filler->addProgress(new UserFilled50Requests(), 1);
$filler->addProgress(new UserFilled75Requests(), 1);
$filler->addProgress(new UserFilled100Requests(), 1);
// Auto Shout
if ($tr->filled_anon == 0) {
$this->chatRepository->systemMessage(
sprintf('[url=%s]%s[/url] has filled request, [url=%s]%s[/url]', $profileUrl, $fillUser->username, $trUrl, $tr->name)
);
} else {
$this->chatRepository->systemMessage(
sprintf('An anonymous user has filled request, [url=%s]%s[/url]', $trUrl, $tr->name)
);
}
$requestUrl = href_request($tr);
$userUrl = href_profile($filler);
$requester = $fillUser;
if ($requester->acceptsNotification($request->user(), $requester, 'request', 'show_request_fill_approve')) {
$requester->notify(new NewRequestFillApprove('torrent', $user->username, $tr));
}
// Auto Shout
if ($tr->filled_anon) {
$this->chatRepository->systemMessage(
sprintf('An anonymous user has filled request, [url=%s]%s[/url]', $requestUrl, $tr->name)
);
} else {
$this->chatRepository->systemMessage(
sprintf('[url=%s]%s[/url] has filled request, [url=%s]%s[/url]', $userUrl, $filler->username, $requestUrl, $tr->name)
);
}
if ($tr->filled_anon == 0) {
return to_route('requests.show', ['id' => $id])
->withSuccess(sprintf(trans('request.approved-user'), $tr->name, $fillUser->username));
}
if ($filler->acceptsNotification($approver, $filler, 'request', 'show_request_fill_approve')) {
$filler->notify(new NewRequestFillApprove('torrent', $approver->username, $tr));
}
if ($tr->filled_anon) {
return to_route('requests.show', ['id' => $id])
->withSuccess(sprintf(trans('request.approved-anon'), $tr->name));
}
return to_route('requests.show', ['id' => $id])
->withErrors(trans('request.access-error'));
->withSuccess(sprintf(trans('request.approved-user'), $tr->name, $filler->username));
}
/**
@@ -430,28 +406,27 @@ class RequestController extends Controller
$user = $request->user();
$torrentRequest = TorrentRequest::findOrFail($id);
if ($user->id == $torrentRequest->user_id) {
if ($torrentRequest->approved_by != null) {
return to_route('requests.show', ['id' => $id])
->withErrors(trans('request.already-rejected'));
}
$requester = User::findOrFail($torrentRequest->filled_by);
if ($requester->acceptsNotification($request->user(), $requester, 'request', 'show_request_fill_reject')) {
$requester->notify(new NewRequestFillReject('torrent', $user->username, $torrentRequest));
}
$torrentRequest->filled_by = null;
$torrentRequest->filled_when = null;
$torrentRequest->torrent_id = null;
$torrentRequest->save();
abort_unless($user->id === $torrentRequest->user_id || $user->group->is_modo, 403);
if ($torrentRequest->approved_by !== null) {
return to_route('requests.show', ['id' => $id])
->withSuccess(trans('request.request-reset'));
->withErrors(trans('request.already-rejected'));
}
$filler = User::findOrFail($torrentRequest->filled_by);
if ($filler->acceptsNotification($request->user(), $filler, 'request', 'show_request_fill_reject')) {
$filler->notify(new NewRequestFillReject('torrent', $user->username, $torrentRequest));
}
$torrentRequest->update([
'filled_by' => null,
'filled_when' => null,
'torrent_id' => null,
]);
return to_route('requests.show', ['id' => $id])
->withSuccess(trans('request.access-error'));
->withSuccess(trans('request.request-reset'));
}
/**
@@ -464,16 +439,12 @@ class RequestController extends Controller
$user = $request->user();
$torrentRequest = TorrentRequest::findOrFail($id);
if ($user->group->is_modo || $torrentRequest->user_id == $user->id) {
$name = $torrentRequest->name;
$torrentRequest->delete();
abort_unless($user->group->is_modo || $torrentRequest->user_id === $user->id, 403);
return to_route('requests.index')
->withSuccess(sprintf(trans('request.deleted'), $name));
}
$torrentRequest->delete();
return to_route('requests.show', ['id' => $id])
->withErrors(trans('request.access-delete-error'));
return to_route('requests.index')
->withSuccess(sprintf(trans('request.deleted'), $torrentRequest->name));
}
/**
@@ -482,15 +453,16 @@ class RequestController extends Controller
public function reset(Request $request, int $id): \Illuminate\Http\RedirectResponse
{
$user = $request->user();
abort_unless($user->group->is_modo, 403);
$torrentRequest = TorrentRequest::findOrFail($id);
$torrentRequest->filled_by = null;
$torrentRequest->filled_when = null;
$torrentRequest->torrent_id = null;
$torrentRequest->approved_by = null;
$torrentRequest->approved_when = null;
$torrentRequest->save();
TorrentRequest::whereKey($id)->update([
'filled_by' => null,
'filled_when' => null,
'torrent_id' => null,
'approved_by' => null,
'approved_when' => null,
]);
return to_route('requests.show', ['id' => $id])
->withSuccess(trans('request.request-reset'));