]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/ChannelController.php
b735a6f70b3e3e6fcc45c0df81dfd9446af970e9
[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                         'bot_nick' => 'string',
51                         'text' => 'string|required',
52                 ]);
53                 $this->authorize('editRestream', $channel);
54                 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
55                 TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick);
56                 return $channel->toJson();
57         }
58
59         public function join(Request $request, Channel $channel) {
60                 if (!$channel->twitch_chat) {
61                         throw new \Exception('channel has no twitch chat set');
62                 }
63                 $validatedData = $request->validate([
64                         'bot_nick' => 'string',
65                 ]);
66                 $this->authorize('editRestream', $channel);
67                 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
68                 if ($nick == 'localhorsttv') {
69                         $channel->join = true;
70                 } else if ($nick == 'horstiebot') {
71                         $channel->chat = true;
72                 }
73                 $channel->save();
74                 TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
75                 return $channel->toJson();
76         }
77
78         public function part(Request $request, Channel $channel) {
79                 if (!$channel->twitch_chat) {
80                         throw new \Exception('channel has no twitch chat set');
81                 }
82                 $validatedData = $request->validate([
83                         'bot_nick' => 'string',
84                 ]);
85                 $this->authorize('editRestream', $channel);
86                 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
87                 if ($nick == 'localhorsttv') {
88                         $channel->join = false;
89                 } else if ($nick == 'horstiebot') {
90                         $channel->chat = false;
91                 }
92                 $channel->save();
93                 TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
94                 return $channel->toJson();
95         }
96
97 }