1
0

First commit

This commit is contained in:
2020-04-29 23:58:18 +02:00
commit f3230a32ce
108 changed files with 77210 additions and 0 deletions

80
app/Console/Kernel.php Normal file
View File

@@ -0,0 +1,80 @@
<?php
namespace App\Console;
use App\TwitchWebhook;
use Carbon\Carbon;
use App\Http\Controllers\WebhookController;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use romanzipp\Twitch\Facades\Twitch;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// resubscribe to nearly expired hooks (1 hour after expire)
$schedule->call(function () {
$hooks = TwitchWebhook::where("disabled", false)->where("disabled_at", null)->whereDate("expires_at", "<=", Carbon::now()->addHours())->get();
foreach ($hooks as $hook) {
$subscription = Twitch::subscribeWebhook($hook->callback, $hook->type, WebhookController::MAX_LEASE_SECONDS);
if ($subscription->success) {
$hook->expires_at = Carbon::now()->addSeconds(WebhookController::MAX_LEASE_SECONDS);
$hook->save();
} else {
// TODO: handle error
}
}
})->everyThirtyMinutes();
// unsubscribe from disabled hooks (disabled 1 day ago)
$schedule->call(function () {
$hooks = TwitchWebhook::where("disabled", false)->whereDate("disabled_at", "<=", Carbon::now()->subDay())->get();
foreach ($hooks as $hook) {
$unsubscription = Twitch::unsubscribeWebhook($hook->callback, $hook->topic);
if ($unsubscription->success) {
$hook->disabled = true;
$hook->save();
} else {
// TODO: handle error
}
}
})->everyThirtyMinutes();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}