]> git.localhorst.tv Git - alttp.git/commitdiff
track twitch token expiration
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 10 Apr 2024 07:29:22 +0000 (09:29 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 10 Apr 2024 07:29:22 +0000 (09:29 +0200)
app/Console/Commands/TwitchChannelInfo.php
app/Models/TwitchToken.php
app/TwitchBot/TwitchBot.php
database/migrations/2024_04_10_071945_store_twitch_token_expiry.php [new file with mode: 0644]

index 6a3261ca8a9eb1b72bb6e4a3dbd7d9768b88541e..7b373c8c94ed54fe611280078158799e35002775 100644 (file)
@@ -37,6 +37,10 @@ class TwitchChannelInfo extends Command {
                        $this->line('please acquire a token for localhorsttv first');
                        return 1;
                }
+               if ($this->token->hasExpired()) {
+                       $this->line('access token has expired, refreshing');
+                       $this->token->refresh();
+               }
                $channels = Channel::where('twitch_chat', '!=', '')->where('twitch_id', '=', '')->get();
                foreach ($channels as $channel) {
                        try {
@@ -52,7 +56,8 @@ class TwitchChannelInfo extends Command {
                        ->where(function($query) {
                                $query->where('join', true);
                                $query->orWhere('chat', true);
-                       })->chunk(100, function ($channels) {
+                       })
+                       ->chunk(100, function ($channels) {
                                $this->updateChannelInfos($channels);
                        });
                return Command::SUCCESS;
index a4c561eaffa40ce75cc49f5272cf13c8a1fb5b87..3a35d8c8ca0ef10606ebbeb517250e18349f34af 100644 (file)
@@ -18,6 +18,10 @@ class TwitchToken extends Model
                        .'&scope='.implode('+', array_map('rawurlencode', $this->scope));
        }
 
+       public function hasExpired() {
+               return now()->isAfter($this->expires_at);
+       }
+
        public function refresh() {
                $rsp = Http::post('https://id.twitch.tv/oauth2/token', [
                        'client_id' => config('twitch.client_id'),
@@ -32,6 +36,7 @@ class TwitchToken extends Model
                $this->scope = $rsp['scope'];
                $this->access = $rsp['access_token'];
                $this->refresh = $rsp['refresh_token'];
+               $this->expires_at = now()->add($rsp['expires_in'], 'second');
                $this->save();
        }
 
index e5a5aa6e442307b06bbae8b68635a07f4255b463..63f4236f685e92bf659610257e93177a78cf7a11 100644 (file)
@@ -23,6 +23,9 @@ class TwitchBot {
                if (!$this->token) {
                        throw new \Exception('unable to find access token');
                }
+               if ($this->token->hasExpired()) {
+                       $this->token->refresh();
+               }
 
                $this->connector = new Connector();
                $this->connect();
diff --git a/database/migrations/2024_04_10_071945_store_twitch_token_expiry.php b/database/migrations/2024_04_10_071945_store_twitch_token_expiry.php
new file mode 100644 (file)
index 0000000..a4ebcbf
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+       /**
+        * Run the migrations.
+        */
+       public function up(): void
+       {
+               Schema::table('twitch_tokens', function (Blueprint $table) {
+                       $table->timestamp('expires_at')->nullable()->default(null);
+               });
+       }
+
+       /**
+        * Reverse the migrations.
+        */
+       public function down(): void
+       {
+               Schema::table('twitch_tokens', function (Blueprint $table) {
+                       $table->dropColumn('expires_at');
+               });
+       }
+};