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

refactor(updatenotification): Migrate legacy code #48323

Merged
merged 2 commits into from
Sep 24, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/updatenotification/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
<job>OCA\UpdateNotification\BackgroundJob\UpdateAvailableNotifications</job>
</background-jobs>

<settings>
<admin>OCA\UpdateNotification\Settings\Admin</admin>
</settings>

<commands>
<command>OCA\UpdateNotification\Command\Check</command>
</commands>

<settings>
<admin>OCA\UpdateNotification\Settings\Admin</admin>
</settings>
</info>
15 changes: 0 additions & 15 deletions apps/updatenotification/js/legacy-notification.js

This file was deleted.

7 changes: 4 additions & 3 deletions apps/updatenotification/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function boot(IBootContext $context): void {
IAppManager $appManager,
IGroupManager $groupManager,
ContainerInterface $container,
LoggerInterface $logger): void {
LoggerInterface $logger,
): void {
if ($config->getSystemValue('updatechecker', true) !== true) {
// Updater check is disabled
return;
Expand All @@ -72,8 +73,8 @@ public function boot(IBootContext $context): void {
}

if ($updateChecker->getUpdateState() !== []) {
Util::addScript('updatenotification', 'legacy-notification');
\OC_Hook::connect('\OCP\Config', 'js', $updateChecker, 'populateJavaScriptVariables');
Util::addScript('updatenotification', 'update-notification-legacy');
$updateChecker->setInitialState();
}
}
});
Expand Down
32 changes: 16 additions & 16 deletions apps/updatenotification/lib/UpdateChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@

use OC\Updater\ChangesCheck;
use OC\Updater\VersionCheck;
use OCP\AppFramework\Services\IInitialState;

class UpdateChecker {
/** @var VersionCheck */
private $updater;
/** @var ChangesCheck */
private $changesCheck;

/**
* @param VersionCheck $updater
*/
public function __construct(VersionCheck $updater, ChangesCheck $changesCheck) {
$this->updater = $updater;
$this->changesCheck = $changesCheck;
public function __construct(
private VersionCheck $updater,
private ChangesCheck $changesCheck,
private IInitialState $initialState,
) {
}

/**
Expand Down Expand Up @@ -59,13 +55,17 @@ public function getUpdateState(): array {
}

/**
* @param array $data
* Provide update information as initial state
*/
public function populateJavaScriptVariables(array $data) {
$data['array']['oc_updateState'] = json_encode([
'updateAvailable' => true,
'updateVersion' => $this->getUpdateState()['updateVersionString'],
'updateLink' => $this->getUpdateState()['updateLink'] ?? '',
public function setInitialState(): void {
$updateState = $this->getUpdateState();
if (empty($updateState)) {
return;
}

$this->initialState->provideInitialState('updateState', [
'updateVersion' => $updateState['updateVersionString'],
'updateLink' => $updateState['updateLink'] ?? '',
]);
}
}
24 changes: 24 additions & 0 deletions apps/updatenotification/src/update-notification-legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the file still named with -legacy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Legacy did not meant the content but the usage: It is only needed if you do not have notifications app enabled.

We could also name it -fallback

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fallback would be clearer for me, but no big deal, was just my curiosity when reading this.

* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { showInfo } from '@nextcloud/dialogs'
import { loadState } from '@nextcloud/initial-state'
import { t } from '@nextcloud/l10n'

interface IUpdateNotificationState {
updateLink: string
updateVersion: string
}

/**
* This only gets loaded if an update is available and the notifications app is not enabled for the user.
*/
window.addEventListener('DOMContentLoaded', function() {
const { updateLink, updateVersion } = loadState<IUpdateNotificationState>('updatenotification', 'updateState')
const text = t('core', '{version} is available. Get more information on how to update.', { version: updateVersion })

// On click open the update link in a new tab
showInfo(text, { onClick: () => window.open(updateLink, '_blank') })
})
44 changes: 37 additions & 7 deletions apps/updatenotification/tests/UpdateCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,28 @@
use OC\Updater\ChangesCheck;
use OC\Updater\VersionCheck;
use OCA\UpdateNotification\UpdateChecker;
use OCP\AppFramework\Services\IInitialState;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class UpdateCheckerTest extends TestCase {
/** @var ChangesCheck|\PHPUnit\Framework\MockObject\MockObject */
protected $changesChecker;
/** @var VersionCheck|\PHPUnit\Framework\MockObject\MockObject */
private $updater;
/** @var UpdateChecker */
private $updateChecker;

private ChangesCheck&MockObject $changesChecker;
private VersionCheck&MockObject $updater;
private IInitialState&MockObject $initialState;
private UpdateChecker $updateChecker;

protected function setUp(): void {
parent::setUp();

$this->updater = $this->createMock(VersionCheck::class);
$this->changesChecker = $this->createMock(ChangesCheck::class);
$this->updateChecker = new UpdateChecker($this->updater, $this->changesChecker);
$this->initialState = $this->createMock(IInitialState::class);
$this->updateChecker = new UpdateChecker(
$this->updater,
$this->changesChecker,
$this->initialState,
);
}

public function testGetUpdateStateWithUpdateAndInvalidLink(): void {
Expand Down Expand Up @@ -110,4 +116,28 @@ public function testGetUpdateStateWithoutUpdate(): void {
$expected = [];
$this->assertSame($expected, $this->updateChecker->getUpdateState());
}

public function testSetInitialState(): void {
$this->updater
->expects($this->once())
->method('check')
->willReturn([
'version' => '1.2.3',
'versionstring' => 'Nextcloud 1.2.3',
'web' => 'https://docs.nextcloud.com/myUrl',
'url' => 'https://downloads.nextcloud.org/server',
'changes' => 'https://updates.nextcloud.com/changelog_server/?version=123.0.0',
'autoupdater' => '1',
'eol' => '0',
]);

$this->initialState->expects(self::once())
->method('provideInitialState')
->with('updateState', [
'updateVersion' => 'Nextcloud 1.2.3',
'updateLink' => 'https://docs.nextcloud.com/myUrl',
]);

$this->updateChecker->setInitialState();
}
}
4 changes: 2 additions & 2 deletions dist/federatedfilesharing-vue-settings-admin.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/federatedfilesharing-vue-settings-admin.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/files-search.js

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

2 changes: 1 addition & 1 deletion dist/files-search.js.map

Large diffs are not rendered by default.

Loading
Loading