]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/EpisodeController.php
better schedule start
[alttp.git] / app / Http / Controllers / EpisodeController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use App\Models\Channel;
6 use App\Models\Episode;
7 use App\Models\EpisodeCrew;
8 use App\Models\User;
9 use Carbon\Carbon;
10 use Illuminate\Http\Request;
11 use Illuminate\Support\Facades\DB;
12
13 class EpisodeController extends Controller
14 {
15
16         public function addRestream(Request $request, Episode $episode) {
17                 $this->authorize('addRestream', $episode);
18                 $validatedData = $request->validate([
19                         'accept_comms' => 'boolean',
20                         'accept_tracker' => 'boolean',
21                         'channel_id' => 'numeric|exists:App\Models\Channel,id',
22                 ]);
23
24                 $channel = Channel::find($validatedData['channel_id']);
25                 $this->authorize('addEpisode', $channel);
26
27                 foreach ($episode->channels as $c) {
28                         if ($c->id == $channel->id) {
29                                 throw new \Exception('channel already exists on episode');
30                         }
31                 }
32
33                 $validatedProps = $request->validate([
34                         'accept_comms' => 'boolean',
35                         'accept_tracker' => 'boolean',
36                 ]);
37
38                 $episode->channels()->attach($channel, $validatedProps);
39
40                 return $episode->load('channels')->toJson();
41         }
42
43         public function crewManage(Request $request, Episode $episode) {
44                 $this->authorize('editRestream', $episode);
45                 $validatedData = $request->validate([
46                         'add' => 'numeric|exists:App\Models\User,id',
47                         'channel_id' => 'numeric|exists:App\Models\Channel,id',
48                         'confirm' => 'nullable|numeric|exists:App\Models\EpisodeCrew,id',
49                         'remove' => 'numeric|exists:App\Models\EpisodeCrew,id',
50                         'role' => 'string|in:commentary,setup,tracking',
51                         'unconfirm' => 'nullable|numeric|exists:App\Models\EpisodeCrew,id',
52                 ]);
53
54                 $channel = Channel::find($validatedData['channel_id']);
55                 $this->authorize('editRestream', $channel);
56
57                 if (isset($validatedData['add'])) {
58                         $crew = $episode->crew()
59                                 ->where('user_id', '=', $validatedData['add'])
60                                 ->where('role', '=', $validatedData['role'])
61                                 ->first();
62                         if (!$crew) {
63                                 $add_user = User::findOrFail($validatedData['add']);
64                                 $crew = new EpisodeCrew();
65                                 $crew->channel()->associate($channel);
66                                 $crew->episode()->associate($episode);
67                                 $crew->user()->associate($add_user);
68                                 $crew->role = $validatedData['role'];
69                         }
70                         $crew->confirmed = true;
71                         $crew->save();
72                 }
73
74                 if (isset($validatedData['confirm'])) {
75                         $crew = EpisodeCrew::find($validatedData['confirm']);
76                         $crew->confirmed = true;
77                         $crew->save();
78                 }
79
80                 if (isset($validatedData['remove'])) {
81                         $crew = EpisodeCrew::find($validatedData['remove']);
82                         if ($crew) {
83                                 $crew->delete();
84                         }
85                 }
86
87                 if (isset($validatedData['unconfirm'])) {
88                         $crew = EpisodeCrew::find($validatedData['unconfirm']);
89                         $crew->confirmed = false;
90                         $crew->save();
91                 }
92
93                 $user = $request->user();
94                 if ($user->isPrivileged()) {
95                         return $episode->load(['crew', 'crew.user'])->toJson();
96                 } else {
97                         return $episode->load([
98                                 'crew' => function ($query) use ($user) {
99                                         $query->where('confirmed', true);
100                                         $query->orWhere('user_id', '=', $user->id);
101                                         $query->orWhereIn('channel_id', $user->channel_crews->pluck('channel_id'));
102                                 },
103                                 'crew.user',
104                         ])->toJson();
105                 }
106         }
107
108         public function crewSignup(Request $request, Episode $episode) {
109                 if (!$request->user()) {
110                         throw new \Exception('requires user to sign up');
111                 }
112                 $validatedData = $request->validate([
113                         'as' => 'string|in:commentary,tracking',
114                         'channel_id' => 'numeric|exists:App\Models\Channel,id',
115                 ]);
116
117                 $channel = $episode->channels()->find($validatedData['channel_id']);
118                 if (!$channel) {
119                         throw new \Exception('channel not found');
120                 }
121
122                 $as = $validatedData['as'];
123                 if ($as == 'commentary' && !$channel->pivot->accept_comms) {
124                         throw new \Exception('channel not looking for commentary');
125                 }
126                 if ($as == 'tracking' && !$channel->pivot->accept_tracker) {
127                         throw new \Exception('channel not looking for trackers');
128                 }
129
130                 $user = $request->user();
131
132                 foreach ($episode->crew as $crew) {
133                         if ($crew->user_id == $user->id && $crew->role == $as) {
134                                 throw new \Exception('already signed up');
135                         }
136                 }
137
138                 $episode->crew()->create([
139                         'channel_id' => $channel->id,
140                         'role' => $as,
141                         'user_id' => $user->id,
142                 ]);
143
144                 if ($user->isPrivileged()) {
145                         return $episode->load(['crew', 'crew.user'])->toJson();
146                 } else {
147                         return $episode->load([
148                                 'crew' => function ($query) use ($user) {
149                                         $query->where('confirmed', true);
150                                         $query->orWhere('user_id', '=', $user->id);
151                                         $query->orWhereIn('channel_id', $user->channel_crews->pluck('channel_id'));
152                                 },
153                                 'crew.user',
154                         ])->toJson();
155                 }
156         }
157
158         public function editRestream(Request $request, Episode $episode) {
159                 $this->authorize('editRestream', $episode);
160                 $validatedData = $request->validate([
161                         'channel_id' => 'numeric|exists:App\Models\Channel,id',
162                 ]);
163
164                 $channel = Channel::find($validatedData['channel_id']);
165                 $this->authorize('editRestream', $channel);
166
167                 $validatedChanges = $request->validate([
168                         'accept_comms' => 'boolean',
169                         'accept_tracker' => 'boolean',
170                 ]);
171
172                 $episode->channels()->updateExistingPivot($channel, $validatedChanges);
173
174                 return $episode->load('channels')->toJson();
175         }
176
177         public function removeRestream(Request $request, Episode $episode) {
178                 $this->authorize('removeRestream', $episode);
179                 $validatedData = $request->validate([
180                         'channel_id' => 'numeric|exists:App\Models\Channel,id',
181                 ]);
182
183                 $channel = Channel::find($validatedData['channel_id']);
184                 $this->authorize('removeEpisode', $channel);
185
186                 $episode->channels()->detach($channel);
187
188                 return $episode->load('channels')->toJson();
189         }
190
191         public function search(Request $request) {
192                 $validatedData = $request->validate([
193                         'after' => 'nullable|date',
194                         'before' => 'nullable|date',
195                         'event' => 'nullable|array',
196                         'event.*' => 'numeric',
197                 ]);
198                 $after = isset($validatedData['after']) ? $validatedData['after'] : Carbon::now();
199                 $before = isset($validatedData['before']) ? $validatedData['before'] : Carbon::now()->add(1, 'days');
200                 $episodes = Episode::with(['channels', 'event', 'players', 'players.user'])
201                         ->select('episodes.*')
202                         ->join('events', 'episodes.event_id', '=', 'events.id')
203                         ->where('episodes.confirmed', '=', true)
204                         ->where(DB::raw('DATE_ADD(`episodes`.`start`, INTERVAL COALESCE(`episodes`.`estimate`, 0) SECOND)'), '>=', $after)
205                         ->where('episodes.start', '<=', $before)
206                         ->where('events.visible', '=', true)
207                         ->orderBy('episodes.start')
208                         ->limit(1000);
209                 if (!empty($validatedData['event'])) {
210                         $episodes = $episodes->whereIn('episodes.event_id', $validatedData['event']);
211                 }
212                 if ($request->user() && $request->user()->isPrivileged()) {
213                         $episodes = $episodes->with(['crew', 'crew.user']);
214                 } else if ($request->user()) {
215                         $episodes = $episodes->with([
216                                 'crew' => function ($query) use ($request) {
217                                         $query->where('confirmed', true);
218                                         $query->orWhere('user_id', '=', $request->user()->id);
219                                         $query->orWhereIn('channel_id', $request->user()->channel_crews->pluck('channel_id'));
220                                 },
221                                 'crew.user',
222                         ]);
223                 } else {
224                         $episodes = $episodes->with([
225                                 'crew' => function ($query) {
226                                         $query->where('confirmed', true);
227                                 },
228                                 'crew.user',
229                         ]);
230                 }
231                 return $episodes->get()->toJson();
232         }
233
234         public function single(Request $request, Episode $episode) {
235                 $this->authorize('view', $episode);
236                 return $episode->toJson();
237         }
238
239 }