]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/ChannelController.php
add twitch chat comand
[alttp.git] / app / Http / Controllers / ChannelController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use App\Models\Channel;
6 use App\Models\TwitchBotCommand;
7 use Illuminate\Database\Eloquent\Builder;
8 use Illuminate\Http\Request;
9
10 class ChannelController extends Controller {
11
12         public function search(Request $request) {
13                 $validatedData = $request->validate([
14                         'joinable' => 'boolean|nullable',
15                         'manageable' => 'boolean|nullable',
16                         'phrase' => 'string|nullable',
17                 ]);
18
19                 $channels = Channel::query();
20                 if (isset($validatedData['joinable']) && $validatedData['joinable']) {
21                         $channels = $channels->where('twitch_chat', '!=', '');
22                 }
23                 if (isset($validatedData['manageable']) && $validatedData['manageable']) {
24                         $user = $request->user();
25                         if (!$user) {
26                                 return [];
27                         }
28                         $channels = $channels->whereHas('crews', function (Builder $query) use ($user) {
29                                 $query->where('user_id', '=', $user->id);
30                         });
31                 }
32                 if (!empty($validatedData['phrase'])) {
33                         $channels = $channels->where('title', 'LIKE', '%'.$validatedData['phrase'].'%')
34                                 ->orWhere('short_name', 'LIKE', '%'.$validatedData['phrase'].'%');
35                 }
36                 $channels = $channels->limit(5);
37                 return $channels->get()->toJson();
38         }
39
40         public function single(Request $request, Channel $channel) {
41                 $this->authorize('view', $channel);
42                 return $channel->toJson();
43         }
44
45         public function chat(Request $request, Channel $channel) {
46                 if (!$channel->twitch_chat) {
47                         throw new \Exception('channel has no twitch chat set');
48                 }
49                 $validatedData = $request->validate([
50                         'text' => 'string|required',
51                 ]);
52                 $this->authorize('editRestream', $channel);
53                 TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user());
54                 return $channel->toJson();
55         }
56
57         public function join(Request $request, Channel $channel) {
58                 if (!$channel->twitch_chat) {
59                         throw new \Exception('channel has no twitch chat set');
60                 }
61                 $this->authorize('editRestream', $channel);
62                 $channel->join = true;
63                 $channel->save();
64                 TwitchBotCommand::join($channel->twitch_chat, $request->user());
65                 return $channel->toJson();
66         }
67
68         public function part(Request $request, Channel $channel) {
69                 if (!$channel->twitch_chat) {
70                         throw new \Exception('channel has no twitch chat set');
71                 }
72                 $this->authorize('editRestream', $channel);
73                 $channel->join = false;
74                 $channel->save();
75                 TwitchBotCommand::part($channel->twitch_chat, $request->user());
76                 return $channel->toJson();
77         }
78
79 }