|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * NOTICE OF LICENSE. |
| 7 | + * |
| 8 | + * UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0 |
| 9 | + * The details is bundled with this project in the file LICENSE.txt. |
| 10 | + * |
| 11 | + * @project UNIT3D Community Edition |
| 12 | + * |
| 13 | + * @author Roardom <roardom@protonmail.com> |
| 14 | + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 |
| 15 | + */ |
| 16 | + |
| 17 | +namespace App\Http\Controllers; |
| 18 | + |
| 19 | +use App\Enums\ModerationStatus; |
| 20 | +use App\Http\Requests\StorePlaylistSuggestionRequest; |
| 21 | +use App\Http\Requests\UpdatePlaylistSuggestionRequest; |
| 22 | +use App\Models\Playlist; |
| 23 | +use App\Models\PlaylistSuggestion; |
| 24 | +use App\Models\PlaylistTorrent; |
| 25 | +use App\Notifications\PlaylistSuggestionCreated; |
| 26 | +use App\Notifications\PlaylistSuggestionRejected; |
| 27 | +use Illuminate\Support\Facades\Validator; |
| 28 | +use Illuminate\Validation\Rule; |
| 29 | + |
| 30 | +class PlaylistSuggestionController extends Controller |
| 31 | +{ |
| 32 | + /** |
| 33 | + * Store a new playlist. |
| 34 | + */ |
| 35 | + public function store(StorePlaylistSuggestionRequest $request, Playlist $playlist): \Illuminate\Http\RedirectResponse |
| 36 | + { |
| 37 | + Validator::make([ |
| 38 | + 'torrent_id' => basename($request->torrent_url) |
| 39 | + ], [ |
| 40 | + 'torrent_id' => [ |
| 41 | + Rule::exists('torrents', 'id'), |
| 42 | + Rule::unique('playlist_suggestions')->where('playlist_id', $playlist->id), |
| 43 | + ], |
| 44 | + ], [ |
| 45 | + 'torrent_id.exists' => 'The torrent ID/URL ":input" entered was not found on site.', |
| 46 | + 'torrent_id.unique' => 'This torrent ID/URL ":input" entered is already suggested and awaiting moderation.', |
| 47 | + ])->validate(); |
| 48 | + |
| 49 | + $playlistSuggestion = PlaylistSuggestion::create([ |
| 50 | + 'playlist_id' => $playlist->id, |
| 51 | + 'torrent_id' => basename($request->torrent_url), |
| 52 | + 'user_id' => $request->user()->id, |
| 53 | + 'message' => $request->message, |
| 54 | + ]); |
| 55 | + |
| 56 | + $playlist->user->notify(new PlaylistSuggestionCreated($playlistSuggestion)); |
| 57 | + |
| 58 | + return to_route('playlists.show', ['playlist' => $playlist]) |
| 59 | + ->with('success', trans('playlist.suggestion-review')); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Update a playlist suggestion. |
| 64 | + */ |
| 65 | + public function update(UpdatePlaylistSuggestionRequest $request, Playlist $playlist, PlaylistSuggestion $playlistSuggestion): \Illuminate\Http\RedirectResponse |
| 66 | + { |
| 67 | + abort_unless($request->user()->id == $playlist->user_id || $request->user()->group->is_modo, 403); |
| 68 | + |
| 69 | + switch (ModerationStatus::from($request->integer('status'))) { |
| 70 | + case ModerationStatus::APPROVED: |
| 71 | + $playlistTorrent = PlaylistTorrent::create([ |
| 72 | + 'playlist_id' => $playlistSuggestion->playlist_id, |
| 73 | + 'torrent_id' => $playlistSuggestion->torrent_id, |
| 74 | + ]); |
| 75 | + |
| 76 | + $playlistTorrent->torrent()->searchable(); |
| 77 | + $playlistSuggestion->delete(); |
| 78 | + |
| 79 | + return to_route('playlists.show', ['playlist' => $playlist]) |
| 80 | + ->withFragment('#playlist_suggestions') |
| 81 | + ->with('success', trans('playlist.suggestion-approved')); |
| 82 | + case ModerationStatus::REJECTED: |
| 83 | + $playlistSuggestion->user->notify(new PlaylistSuggestionRejected($playlistSuggestion, $request->rejection_message)); |
| 84 | + $playlistSuggestion->delete(); |
| 85 | + |
| 86 | + return to_route('playlists.show', ['playlist' => $playlist]) |
| 87 | + ->withFragment('#playlist_suggestions') |
| 88 | + ->with('success', trans('playlist.suggestion-rejected')); |
| 89 | + |
| 90 | + default: |
| 91 | + return to_route('playlists.show', ['playlist' => $playlist]); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments