validate([ 'joinable' => 'boolean|nullable', 'manageable' => 'boolean|nullable', 'phrase' => 'string|nullable', ]); $channels = Channel::query(); if (isset($validatedData['joinable']) && $validatedData['joinable']) { $channels = $channels->where('twitch_chat', '!=', ''); } if (isset($validatedData['manageable']) && $validatedData['manageable']) { $user = $request->user(); if (!$user) { return []; } $channels = $channels->whereHas('crews', function (Builder $query) use ($user) { $query->where('user_id', '=', $user->id); }); } if (!empty($validatedData['phrase'])) { $channels = $channels->where('title', 'LIKE', '%'.$validatedData['phrase'].'%') ->orWhere('short_name', 'LIKE', '%'.$validatedData['phrase'].'%'); } $channels = $channels->limit(5); return $channels->get()->toJson(); } public function single(Request $request, Channel $channel) { $this->authorize('view', $channel); return $channel->toJson(); } public function chat(Request $request, Channel $channel) { if (!$channel->twitch_chat) { throw new \Exception('channel has no twitch chat set'); } $validatedData = $request->validate([ 'bot_nick' => 'string', 'text' => 'string|required', ]); $this->authorize('editRestream', $channel); $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick']; TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick); return $channel->toJson(); } public function chatSettings(Request $request, Channel $channel) { if (!$channel->twitch_chat) { throw new \Exception('channel has no twitch chat set'); } $validatedData = $request->validate([ 'wait_msgs_min' => 'integer|min:1', 'wait_msgs_max' => 'integer', 'wait_time_min' => 'integer', 'wait_time_max' => 'integer', ]); $this->authorize('editRestream', $channel); $channel->chat_settings = $validatedData; $channel->save(); return $channel->toJson(); } public function join(Request $request, Channel $channel) { if (!$channel->twitch_chat) { throw new \Exception('channel has no twitch chat set'); } $validatedData = $request->validate([ 'bot_nick' => 'string', ]); $this->authorize('editRestream', $channel); $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick']; if ($nick == 'localhorsttv') { $channel->join = true; } else if ($nick == 'horstiebot') { $channel->chat = true; } $channel->save(); TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick); return $channel->toJson(); } public function part(Request $request, Channel $channel) { if (!$channel->twitch_chat) { throw new \Exception('channel has no twitch chat set'); } $validatedData = $request->validate([ 'bot_nick' => 'string', ]); $this->authorize('editRestream', $channel); $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick']; if ($nick == 'localhorsttv') { $channel->join = false; } else if ($nick == 'horstiebot') { $channel->chat = false; } $channel->save(); TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick); return $channel->toJson(); } }