Skip to content

Commit

Permalink
Add Software Update banner to admin home feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
dansup committed Feb 16, 2024
1 parent 83eadbb commit b0fb198
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 3 deletions.
21 changes: 21 additions & 0 deletions app/Http/Controllers/SoftwareUpdateController.php
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\Internal\SoftwareUpdateService;

class SoftwareUpdateController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('admin');
}

public function getSoftwareUpdateCheck(Request $request)
{
$res = SoftwareUpdateService::get();
return $res;
}
}
68 changes: 68 additions & 0 deletions app/Services/Internal/SoftwareUpdateService.php
@@ -0,0 +1,68 @@
<?php

namespace App\Services\Internal;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;

class SoftwareUpdateService
{
const CACHE_KEY = 'pf:services:software-update:';

public static function get()
{
$curVersion = config('pixelfed.version');

$versions = Cache::remember(self::CACHE_KEY . 'latest:v1.0.0', 1800, function() {
return self::fetchLatest();
});

if(!$versions || !isset($versions['latest'], $versions['latest']['version'])) {
$hideWarning = (bool) config('instance.software-update.disable_failed_warning');
return [
'current' => $curVersion,
'latest' => [
'version' => null,
'published_at' => null,
'url' => null,
],
'running_latest' => $hideWarning ? true : null
];
}

return [
'current' => $curVersion,
'latest' => [
'version' => $versions['latest']['version'],
'published_at' => $versions['latest']['published_at'],
'url' => $versions['latest']['url'],
],
'running_latest' => strval($versions['latest']['version']) === strval($curVersion)
];
}

public static function fetchLatest()
{
try {
$res = Http::withOptions(['allow_redirects' => false])
->timeout(5)
->connectTimeout(5)
->retry(2, 500)
->get('https://versions.pixelfed.org/versions.json');
} catch (RequestException $e) {
return;
} catch (ConnectionException $e) {
return;
} catch (Exception $e) {
return;
}

if(!$res->ok()) {
return;
}

return $res->json();
}
}
6 changes: 5 additions & 1 deletion config/instance.php
Expand Up @@ -140,5 +140,9 @@
'max_children' => env('INSTANCE_PARENTAL_CONTROLS_MAX_CHILDREN', 1),
'auto_verify_email' => true,
],
]
],

'software-update' => [
'disable_failed_warning' => env('INSTANCE_SOFTWARE_UPDATE_DISABLE_FAILED_WARNING', false)
],
];
71 changes: 69 additions & 2 deletions resources/assets/components/Home.vue
Expand Up @@ -9,6 +9,48 @@
</div>

<div class="col-md-8 col-lg-6 px-0">
<template v-if="showUpdateWarning && updateInfo && updateInfo.hasOwnProperty('running_latest')">
<div class="card rounded-lg mb-4 ft-std" style="background: #e11d48;border: 3px dashed #fff">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center flex-column flex-lg-row" style="gap:1rem">
<div class="d-flex justify-content-between align-items-center" style="gap:1rem">
<i class="d-none d-sm-block far fa-exclamation-triangle fa-5x text-white"></i>

<div>
<h1 class="h3 font-weight-bold text-light mb-0">New Update Available</h1>
<p class="mb-0 text-white" style="font-size:18px;">Update your Pixelfed server as soon as possible!</p>
<p class="mb-n1 text-white small" style="opacity:.7">Once you update, this message will disappear.</p>
<p class="mb-0 text-white small d-flex" style="opacity:.5;gap:1rem;">
<span>Current version: <strong>{{ updateInfo?.current ?? 'Unknown' }}</strong></span>
<span>Latest version: <strong>{{ updateInfo?.latest?.version ?? 'Unknown' }}</strong></span>
</p>
</div>
</div>

<a v-if="updateInfo.latest.url" class="btn btn-light font-weight-bold" :href="updateInfo.latest.url" target="_blank">View Update</a>
</div>
</div>
</div>
</template>
<template v-if="showUpdateConnectionWarning">
<div class="card rounded-lg mb-4 ft-std" style="background: #e11d48;border: 3px dashed #fff">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center flex-column flex-lg-row" style="gap:1rem">
<div class="d-flex justify-content-between align-items-center" style="gap:1rem">
<i class="d-none d-sm-block far fa-exclamation-triangle fa-5x text-white"></i>

<div>
<h1 class="h3 font-weight-bold text-light mb-1">Software Update Check Failed</h1>
<p class="mb-1 text-white" style="font-size:18px;line-height: 1.2;">We attempted to check if there is a new version available, however we encountered an error. <a href="https://github.com/pixelfed/pixelfed/releases" class="text-white font-weight-bold" style="text-decoration: underline;" target="_blank">Click here</a> to view the latest releases.</p>
<p class="mb-0 text-white small">You can set <code class="text-white">INSTANCE_SOFTWARE_UPDATE_DISABLE_FAILED_WARNING=true</code> to remove this warning.</p>
<p class="mb-0 text-white small" style="opacity:.7">Current version: {{ updateInfo?.current ?? 'Unknown' }}</p>
</div>
</div>
</div>
</div>
</div>
</template>

<story-carousel
v-if="storiesEnabled"
:profile="profile" />
Expand Down Expand Up @@ -59,15 +101,17 @@
"rightbar": Rightbar,
"story-carousel": StoryCarousel,
},
data() {
return {
isLoaded: false,
profile: undefined,
recommended: [],
trending: [],
storiesEnabled: false,
shouldRefresh: false
shouldRefresh: false,
showUpdateWarning: false,
showUpdateConnectionWarning: false,
updateInfo: undefined,
}
},

Expand All @@ -84,10 +128,33 @@
this.profile = window._sharedData.user;
this.isLoaded = true;
this.storiesEnabled = window.App?.config?.features?.hasOwnProperty('stories') ? window.App.config.features.stories : false;

if(this.profile.is_admin) {
this.softwareUpdateCheck();
}
},

updateProfile(delta) {
this.profile = Object.assign(this.profile, delta);
},

softwareUpdateCheck() {
axios.get('/api/web-admin/software-update/check')
.then(res => {
if(!res || !res.data || !res.data.hasOwnProperty('running_latest') || res.data.running_latest) {
return;
}
if(res.data.running_latest === null) {
this.updateInfo = res.data;
this.showUpdateConnectionWarning = true;
return;
}
this.updateInfo = res.data;
this.showUpdateWarning = !res.data.running_latest;
})
.catch(err => {
this.showUpdateConnectionWarning = true;
})
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions routes/web-api.php
@@ -1,12 +1,18 @@
<?php

use App\Http\Controllers\SoftwareUpdateController;

Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofactor', 'localization'])->group(function () {
Route::group(['prefix' => 'api'], function () {
Route::get('search', 'SearchController@searchAPI');
Route::post('status/view', 'StatusController@storeView');
Route::get('v1/polls/{id}', 'PollController@getPoll');
Route::post('v1/polls/{id}/votes', 'PollController@vote');

Route::group(['prefix' => 'web-admin'], function() {
Route::get('software-update/check', [SoftwareUpdateController::class, 'getSoftwareUpdateCheck']);
});

Route::group(['prefix' => 'compose'], function() {
Route::group(['prefix' => 'v0'], function() {
Route::post('/media/upload', 'ComposeController@mediaUpload');
Expand Down

0 comments on commit b0fb198

Please sign in to comment.