Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Software Update banner to admin home feeds #4934

Merged
merged 3 commits into from Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### Features
- Autospam Live Filters - block remote activities based on comma separated keywords ([40b45b2a](https://github.com/pixelfed/pixelfed/commit/40b45b2a))
- Added Software Update banner to admin home feeds ([b0fb1988](https://github.com/pixelfed/pixelfed/commit/b0fb1988))

### Updated

Expand Down
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)
],
];
2 changes: 2 additions & 0 deletions public/js/home.chunk.88eeebf6c53d4dca.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions public/js/home.chunk.ada2cbf0ec3271bd.js

This file was deleted.

2 changes: 1 addition & 1 deletion public/js/manifest.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions public/mix-manifest.json
Expand Up @@ -24,8 +24,8 @@
"/js/admin_invite.js": "/js/admin_invite.js?id=285b7aab611b66b12e16fb95c5cd6a24",
"/js/landing.js": "/js/landing.js?id=b87dd6bea094176c0eae6efc32dcedd4",
"/js/remote_auth.js": "/js/remote_auth.js?id=8a261c63271e4f126bc259ca94fe691c",
"/js/manifest.js": "/js/manifest.js?id=2a837f1e450c4546dcf143d383bd9b0b",
"/js/home.chunk.ada2cbf0ec3271bd.js": "/js/home.chunk.ada2cbf0ec3271bd.js?id=bb5799dbaac96798385031707a3d9c73",
"/js/manifest.js": "/js/manifest.js?id=76024a2046c3adf4a33bf6af500ccf86",
"/js/home.chunk.88eeebf6c53d4dca.js": "/js/home.chunk.88eeebf6c53d4dca.js?id=3693078e44228c96ef74e32bc7dd0ce1",
"/js/compose.chunk.1ac292c93b524406.js": "/js/compose.chunk.1ac292c93b524406.js?id=9f2ed8b63c8ccea71948decd16951945",
"/js/post.chunk.eb9804ff282909ae.js": "/js/post.chunk.eb9804ff282909ae.js?id=edfea2631b019f79bf694711b313ed69",
"/js/profile.chunk.d52916cb68c9a146.js": "/js/profile.chunk.d52916cb68c9a146.js?id=12cceca2cf40ade55dd52e55beaebcf5",
Expand Down
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