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