3 namespace App\Http\Controllers;
5 use App\Models\Channel;
6 use App\Models\TwitchBotCommand;
7 use Illuminate\Database\Eloquent\Builder;
8 use Illuminate\Http\Request;
9 use Illuminate\Support\Collection;
10 use Illuminate\Support\Facades\Gate;
12 class ChannelController extends Controller {
14 public function search(Request $request) {
15 $validatedData = $request->validate([
17 'id.*' => 'integer|numeric',
18 'joinable' => 'boolean|nullable',
19 'manageable' => 'boolean|nullable',
20 'phrase' => 'string|nullable',
23 $channels = Channel::query();
24 if (!empty($validatedData['id'])) {
25 $channels = $channels->whereIn('id', $validatedData['id']);
27 if (isset($validatedData['joinable']) && $validatedData['joinable']) {
28 $channels = $channels->where('twitch_chat', '!=', '');
30 if (isset($validatedData['manageable']) && $validatedData['manageable']) {
31 $user = $request->user();
35 $channels = $channels->whereHas('crews', function (Builder $query) use ($user) {
36 $query->where('user_id', '=', $user->id);
39 if (!empty($validatedData['phrase'])) {
40 $channels = $channels->where('title', 'LIKE', '%'.$validatedData['phrase'].'%')
41 ->orWhere('short_name', 'LIKE', '%'.$validatedData['phrase'].'%');
43 $channels = $channels->limit(5);
44 return $this->sendChannels($channels->get());
47 public function single(Request $request, Channel $channel) {
48 $this->authorize('view', $channel);
49 return $this->sendChannel($channel);
52 public function chat(Request $request, Channel $channel) {
53 if (!$channel->twitch_chat) {
54 throw new \Exception('channel has no twitch chat set');
56 $validatedData = $request->validate([
58 'bot_nick' => 'string',
59 'category' => 'string',
62 $this->authorize('editRestream', $channel);
63 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
64 if (isset($validatedData['adlib']) && $validatedData['adlib']) {
65 TwitchBotCommand::adlibChat($channel, $request->user());
66 } else if (empty($validatedData['category'])) {
67 TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick);
69 TwitchBotCommand::randomChat($channel, $validatedData['category'], $request->user(), $nick);
71 return $this->sendChannel($channel);
74 public function chatBotLog(Request $request, Channel $channel) {
75 $this->authorize('editRestream', $channel);
76 $log = $channel->chat_bot_logs()
77 ->with(['origin', 'user'])
78 ->orderBy('created_at', 'DESC')
81 return $log->values()->toJson();
84 public function chatSettings(Request $request, Channel $channel) {
85 if (!$channel->twitch_chat) {
86 throw new \Exception('channel has no twitch chat set');
88 $validatedData = $request->validate([
89 'adlib' => 'integer|min:0|max:100',
90 'language' => 'string|in:de,en,es,fr',
91 'min_age' => 'integer|min:1',
92 'respond' => 'string|in:50,no,yes',
93 'source' => 'string|in:any,cat,catchan,chan',
94 'wait_msgs_min' => 'integer|min:1',
95 'wait_msgs_max' => 'integer|min:1',
96 'wait_time_min' => 'integer',
97 'wait_time_max' => 'integer',
99 $this->authorize('editRestream', $channel);
100 $channel->chat_settings = $validatedData;
102 return $this->sendChannel($channel);
105 public function join(Request $request, Channel $channel) {
106 if (!$channel->twitch_chat) {
107 throw new \Exception('channel has no twitch chat set');
109 $validatedData = $request->validate([
110 'bot_nick' => 'string',
112 $this->authorize('editRestream', $channel);
113 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
114 if ($nick == 'localhorsttv') {
115 $channel->join = true;
116 } else if ($nick == 'horstiebot') {
117 $channel->chat = true;
120 TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
121 return $this->sendChannel($channel);
124 public function part(Request $request, Channel $channel) {
125 if (!$channel->twitch_chat) {
126 throw new \Exception('channel has no twitch chat set');
128 $validatedData = $request->validate([
129 'bot_nick' => 'string',
131 $this->authorize('editRestream', $channel);
132 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
133 if ($nick == 'localhorsttv') {
134 $channel->join = false;
135 } else if ($nick == 'horstiebot') {
136 $channel->chat = false;
139 TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
140 return $this->sendChannel($channel);
143 public function deleteCommand(Channel $channel, $command) {
144 $this->authorize('editRestream', $channel);
145 if (isset($channel->chat_commands[$command])) {
146 $cmds = $channel->chat_commands;
147 unset($cmds[$command]);
148 $channel->chat_commands = $cmds;
151 return $this->sendChannel($channel);
154 public function saveCommand(Request $request, Channel $channel, $command) {
155 $this->authorize('editRestream', $channel);
157 $validatedData = $request->validate([
158 'command' => 'string',
159 'restrict' => 'string',
163 $cmds = $channel->chat_commands;
164 $cmds[$command] = $validatedData;
165 $channel->chat_commands = $cmds;
168 return $this->sendChannel($channel);
171 public function controlGuessingGame(Request $request, Channel $channel, $name) {
172 $this->authorize('editRestream', $channel);
174 $validatedData = $request->validate([
175 'action' => 'required|in:cancel,solve,start,stop',
179 switch ($validatedData['action']) {
181 $channel->clearGuessing();
182 $msg = $channel->getGuessingSetting('cancel_message');
184 TwitchBotCommand::chat($channel->twitch_chat, $msg);
188 if ($channel->hasActiveGuessing() && $channel->isValidGuess($validatedData['solution'])) {
189 $winners = $channel->solveGuessing($validatedData['solution']);
190 $msg = $channel->listGuessingWinners($winners);
192 TwitchBotCommand::chat($channel->twitch_chat, $msg);
194 $channel->clearGuessing();
198 if (!$channel->hasActiveGuessing()) {
199 $channel->startGuessing($name);
200 $msg = $channel->getGuessingSetting('start_message');
202 TwitchBotCommand::chat($channel->twitch_chat, $msg);
207 if ($channel->hasActiveGuessing()) {
208 $channel->stopGuessing();
209 $msg = $channel->getGuessingSetting('stop_message');
211 TwitchBotCommand::chat($channel->twitch_chat, $msg);
217 return $this->sendChannel($channel);
220 public function getGuessingGame(Channel $channel, $name) {
221 $this->authorize('editRestream', $channel);
223 $cutoff = $channel->guessing_start;
224 if (is_null($cutoff)) {
225 $last = $channel->winners()->latest()->first();
226 $cutoff = $last->pod;
228 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
229 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
231 'guesses' => $guesses->toArray(),
232 'winners' => $winners->toArray(),
236 public function getGuessingGameLeaderboard(Channel $channel, $type) {
238 'all' => $channel->getGuessingLeaderboard(),
242 public function getGuessingGameMonitor($key) {
243 $channel = Channel::where('access_key', '=', $key)->firstOrFail();
245 $cutoff = $channel->guessing_start;
246 if (is_null($cutoff)) {
247 $last = $channel->winners()->latest()->first();
248 $cutoff = $last->pod;
250 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
251 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
253 'channel' => $channel->toArray(),
254 'guesses' => $guesses->toArray(),
255 'winners' => $winners->toArray(),
259 public function saveGuessingGame(Request $request, Channel $channel, $name) {
260 $this->authorize('editRestream', $channel);
262 $validatedData = $request->validate([
263 'active_message' => 'string',
264 'cancel_message' => 'string',
265 'close_winners_message' => 'string',
266 'invalid_solution_message' => 'string',
267 'leaderboard_type' => 'string',
268 'no_winners_message' => 'string',
269 'not_active_message' => 'string',
270 'points_exact_first' => 'numeric|min:1|max:5',
271 'points_exact_other' => 'numeric|min:0|max:5',
272 'points_close_first' => 'numeric|min:0|max:5',
273 'points_close_max' => 'integer|min:0',
274 'points_close_other' => 'numeric|min:0|max:5',
275 'start_message' => 'string',
276 'stop_message' => 'string',
277 'winners_message' => 'string',
280 $settings = $channel->guessing_settings;
281 $settings[$name] = $validatedData;
282 $channel->guessing_settings = $settings;
285 return $this->sendChannel($channel);
288 protected function sendChannel(Channel $channel) {
289 if (Gate::allows('editRestream', $channel)) {
290 $this->unhideChannel($channel);
292 return $channel->toJson();
295 protected function sendChannels(Collection $channels) {
296 foreach ($channels as $channel) {
297 if (Gate::allows('editRestream', $channel)) {
298 $this->unhideChannel($channel);
301 return $channels->toJson();
304 private function unhideChannel(Channel $channel) {
305 $channel->makeVisible([