]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/ChannelController.php
basic twitch join/part commands
[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 join(Request $request, Channel $channel) {
46                 if (!$channel->twitch_chat) {
47                         throw new \Exception('channel has no twitch chat set');
48                 }
49                 $this->authorize('editRestream', $channel);
50                 $channel->join = true;
51                 $channel->save();
52                 TwitchBotCommand::join($channel->twitch_chat);
53                 return $channel->toJson();
54         }
55
56         public function part(Request $request, Channel $channel) {
57                 if (!$channel->twitch_chat) {
58                         throw new \Exception('channel has no twitch chat set');
59                 }
60                 $this->authorize('editRestream', $channel);
61                 $channel->join = false;
62                 $channel->save();
63                 TwitchBotCommand::part($channel->twitch_chat);
64                 return $channel->toJson();
65         }
66
67 }