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 'respond' => 'string|in:50,no,yes',
88 'wait_msgs_min' => 'integer|min:1',
89 'wait_msgs_max' => 'integer',
90 'wait_time_min' => 'integer',
91 'wait_time_max' => 'integer',
93 $this->authorize('editRestream', $channel);
94 $channel->chat_settings = $validatedData;
96 return $this->sendChannel($channel);
99 public function join(Request $request, Channel $channel) {
100 if (!$channel->twitch_chat) {
101 throw new \Exception('channel has no twitch chat set');
103 $validatedData = $request->validate([
104 'bot_nick' => 'string',
106 $this->authorize('editRestream', $channel);
107 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
108 if ($nick == 'localhorsttv') {
109 $channel->join = true;
110 } else if ($nick == 'horstiebot') {
111 $channel->chat = true;
114 TwitchBotCommand::join($channel->twitch_chat, $request->user(), $nick);
115 return $this->sendChannel($channel);
118 public function part(Request $request, Channel $channel) {
119 if (!$channel->twitch_chat) {
120 throw new \Exception('channel has no twitch chat set');
122 $validatedData = $request->validate([
123 'bot_nick' => 'string',
125 $this->authorize('editRestream', $channel);
126 $nick = empty($validatedData['bot_nick']) ? 'localhorsttv' : $validatedData['bot_nick'];
127 if ($nick == 'localhorsttv') {
128 $channel->join = false;
129 } else if ($nick == 'horstiebot') {
130 $channel->chat = false;
133 TwitchBotCommand::part($channel->twitch_chat, $request->user(), $nick);
134 return $this->sendChannel($channel);
137 public function deleteCommand(Channel $channel, $command) {
138 $this->authorize('editRestream', $channel);
139 if (isset($channel->chat_commands[$command])) {
140 $cmds = $channel->chat_commands;
141 unset($cmds[$command]);
142 $channel->chat_commands = $cmds;
145 return $this->sendChannel($channel);
148 public function saveCommand(Request $request, Channel $channel, $command) {
149 $this->authorize('editRestream', $channel);
151 $validatedData = $request->validate([
152 'command' => 'string',
153 'restrict' => 'string',
157 $cmds = $channel->chat_commands;
158 $cmds[$command] = $validatedData;
159 $channel->chat_commands = $cmds;
162 return $this->sendChannel($channel);
165 public function controlGuessingGame(Request $request, Channel $channel, $name) {
166 $this->authorize('editRestream', $channel);
168 $validatedData = $request->validate([
169 'action' => 'required|in:cancel,solve,start,stop',
173 switch ($validatedData['action']) {
175 $channel->clearGuessing();
176 $msg = $channel->getGuessingSetting('cancel_message');
178 TwitchBotCommand::chat($channel->twitch_chat, $msg);
182 if ($channel->hasActiveGuessing() && $channel->isValidGuess($validatedData['solution'])) {
183 $winners = $channel->solveGuessing($validatedData['solution']);
184 $msg = $channel->listGuessingWinners($winners);
186 TwitchBotCommand::chat($channel->twitch_chat, $msg);
188 $channel->clearGuessing();
192 if (!$channel->hasActiveGuessing()) {
193 $channel->startGuessing($name);
194 $msg = $channel->getGuessingSetting('start_message');
196 TwitchBotCommand::chat($channel->twitch_chat, $msg);
201 if ($channel->hasActiveGuessing()) {
202 $channel->stopGuessing();
203 $msg = $channel->getGuessingSetting('stop_message');
205 TwitchBotCommand::chat($channel->twitch_chat, $msg);
211 return $this->sendChannel($channel);
214 public function getGuessingGame(Channel $channel, $name) {
215 $this->authorize('editRestream', $channel);
217 $cutoff = $channel->guessing_start;
218 if (is_null($cutoff)) {
219 $last = $channel->winners()->latest()->first();
220 $cutoff = $last->pod;
222 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
223 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
225 'guesses' => $guesses->toArray(),
226 'winners' => $winners->toArray(),
230 public function getGuessingGameLeaderboard(Channel $channel, $type) {
232 'all' => $channel->getGuessingLeaderboard(),
236 public function getGuessingGameMonitor($key) {
237 $channel = Channel::where('access_key', '=', $key)->firstOrFail();
239 $cutoff = $channel->guessing_start;
240 if (is_null($cutoff)) {
241 $last = $channel->winners()->latest()->first();
242 $cutoff = $last->pod;
244 $guesses = $channel->guesses()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
245 $winners = $channel->winners()->where('created_at', '>=', $cutoff)->orderBy('created_at')->get();
247 'channel' => $channel->toArray(),
248 'guesses' => $guesses->toArray(),
249 'winners' => $winners->toArray(),
253 public function saveGuessingGame(Request $request, Channel $channel, $name) {
254 $this->authorize('editRestream', $channel);
256 $validatedData = $request->validate([
257 'active_message' => 'string',
258 'cancel_message' => 'string',
259 'close_winners_message' => 'string',
260 'invalid_solution_message' => 'string',
261 'leaderboard_type' => 'string',
262 'no_winners_message' => 'string',
263 'not_active_message' => 'string',
264 'points_exact_first' => 'numeric|min:1|max:5',
265 'points_exact_other' => 'numeric|min:0|max:5',
266 'points_close_first' => 'numeric|min:0|max:5',
267 'points_close_max' => 'integer|min:0',
268 'points_close_other' => 'numeric|min:0|max:5',
269 'start_message' => 'string',
270 'stop_message' => 'string',
271 'winners_message' => 'string',
274 $settings = $channel->guessing_settings;
275 $settings[$name] = $validatedData;
276 $channel->guessing_settings = $settings;
279 return $this->sendChannel($channel);
282 protected function sendChannel(Channel $channel) {
283 if (Gate::allows('editRestream', $channel)) {
284 $this->unhideChannel($channel);
286 return $channel->toJson();
289 protected function sendChannels(Collection $channels) {
290 foreach ($channels as $channel) {
291 if (Gate::allows('editRestream', $channel)) {
292 $this->unhideChannel($channel);
295 return $channels->toJson();
298 private function unhideChannel(Channel $channel) {
299 $channel->makeVisible([