]> git.localhorst.tv Git - alttp.git/blobdiff - app/Http/Controllers/ChannelController.php
basic twitch join/part commands
[alttp.git] / app / Http / Controllers / ChannelController.php
diff --git a/app/Http/Controllers/ChannelController.php b/app/Http/Controllers/ChannelController.php
new file mode 100644 (file)
index 0000000..904cb8f
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\Channel;
+use App\Models\TwitchBotCommand;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Http\Request;
+
+class ChannelController extends Controller {
+
+       public function search(Request $request) {
+               $validatedData = $request->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 join(Request $request, Channel $channel) {
+               if (!$channel->twitch_chat) {
+                       throw new \Exception('channel has no twitch chat set');
+               }
+               $this->authorize('editRestream', $channel);
+               $channel->join = true;
+               $channel->save();
+               TwitchBotCommand::join($channel->twitch_chat);
+               return $channel->toJson();
+       }
+
+       public function part(Request $request, Channel $channel) {
+               if (!$channel->twitch_chat) {
+                       throw new \Exception('channel has no twitch chat set');
+               }
+               $this->authorize('editRestream', $channel);
+               $channel->join = false;
+               $channel->save();
+               TwitchBotCommand::part($channel->twitch_chat);
+               return $channel->toJson();
+       }
+
+}