3 namespace App\Http\Controllers;
5 use App\Models\Channel;
6 use App\Models\TwitchBotCommand;
7 use Illuminate\Database\Eloquent\Builder;
8 use Illuminate\Http\Request;
9 use Illuminate\Support\Collection;
10 use Illuminate\Support\Facades\Gate;
12 class ChannelController extends Controller {
14 public function search(Request $request) {
15 $validatedData = $request->validate([
17 'id.*' => 'integer|numeric',
18 'joinable' => 'boolean|nullable',
19 'manageable' => 'boolean|nullable',
20 'phrase' => 'string|nullable',
23 $channels = Channel::query();
24 if (!empty($validatedData['id'])) {
25 $channels = $channels->whereIn('id', $validatedData['id']);
27 if (isset($validatedData['joinable']) && $validatedData['joinable']) {
28 $channels = $channels->where('twitch_chat', '!=', '');
30 if (isset($validatedData['manageable']) && $validatedData['manageable']) {
31 $user = $request->user();
35 $channels = $channels->whereHas('crews', function (Builder $query) use ($user) {
36 $query->where('user_id', '=', $user->id);
39 if (!empty($validatedData['phrase'])) {
40 $channels = $channels->where('title', 'LIKE', '%'.$validatedData['phrase'].'%')
41 ->orWhere('short_name', 'LIKE', '%'.$validatedData['phrase'].'%');
43 $channels = $channels->limit(5);
44 return $this->sendChannels($channels->get());
47 public function single(Request $request, Channel $channel) {
48 $this->authorize('view', $channel);
49 return $this->sendChannel($channel);
52 public function chat(Request $request, Channel $channel) {
53 if (!$channel->twitch_chat) {
54 throw new \Exception('channel has no twitch chat set');
56 $validatedData = $request->validate([
57 'bot_nick' => 'string',
58 'category' => 'string',
61 $this->authorize('editRestream', $channel);
62 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
63 if (empty($validatedData['category'])) {
64 TwitchBotCommand::chat($channel->twitch_chat, $validatedData['text'], $request->user(), $nick);
66 TwitchBotCommand::randomChat($channel, $validatedData['category'], $request->user(), $nick);
68 return $this->sendChannel($channel);
71 public function chatBotLog(Request $request, Channel $channel) {
72 $this->authorize('editRestream', $channel);
73 $log = $channel->chat_bot_logs()
74 ->with(['origin', 'user'])
75 ->orderBy('created_at', 'DESC')
78 return $log->values()->toJson();
81 public function chatSettings(Request $request, Channel $channel) {
82 if (!$channel->twitch_chat) {
83 throw new \Exception('channel has no twitch chat set');
85 $validatedData = $request->validate([
86 'language' => 'string|in:de,en,es,fr',
87 'min_age' => 'integer|min:1',
88 'respond' => 'string|in:50,no,yes',
89 'source' => 'string|in:any,cat,catchan,chan',
90 'wait_msgs_min' => 'integer|min:1',
91 'wait_msgs_max' => 'integer|min:1',
92 'wait_time_min' => 'integer',
93 'wait_time_max' => 'integer',
95 $this->authorize('editRestream', $channel);
96 $channel->chat_settings = $validatedData;
98 return $this->sendChannel($channel);
101 public function join(Request $request, Channel $channel) {
102 if (!$channel->twitch_chat) {
103 throw new \Exception('channel has no twitch chat set');
105 $validatedData = $request->validate([
106 'bot_nick' => 'string',
108 $this->authorize('editRestream', $channel);
109 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
110 if ($nick == 'localhorsttv') {
111 $channel->join = true;
112 } else if ($nick == 'horstiebot') {
113 $channel->chat = true;
116 TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
117 return $this->sendChannel($channel);
120 public function part(Request $request, Channel $channel) {
121 if (!$channel->twitch_chat) {
122 throw new \Exception('channel has no twitch chat set');
124 $validatedData = $request->validate([
125 'bot_nick' => 'string',
127 $this->authorize('editRestream', $channel);
128 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
129 if ($nick == 'localhorsttv') {
130 $channel->join = false;
131 } else if ($nick == 'horstiebot') {
132 $channel->chat = false;
135 TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
136 return $this->sendChannel($channel);
139 public function deleteCommand(Channel $channel, $command) {
140 $this->authorize('editRestream', $channel);
141 if (isset($channel->chat_commands[$command])) {
142 $cmds = $channel->chat_commands;
143 unset($cmds[$command]);
144 $channel->chat_commands = $cmds;
147 return $this->sendChannel($channel);
150 public function saveCommand(Request $request, Channel $channel, $command) {
151 $this->authorize('editRestream', $channel);
153 $validatedData = $request->validate([
154 'command' => 'string',
155 'restrict' => 'string',
159 $cmds = $channel->chat_commands;
160 $cmds[$command] = $validatedData;
161 $channel->chat_commands = $cmds;
164 return $this->sendChannel($channel);
167 public function controlGuessingGame(Request $request, Channel $channel, $name) {
168 $this->authorize('editRestream', $channel);
170 $validatedData = $request->validate([
171 'action' => 'required|in:cancel,solve,start,stop',
175 switch ($validatedData['action']) {
177 $channel->clearGuessing();
178 $msg = $channel->getGuessingSetting('cancel_message');
180 TwitchBotCommand::chat($channel->twitch_chat, $msg);
184 if ($channel->hasActiveGuessing() && $channel->isValidGuess($validatedData['solution'])) {
185 $winners = $channel->solveGuessing($validatedData['solution']);
186 $msg = $channel->listGuessingWinners($winners);
188 TwitchBotCommand::chat($channel->twitch_chat, $msg);
190 $channel->clearGuessing();
194 if (!$channel->hasActiveGuessing()) {
195 $channel->startGuessing($name);
196 $msg = $channel->getGuessingSetting('start_message');
198 TwitchBotCommand::chat($channel->twitch_chat, $msg);
203 if ($channel->hasActiveGuessing()) {
204 $channel->stopGuessing();
205 $msg = $channel->getGuessingSetting('stop_message');
207 TwitchBotCommand::chat($channel->twitch_chat, $msg);
213 return $this->sendChannel($channel);
216 public function getGuessingGame(Channel $channel, $name) {
217 $this->authorize('editRestream', $channel);
219 $cutoff = $channel->guessing_start;
220 if (is_null($cutoff)) {
221 $last = $channel->winners()->latest()->first();
222 $cutoff = $last->pod;
224 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
225 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
227 'guesses' => $guesses->toArray(),
228 'winners' => $winners->toArray(),
232 public function getGuessingGameLeaderboard(Channel $channel, $type) {
234 'all' => $channel->getGuessingLeaderboard(),
238 public function getGuessingGameMonitor($key) {
239 $channel = Channel::where('access_key', '=', $key)->firstOrFail();
241 $cutoff = $channel->guessing_start;
242 if (is_null($cutoff)) {
243 $last = $channel->winners()->latest()->first();
244 $cutoff = $last->pod;
246 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
247 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
249 'channel' => $channel->toArray(),
250 'guesses' => $guesses->toArray(),
251 'winners' => $winners->toArray(),
255 public function saveGuessingGame(Request $request, Channel $channel, $name) {
256 $this->authorize('editRestream', $channel);
258 $validatedData = $request->validate([
259 'active_message' => 'string',
260 'cancel_message' => 'string',
261 'close_winners_message' => 'string',
262 'invalid_solution_message' => 'string',
263 'leaderboard_type' => 'string',
264 'no_winners_message' => 'string',
265 'not_active_message' => 'string',
266 'points_exact_first' => 'numeric|min:1|max:5',
267 'points_exact_other' => 'numeric|min:0|max:5',
268 'points_close_first' => 'numeric|min:0|max:5',
269 'points_close_max' => 'integer|min:0',
270 'points_close_other' => 'numeric|min:0|max:5',
271 'start_message' => 'string',
272 'stop_message' => 'string',
273 'winners_message' => 'string',
276 $settings = $channel->guessing_settings;
277 $settings[$name] = $validatedData;
278 $channel->guessing_settings = $settings;
281 return $this->sendChannel($channel);
284 protected function sendChannel(Channel $channel) {
285 if (Gate::allows('editRestream', $channel)) {
286 $this->unhideChannel($channel);
288 return $channel->toJson();
291 protected function sendChannels(Collection $channels) {
292 foreach ($channels as $channel) {
293 if (Gate::allows('editRestream', $channel)) {
294 $this->unhideChannel($channel);
297 return $channels->toJson();
300 private function unhideChannel(Channel $channel) {
301 $channel->makeVisible([