From a217d21a34877a0de1394e00bd21b482d25011b8 Mon Sep 17 00:00:00 2001 From: Kovah Date: Fri, 26 Feb 2021 23:22:15 +0100 Subject: [PATCH] Add version information and update check to footer (#220) --- app/Helper/UpdateHelper.php | 44 ++++++++++++---- app/Helper/functions.php | 16 ------ .../App/SystemSettingsController.php | 3 +- resources/assets/js/components/UpdateCheck.js | 10 ++-- resources/assets/js/components/UrlField.js | 6 +-- resources/docker/php/php-dev.ini | 2 +- .../partials/system/updates.blade.php | 10 +--- .../views/components/update-check.blade.php | 6 +++ resources/views/partials/footer.blade.php | 7 ++- tests/Helper/HelperFunctionsTest.php | 27 ---------- tests/Helper/UpdateCheckTest.php | 51 +++++++++++++++---- webpack.mix.js | 1 + 12 files changed, 99 insertions(+), 84 deletions(-) create mode 100644 resources/views/components/update-check.blade.php diff --git a/app/Helper/UpdateHelper.php b/app/Helper/UpdateHelper.php index 70f57659..81af137f 100644 --- a/app/Helper/UpdateHelper.php +++ b/app/Helper/UpdateHelper.php @@ -3,7 +3,10 @@ namespace App\Helper; use Composer\Semver\Comparator; +use Exception; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Storage; /** * Class UpdateHelper @@ -14,28 +17,49 @@ class UpdateHelper { protected static $releaseApiUrl = 'https://api.github.com/repos/kovah/linkace/releases'; + /** + * Get the current version from the package.json file and cache it for a day. + * + * @return mixed + */ + public static function currentVersion() + { + return Cache::remember('current-version', 86400, function () { + try { + $package = json_decode(Storage::disk('root')->get('package.json'), false); + } catch (Exception $e) { + return null; + } + + return isset($package->version) ? 'v' . $package->version : null; + }); + } + /** * Returns the version string if there is a newer version is available. * Returns true if the check was successful, but no updates was found. * Returns false if the check could not be executed, e.g. due to network * issues. * + * @param bool $cacheResult * @return bool|string */ - public static function checkForUpdates() + public static function checkForUpdates(bool $cacheResult = false) { - $currentVersion = getVersionFromPackage(); - $latestVersion = self::getCurrentVersionFromAPI(); + return Cache::remember('updatecheck', $cacheResult ? 86400 : 0, function () { + $currentVersion = self::currentVersion(); + $latestVersion = self::getCurrentVersionFromAPI(); - if ($latestVersion === null) { - return false; - } + if ($latestVersion === null) { + return false; + } - if (Comparator::greaterThan($latestVersion, $currentVersion)) { - return $latestVersion; - } + if (Comparator::greaterThan($latestVersion, $currentVersion)) { + return $latestVersion; + } - return true; + return true; + }); } /** diff --git a/app/Helper/functions.php b/app/Helper/functions.php index 338e3ad8..de82c794 100644 --- a/app/Helper/functions.php +++ b/app/Helper/functions.php @@ -207,22 +207,6 @@ function linkTarget(): string return usersettings('links_new_tab') ? $newTab : ''; } -/** - * Get the current version from the package.json file - * - * @return string|null - */ -function getVersionFromPackage(): ?string -{ - try { - $package = json_decode(Storage::disk('root')->get('package.json'), false); - } catch (Exception $e) { - return null; - } - - return isset($package->version) ? 'v' . $package->version : null; -} - /** * Properly escape symbols used in search queries. * diff --git a/app/Http/Controllers/App/SystemSettingsController.php b/app/Http/Controllers/App/SystemSettingsController.php index 90e1185f..7a8c64f6 100644 --- a/app/Http/Controllers/App/SystemSettingsController.php +++ b/app/Http/Controllers/App/SystemSettingsController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\App; +use App\Helper\UpdateHelper; use App\Http\Controllers\Controller; use App\Http\Requests\SystemSettingsUpdateRequest; use App\Models\Setting; @@ -22,7 +23,7 @@ class SystemSettingsController extends Controller public function getSystemSettings(): View { return view('actions.settings.system', [ - 'linkaceVersion' => getVersionFromPackage(), + 'linkaceVersion' => UpdateHelper::currentVersion(), ]); } diff --git a/resources/assets/js/components/UpdateCheck.js b/resources/assets/js/components/UpdateCheck.js index 2ddfa2e6..c7de1c41 100644 --- a/resources/assets/js/components/UpdateCheck.js +++ b/resources/assets/js/components/UpdateCheck.js @@ -17,12 +17,10 @@ export default class UpdateCheck { } init () { - debounce(() => { - this.checkForUpdate().then(result => { - this.result = result; - this.updateCheckStatus(); - }); - }, 500); + this.checkForUpdate().then(result => { + this.result = result; + this.updateCheckStatus(); + }); } updateCheckStatus () { diff --git a/resources/assets/js/components/UrlField.js b/resources/assets/js/components/UrlField.js index cccd3923..aa94e45d 100644 --- a/resources/assets/js/components/UrlField.js +++ b/resources/assets/js/components/UrlField.js @@ -9,10 +9,8 @@ export default class UrlField { this.$field.addEventListener('keyup', this.onKeyup.bind(this)); - this.tagSuggestions = getInstance( - document.querySelector('#tags'), - TagsSelect - ); + const $tags = document.querySelector('#tags'); + this.tagSuggestions = $tags ? getInstance($tags, TagsSelect) : null; } onKeyup () { diff --git a/resources/docker/php/php-dev.ini b/resources/docker/php/php-dev.ini index e4e053fb..5bd4ef68 100644 --- a/resources/docker/php/php-dev.ini +++ b/resources/docker/php/php-dev.ini @@ -28,7 +28,7 @@ xdebug.scream = 0 xdebug.cli_color = 1 xdebug.show_local_vars = 1 -xdebug.mode = profile +xdebug.mode = off xdebug.profiler_enable = 0 xdebug.profiler_enable_trigger=1 xdebug.profiler_enable_trigger_value=1 diff --git a/resources/views/actions/settings/partials/system/updates.blade.php b/resources/views/actions/settings/partials/system/updates.blade.php index f6c015fd..686b519f 100644 --- a/resources/views/actions/settings/partials/system/updates.blade.php +++ b/resources/views/actions/settings/partials/system/updates.blade.php @@ -2,14 +2,8 @@
@lang('settings.update_check')
-
- +

@lang('linkace.version', ['version' => $linkaceVersion])

- -
@lang('settings.update_check_running')
-
@lang('settings.update_check_version_found')
-
@lang('settings.update_check_success')
-
@lang('settings.update_check_failed')
- +
diff --git a/resources/views/components/update-check.blade.php b/resources/views/components/update-check.blade.php new file mode 100644 index 00000000..b1a5b638 --- /dev/null +++ b/resources/views/components/update-check.blade.php @@ -0,0 +1,6 @@ +
merge(['class' => 'update-check']) }} data-current-version="{{ \App\Helper\UpdateHelper::currentVersion() }}"> +
@lang('settings.update_check_running')
+
@lang('settings.update_check_version_found')
+
@lang('settings.update_check_success')
+
@lang('settings.update_check_failed')
+
diff --git a/resources/views/partials/footer.blade.php b/resources/views/partials/footer.blade.php index 21b3c9a5..fde3a1c4 100644 --- a/resources/views/partials/footer.blade.php +++ b/resources/views/partials/footer.blade.php @@ -1,3 +1,8 @@ diff --git a/tests/Helper/HelperFunctionsTest.php b/tests/Helper/HelperFunctionsTest.php index 26ebe14e..8b1ec13e 100644 --- a/tests/Helper/HelperFunctionsTest.php +++ b/tests/Helper/HelperFunctionsTest.php @@ -136,31 +136,4 @@ class HelperFunctionsTest extends TestCase $this->assertNull($link); } - - public function testVersionFromPackage(): void - { - Storage::fake('root')->put('package.json', '{"version":"0.0.39"}'); - - $version = getVersionFromPackage(); - - $this->assertEquals('v0.0.39', $version); - } - - public function testVersionFromPackageWithInvalidFile(): void - { - Storage::fake('root')->put('package.json', '{"foo":"bar"}'); - - $version = getVersionFromPackage(); // should now return null because there is no version field - - $this->assertNull($version); - } - - public function testVersionFromPackageWithMissingFile(): void - { - Storage::fake('root'); - - $version = getVersionFromPackage(); // should now return null because there is no package.json - - $this->assertNull($version); - } } diff --git a/tests/Helper/UpdateCheckTest.php b/tests/Helper/UpdateCheckTest.php index 2beab74d..082dc009 100644 --- a/tests/Helper/UpdateCheckTest.php +++ b/tests/Helper/UpdateCheckTest.php @@ -4,15 +4,14 @@ namespace Tests\Helper; use App\Helper\UpdateHelper; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Storage; use Tests\TestCase; class UpdateCheckTest extends TestCase { - /** + /* * Test the checkForUpdates() helper function with a new update available. * Must return the given version string. - * - * @return void */ public function testSuccessfulCheck(): void { @@ -28,11 +27,9 @@ class UpdateCheckTest extends TestCase $this->assertEquals('v100.0.0', $result); } - /** + /* * Test the checkForUpdates() helper function with no update available. * Must return true. - * - * @return void */ public function testSuccessfulCheckWithoutVersion(): void { @@ -48,13 +45,11 @@ class UpdateCheckTest extends TestCase $this->assertTrue($result); } - /** + /* * Test the checkForUpdates() helper function, but trigger a network / http error. * Must return false. - * - * @return void */ - public function testValidWaybackLink(): void + public function testUpdateCheckWithNetworkError(): void { Http::fake([ 'github.com/*' => Http::response([], 404), @@ -64,4 +59,40 @@ class UpdateCheckTest extends TestCase $this->assertFalse($result); } + + /* + * Test if the UpdateHelper correctly returns a version from the package.json file. + */ + public function testVersionFromPackage(): void + { + Storage::fake('root')->put('package.json', '{"version":"0.0.39"}'); + + $version = UpdateHelper::currentVersion(); + + $this->assertEquals('v0.0.39', $version); + } + + /* + * The UpdateHelper should return null if there is no version field. + */ + public function testVersionFromPackageWithInvalidFile(): void + { + Storage::fake('root')->put('package.json', '{"foo":"bar"}'); + + $version = UpdateHelper::currentVersion(); + + $this->assertNull($version); + } + + /* + * The UpdateHelper should return null if no package.json file was found. + */ + public function testVersionFromPackageWithMissingFile(): void + { + Storage::fake('root'); + + $version = UpdateHelper::currentVersion(); + + $this->assertNull($version); + } } diff --git a/webpack.mix.js b/webpack.mix.js index 44217a4d..431e133d 100644 --- a/webpack.mix.js +++ b/webpack.mix.js @@ -9,6 +9,7 @@ mix.disableNotifications(); mix.setPublicPath('public'); mix.js('resources/assets/js/app.js', 'assets/dist/js') + .sourceMaps() .version(); mix.combine([