Skip to content

Commit 0885050

Browse files
committed
Refactor all the logic from app.php to Application class
Signed-off-by: Joas Schilling <[email protected]>
1 parent 50e1cee commit 0885050

File tree

3 files changed

+63
-43
lines changed

3 files changed

+63
-43
lines changed
Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,25 @@
11
<?php
22
/**
3-
* @copyright Copyright (c) 2016, ownCloud, Inc.
3+
* @copyright Copyright (c) 2018, Joas Schilling <[email protected]>
44
*
55
* @author Joas Schilling <[email protected]>
6-
* @author Lukas Reschke <[email protected]>
7-
* @author Morris Jobke <[email protected]>
8-
* @author Thomas Müller <[email protected]>
96
*
10-
* @license AGPL-3.0
7+
* @license GNU AGPL version 3 or any later version
118
*
12-
* This code is free software: you can redistribute it and/or modify
13-
* it under the terms of the GNU Affero General Public License, version 3,
14-
* as published by the Free Software Foundation.
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
1513
*
1614
* This program is distributed in the hope that it will be useful,
1715
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1917
* GNU Affero General Public License for more details.
2018
*
21-
* You should have received a copy of the GNU Affero General Public License, version 3,
22-
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2321
*
2422
*/
2523

26-
if(\OC::$server->getConfig()->getSystemValue('updatechecker', true) === true) {
27-
$updater = new \OC\Updater\VersionCheck(
28-
\OC::$server->getHTTPClientService(),
29-
\OC::$server->getConfig()
30-
);
31-
$updateChecker = new \OCA\UpdateNotification\UpdateChecker(
32-
$updater
33-
);
34-
35-
$userObject = \OC::$server->getUserSession()->getUser();
36-
if($userObject !== null) {
37-
if(\OC::$server->getGroupManager()->isAdmin($userObject->getUID()) &&
38-
!\OC::$server->getAppManager()->isEnabledForUser('notifications')) {
39-
if($updateChecker->getUpdateState() !== []) {
40-
\OCP\Util::addScript('updatenotification', 'notification');
41-
OC_Hook::connect('\OCP\Config', 'js', $updateChecker, 'getJavaScript');
42-
}
43-
}
44-
}
45-
46-
$manager = \OC::$server->getNotificationManager();
47-
$manager->registerNotifier(function() {
48-
return \OC::$server->query(\OCA\UpdateNotification\Notification\Notifier::class);
49-
}, function() {
50-
$l = \OC::$server->getL10N('updatenotification');
51-
return [
52-
'id' => 'updatenotification',
53-
'name' => $l->t('Update notifications'),
54-
];
55-
});
56-
}
24+
$app = new \OCA\UpdateNotification\AppInfo\Application();
25+
$app->register();

apps/updatenotification/lib/AppInfo/Application.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,61 @@
2323

2424
namespace OCA\UpdateNotification\AppInfo;
2525

26+
use OCA\UpdateNotification\Notification\Notifier;
27+
use OCA\UpdateNotification\UpdateChecker;
2628
use OCP\AppFramework\App;
29+
use OCP\AppFramework\QueryException;
30+
use OCP\IUser;
31+
use OCP\Util;
2732

2833
class Application extends App {
2934
public function __construct() {
3035
parent::__construct('updatenotification', []);
3136
}
37+
38+
public function register() {
39+
$server = $this->getContainer()->getServer();
40+
41+
if ($server->getConfig()->getSystemValue('updatechecker', true) !== true) {
42+
// Updater check is disabled
43+
return;
44+
}
45+
46+
$user = $server->getUserSession()->getUser();
47+
if (!$user instanceof IUser) {
48+
// Nothing to do for guests
49+
return;
50+
}
51+
52+
if ($server->getAppManager()->isEnabledForUser('notifications')) {
53+
// Notifications app is available, so we register.
54+
// Since notifications also work for non-admins we don't check this here.
55+
$this->registerNotifier();
56+
} else if ($server->getGroupManager()->isAdmin($user->getUID())) {
57+
try {
58+
$updateChecker = $this->getContainer()->query(UpdateChecker::class);
59+
} catch (QueryException $e) {
60+
$server->getLogger()->logException($e);
61+
return;
62+
}
63+
64+
if ($updateChecker->getUpdateState() !== []) {
65+
Util::addScript('updatenotification', 'notification');
66+
\OC_Hook::connect('\OCP\Config', 'js', $updateChecker, 'populateJavaScriptVariables');
67+
}
68+
}
69+
}
70+
71+
public function registerNotifier() {
72+
$notificationsManager = $this->getContainer()->getServer()->getNotificationManager();
73+
$notificationsManager->registerNotifier(function() {
74+
return $this->getContainer()->query(Notifier::class);
75+
}, function() {
76+
$l = $this->getContainer()->getServer()->getL10N('updatenotification');
77+
return [
78+
'id' => 'updatenotification',
79+
'name' => $l->t('Update notifications'),
80+
];
81+
});
82+
}
3283
}

apps/updatenotification/lib/UpdateChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function getUpdateState() {
6464
/**
6565
* @param array $data
6666
*/
67-
public function getJavaScript(array $data) {
67+
public function populateJavaScriptVariables(array $data) {
6868
$data['array']['oc_updateState'] = json_encode([
6969
'updateAvailable' => true,
7070
'updateVersion' => $this->getUpdateState()['updateVersion'],

0 commit comments

Comments
 (0)