X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FHttp%2FControllers%2FEpisodeController.php;h=4e93427836287daea50cf6645cfaa638c5d22b59;hb=212561cf1c6724b52c490104f5a2b4c3418b1c62;hp=2fc5f28867dbdb177e8e249ec1c7d0ec800af9d6;hpb=15132749249f6418fd5695547b5c323a0ad10939;p=alttp.git diff --git a/app/Http/Controllers/EpisodeController.php b/app/Http/Controllers/EpisodeController.php index 2fc5f28..4e93427 100644 --- a/app/Http/Controllers/EpisodeController.php +++ b/app/Http/Controllers/EpisodeController.php @@ -2,29 +2,232 @@ namespace App\Http\Controllers; +use App\Models\Channel; use App\Models\Episode; +use App\Models\EpisodeCrew; +use App\Models\User; use Carbon\Carbon; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; class EpisodeController extends Controller { + public function addRestream(Request $request, Episode $episode) { + $this->authorize('addRestream', $episode); + $validatedData = $request->validate([ + 'accept_comms' => 'boolean', + 'accept_tracker' => 'boolean', + 'channel_id' => 'numeric|exists:App\Models\Channel,id', + ]); + + $channel = Channel::find($validatedData['channel_id']); + $this->authorize('addEpisode', $channel); + + foreach ($episode->channels as $c) { + if ($c->id == $channel->id) { + throw new \Exception('channel already exists on episode'); + } + } + + $validatedProps = $request->validate([ + 'accept_comms' => 'boolean', + 'accept_tracker' => 'boolean', + ]); + + $episode->channels()->attach($channel, $validatedProps); + + return $episode->load('channels')->toJson(); + } + + public function crewManage(Request $request, Episode $episode) { + $this->authorize('editRestream', $episode); + $validatedData = $request->validate([ + 'add' => 'numeric|exists:App\Models\User,id', + 'channel_id' => 'numeric|exists:App\Models\Channel,id', + 'confirm' => 'nullable|numeric|exists:App\Models\EpisodeCrew,id', + 'remove' => 'numeric|exists:App\Models\EpisodeCrew,id', + 'role' => 'string|in:commentary,setup,tracking', + 'unconfirm' => 'nullable|numeric|exists:App\Models\EpisodeCrew,id', + ]); + + $channel = Channel::find($validatedData['channel_id']); + $this->authorize('editRestream', $channel); + + if (isset($validatedData['add'])) { + $crew = $episode->crew() + ->where('user_id', '=', $validatedData['add']) + ->where('role', '=', $validatedData['role']) + ->first(); + if (!$crew) { + $add_user = User::findOrFail($validatedData['add']); + $crew = new EpisodeCrew(); + $crew->channel()->associate($channel); + $crew->episode()->associate($episode); + $crew->user()->associate($add_user); + $crew->role = $validatedData['role']; + } + $crew->confirmed = true; + $crew->save(); + } + + if (isset($validatedData['confirm'])) { + $crew = EpisodeCrew::find($validatedData['confirm']); + $crew->confirmed = true; + $crew->save(); + } + + if (isset($validatedData['remove'])) { + $crew = EpisodeCrew::find($validatedData['remove']); + if ($crew) { + $crew->delete(); + } + } + + if (isset($validatedData['unconfirm'])) { + $crew = EpisodeCrew::find($validatedData['unconfirm']); + $crew->confirmed = false; + $crew->save(); + } + + $user = $request->user(); + if ($user->isPrivileged()) { + return $episode->load(['crew', 'crew.user'])->toJson(); + } else { + return $episode->load([ + 'crew' => function ($query) use ($user) { + $query->where('confirmed', true); + $query->orWhere('user_id', '=', $user->id); + $query->orWhereIn('channel_id', $user->channel_crews->pluck('channel_id')); + }, + 'crew.user', + ])->toJson(); + } + } + + public function crewSignup(Request $request, Episode $episode) { + if (!$request->user()) { + throw new \Exception('requires user to sign up'); + } + $validatedData = $request->validate([ + 'as' => 'string|in:commentary,tracking', + 'channel_id' => 'numeric|exists:App\Models\Channel,id', + ]); + + $channel = $episode->channels()->find($validatedData['channel_id']); + if (!$channel) { + throw new \Exception('channel not found'); + } + + $as = $validatedData['as']; + if ($as == 'commentary' && !$channel->pivot->accept_comms) { + throw new \Exception('channel not looking for commentary'); + } + if ($as == 'tracking' && !$channel->pivot->accept_tracker) { + throw new \Exception('channel not looking for trackers'); + } + + $user = $request->user(); + + foreach ($episode->crew as $crew) { + if ($crew->user_id == $user->id && $crew->role == $as) { + throw new \Exception('already signed up'); + } + } + + $episode->crew()->create([ + 'channel_id' => $channel->id, + 'role' => $as, + 'user_id' => $user->id, + ]); + + if ($user->isPrivileged()) { + return $episode->load(['crew', 'crew.user'])->toJson(); + } else { + return $episode->load([ + 'crew' => function ($query) use ($user) { + $query->where('confirmed', true); + $query->orWhere('user_id', '=', $user->id); + $query->orWhereIn('channel_id', $user->channel_crews->pluck('channel_id')); + }, + 'crew.user', + ])->toJson(); + } + } + + public function editRestream(Request $request, Episode $episode) { + $this->authorize('editRestream', $episode); + $validatedData = $request->validate([ + 'channel_id' => 'numeric|exists:App\Models\Channel,id', + ]); + + $channel = Channel::find($validatedData['channel_id']); + $this->authorize('editRestream', $channel); + + $validatedChanges = $request->validate([ + 'accept_comms' => 'boolean', + 'accept_tracker' => 'boolean', + ]); + + $episode->channels()->updateExistingPivot($channel, $validatedChanges); + + return $episode->load('channels')->toJson(); + } + + public function removeRestream(Request $request, Episode $episode) { + $this->authorize('removeRestream', $episode); + $validatedData = $request->validate([ + 'channel_id' => 'numeric|exists:App\Models\Channel,id', + ]); + + $channel = Channel::find($validatedData['channel_id']); + $this->authorize('removeEpisode', $channel); + + $episode->channels()->detach($channel); + + return $episode->load('channels')->toJson(); + } + public function search(Request $request) { $validatedData = $request->validate([ 'after' => 'nullable|date', 'before' => 'nullable|date', + 'event' => 'nullable|array', + 'event.*' => 'numeric', ]); - $after = isset($validatedData['after']) ? $validatedData['after'] : Carbon::now()->sub(2, 'hours'); + $after = isset($validatedData['after']) ? $validatedData['after'] : Carbon::now(); $before = isset($validatedData['before']) ? $validatedData['before'] : Carbon::now()->add(1, 'days'); $episodes = Episode::with(['channels', 'event', 'players', 'players.user']) ->select('episodes.*') ->join('events', 'episodes.event_id', '=', 'events.id') ->where('episodes.confirmed', '=', true) - ->where('episodes.start', '>=', $after) + ->where(DB::raw('DATE_ADD(`episodes`.`start`, INTERVAL COALESCE(`episodes`.`estimate`, 0) SECOND)'), '>=', $after) ->where('episodes.start', '<=', $before) ->where('events.visible', '=', true) ->orderBy('episodes.start') ->limit(1000); + if (!empty($validatedData['event'])) { + $episodes = $episodes->whereIn('episodes.event_id', $validatedData['event']); + } + if ($request->user() && $request->user()->isPrivileged()) { + $episodes = $episodes->with(['crew', 'crew.user']); + } else if ($request->user()) { + $episodes = $episodes->with([ + 'crew' => function ($query) use ($request) { + $query->where('confirmed', true); + $query->orWhere('user_id', '=', $request->user()->id); + $query->orWhereIn('channel_id', $request->user()->channel_crews->pluck('channel_id')); + }, + 'crew.user', + ]); + } else { + $episodes = $episodes->with([ + 'crew' => function ($query) { + $query->where('confirmed', true); + }, + 'crew.user', + ]); + } return $episodes->get()->toJson(); }