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;
10 class ChannelController extends Controller {
12 public function search(Request $request) {
13 $validatedData = $request->validate([
14 'joinable' => 'boolean|nullable',
15 'manageable' => 'boolean|nullable',
16 'phrase' => 'string|nullable',
19 $channels = Channel::query();
20 if (isset($validatedData['joinable']) && $validatedData['joinable']) {
21 $channels = $channels->where('twitch_chat', '!=', '');
23 if (isset($validatedData['manageable']) && $validatedData['manageable']) {
24 $user = $request->user();
28 $channels = $channels->whereHas('crews', function (Builder $query) use ($user) {
29 $query->where('user_id', '=', $user->id);
32 if (!empty($validatedData['phrase'])) {
33 $channels = $channels->where('title', 'LIKE', '%'.$validatedData['phrase'].'%')
34 ->orWhere('short_name', 'LIKE', '%'.$validatedData['phrase'].'%');
36 $channels = $channels->limit(5);
37 return $channels->get()->toJson();
40 public function single(Request $request, Channel $channel) {
41 $this->authorize('view', $channel);
42 return $channel->toJson();
45 public function chat(Request $request, Channel $channel) {
46 if (!$channel->twitch_chat) {
47 throw new \Exception('channel has no twitch chat set');
49 $validatedData = $request->validate([
50 'text' => 'string|required',
52 $this->authorize('editRestream', $channel);
53 TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user());
54 return $channel->toJson();
57 public function join(Request $request, Channel $channel) {
58 if (!$channel->twitch_chat) {
59 throw new \Exception('channel has no twitch chat set');
61 $this->authorize('editRestream', $channel);
62 $channel->join = true;
64 TwitchBotCommand::join($channel->twitch_chat, $request->user());
65 return $channel->toJson();
68 public function part(Request $request, Channel $channel) {
69 if (!$channel->twitch_chat) {
70 throw new \Exception('channel has no twitch chat set');
72 $this->authorize('editRestream', $channel);
73 $channel->join = false;
75 TwitchBotCommand::part($channel->twitch_chat, $request->user());
76 return $channel->toJson();