--- /dev/null
+<?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();
+ }
+
+}