Skip to content

Commit 89e39e2

Browse files
authored
[Feature] Check for a new version and notify the admin(s) (alexjustesen#1005)
1 parent 8e655b0 commit 89e39e2

File tree

6 files changed

+80
-12
lines changed

6 files changed

+80
-12
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Models\User;
6+
use App\Services\SystemChecker;
7+
use Filament\Notifications\Actions\Action;
8+
use Filament\Notifications\Notification;
9+
use Illuminate\Console\Command;
10+
11+
class VersionChecker extends Command
12+
{
13+
/**
14+
* The name and signature of the console command.
15+
*
16+
* @var string
17+
*/
18+
protected $signature = 'app:version';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Sends a notification to the admin users when Speedtest Tracker is outdated.';
26+
27+
/**
28+
* Execute the console command.
29+
*/
30+
public function handle()
31+
{
32+
$system = new SystemChecker;
33+
34+
if (! $system->isOutOfDate()) {
35+
return Command::SUCCESS;
36+
}
37+
38+
$admins = User::select(['id', 'name', 'email', 'role'])
39+
->where('role', 'admin')
40+
->get();
41+
42+
foreach ($admins as $user) {
43+
Notification::make()
44+
->title('Update Available')
45+
->body("There's a new version of Speedtest Tracker available: {$system->getRemoteVersion()}")
46+
->info()
47+
->actions([
48+
Action::make('view releases')
49+
->button()
50+
->color('gray')
51+
->url('https://github.com/alexjustesen/speedtest-tracker/releases')
52+
->openUrlInNewTab(),
53+
])
54+
->sendToDatabase($user);
55+
}
56+
57+
return Command::SUCCESS;
58+
}
59+
}

app/Console/Kernel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Console;
44

5+
use App\Console\Commands\VersionChecker;
56
use App\Jobs\ExecSpeedtest;
67
use App\Settings\GeneralSettings;
78
use Cron\CronExpression;
@@ -17,6 +18,13 @@ protected function schedule(Schedule $schedule): void
1718
{
1819
$settings = new GeneralSettings();
1920

21+
/**
22+
* Checked for new versions weekly on Thursday because
23+
* I usually do releases on Thursday or Friday.
24+
*/
25+
$schedule->command(VersionChecker::class)->weeklyOn(5)
26+
->timezone($settings->timezone ?? 'UTC');
27+
2028
/**
2129
* Check for speedtests that need to run.
2230
*/

app/Filament/VersionProviders/SpeedtestTrackerVersionProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use App\Services\SystemChecker;
34
use Awcodes\FilamentVersions\Providers\Contracts\VersionProvider;
45

56
class SpeedtestTrackerVersionProvider implements VersionProvider
@@ -12,7 +13,7 @@ public function getName(): string
1213
public function getVersion(): string
1314
{
1415
return app()->isProduction()
15-
? config('speedtest.build_version')
16+
? app(new SystemChecker)->getLocalVersion()
1617
: config('app.env');
1718
}
1819
}

app/Providers/AppServiceProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Providers;
44

5+
use App\Services\SystemChecker;
6+
use Illuminate\Foundation\Console\AboutCommand;
57
use Illuminate\Support\Facades\URL;
68
use Illuminate\Support\ServiceProvider;
79

@@ -28,5 +30,12 @@ public function boot(): void
2830
if (config('app.force_https')) {
2931
URL::forceScheme('https');
3032
}
33+
34+
$system = new SystemChecker;
35+
36+
AboutCommand::add('Speedtest Tracker', fn () => [
37+
'Version' => $system->getLocalVersion(),
38+
'Out of date' => $system->isOutOfDate() ? 'Yes' : 'No',
39+
]);
3140
}
3241
}

app/Services/SystemChecker.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace App\Services;
44

5-
use Illuminate\Support\Facades\Cache;
6-
75
/**
86
* 🤔 inspired by: https://github.com/ploi/roadmap/blob/main/app/Services/SystemChecker.php
97
*/
@@ -50,9 +48,9 @@ public function isOutOfDate()
5048
public function flushVersionData()
5149
{
5250
try {
53-
Cache::forget($this->cacheKeyLocal);
51+
cache()->forget($this->cacheKeyLocal);
5452

55-
Cache::forget($this->cacheKeyRemote);
53+
cache()->forget($this->cacheKeyRemote);
5654
} catch (\Exception $exception) {
5755
// fail silently, it's ok
5856
}

config/speedtest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33
use Carbon\Carbon;
44

55
return [
6-
/**
7-
* Build information
8-
*/
9-
'build_date' => Carbon::parse('2023-12-20'),
10-
11-
'build_version' => '0.14.1',
12-
136
/**
147
* General
158
*/

0 commit comments

Comments
 (0)