Skip to content

Commit 24a5493

Browse files
committed
Update shipped implementations of the INotifier
Signed-off-by: Joas Schilling <coding@schilljs.com>
1 parent 64c4684 commit 24a5493

File tree

6 files changed

+74
-36
lines changed

6 files changed

+74
-36
lines changed

apps/comments/lib/AppInfo/Application.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,7 @@ protected function registerDavEntity(EventDispatcherInterface $dispatcher) {
7676
}
7777

7878
protected function registerNotifier() {
79-
$this->getContainer()->getServer()->getNotificationManager()->registerNotifier(
80-
function() {
81-
return $this->getContainer()->query(Notifier::class);
82-
},
83-
function () {
84-
$l = $this->getContainer()->getServer()->getL10NFactory()->get('comments');
85-
return ['id' => 'comments', 'name' => $l->t('Comments')];
86-
}
87-
);
79+
$this->getContainer()->getServer()->getNotificationManager()->registerNotifier(Notifier::class);
8880
}
8981

9082
protected function registerCommentsEventHandler() {

apps/comments/lib/Notification/Notifier.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\IUser;
3333
use OCP\IUserManager;
3434
use OCP\L10N\IFactory;
35+
use OCP\Notification\AlreadyProcessedException;
3536
use OCP\Notification\INotification;
3637
use OCP\Notification\INotifier;
3738

@@ -66,13 +67,35 @@ public function __construct(
6667
$this->userManager = $userManager;
6768
}
6869

70+
/**
71+
* Identifier of the notifier, only use [a-z0-9_]
72+
*
73+
* @return string
74+
* @since 17.0.0
75+
*/
76+
public function getID(): string {
77+
return 'comments';
78+
}
79+
80+
/**
81+
* Human readable name describing the notifier
82+
*
83+
* @return string
84+
* @since 17.0.0
85+
*/
86+
public function getName(): string {
87+
return $this->l10nFactory->get('comments')->t('Comments');
88+
}
89+
6990
/**
7091
* @param INotification $notification
7192
* @param string $languageCode The code of the language that should be used to prepare the notification
7293
* @return INotification
7394
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
95+
* @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
96+
* @since 9.0.0
7497
*/
75-
public function prepare(INotification $notification, $languageCode) {
98+
public function prepare(INotification $notification, string $languageCode): INotification {
7699
if($notification->getApp() !== 'comments') {
77100
throw new \InvalidArgumentException();
78101
}
@@ -101,7 +124,7 @@ public function prepare(INotification $notification, $languageCode) {
101124
$userFolder = $this->rootFolder->getUserFolder($notification->getUser());
102125
$nodes = $userFolder->getById((int)$parameters[1]);
103126
if(empty($nodes)) {
104-
throw new \InvalidArgumentException('Cannot resolve file ID to node instance');
127+
throw new AlreadyProcessedException();
105128
}
106129
$node = $nodes[0];
107130

apps/updatenotification/lib/AppInfo/Application.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,6 @@ public function register() {
7171

7272
public function registerNotifier() {
7373
$notificationsManager = $this->getContainer()->getServer()->getNotificationManager();
74-
$notificationsManager->registerNotifier(function() {
75-
return $this->getContainer()->query(Notifier::class);
76-
}, function() {
77-
$l = $this->getContainer()->getServer()->getL10N('updatenotification');
78-
return [
79-
'id' => 'updatenotification',
80-
'name' => $l->t('Update notifications'),
81-
];
82-
});
74+
$notificationsManager->registerNotifier(Notifier::class);
8375
}
8476
}

apps/updatenotification/lib/Notification/Notifier.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use OCP\IUser;
3232
use OCP\IUserSession;
3333
use OCP\L10N\IFactory;
34+
use OCP\Notification\AlreadyProcessedException;
3435
use OCP\Notification\IManager;
3536
use OCP\Notification\INotification;
3637
use OCP\Notification\INotifier;
@@ -79,14 +80,35 @@ public function __construct(IURLGenerator $url, IConfig $config, IManager $notif
7980
$this->appVersions = $this->getAppVersions();
8081
}
8182

83+
/**
84+
* Identifier of the notifier, only use [a-z0-9_]
85+
*
86+
* @return string
87+
* @since 17.0.0
88+
*/
89+
public function getID(): string {
90+
return 'updatenotification';
91+
}
92+
93+
/**
94+
* Human readable name describing the notifier
95+
*
96+
* @return string
97+
* @since 17.0.0
98+
*/
99+
public function getName(): string {
100+
return $this->l10NFactory->get('updatenotification')->t('Update notifications');
101+
}
102+
82103
/**
83104
* @param INotification $notification
84105
* @param string $languageCode The code of the language that should be used to prepare the notification
85106
* @return INotification
86107
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
108+
* @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
87109
* @since 9.0.0
88110
*/
89-
public function prepare(INotification $notification, $languageCode): INotification {
111+
public function prepare(INotification $notification, string $languageCode): INotification {
90112
if ($notification->getApp() !== 'updatenotification') {
91113
throw new \InvalidArgumentException('Unknown app id');
92114
}
@@ -142,12 +164,11 @@ public function prepare(INotification $notification, $languageCode): INotificati
142164
*
143165
* @param INotification $notification
144166
* @param string $installedVersion
145-
* @throws \InvalidArgumentException When the update is already installed
167+
* @throws AlreadyProcessedException When the update is already installed
146168
*/
147169
protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) {
148170
if (version_compare($notification->getObjectId(), $installedVersion, '<=')) {
149-
$this->notificationManager->markProcessed($notification);
150-
throw new \InvalidArgumentException('Update already installed');
171+
throw new AlreadyProcessedException();
151172
}
152173
}
153174

apps/user_ldap/appinfo/app.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,7 @@
4242
$ldapWrapper = new OCA\User_LDAP\LDAP();
4343
$ocConfig = \OC::$server->getConfig();
4444
$notificationManager = \OC::$server->getNotificationManager();
45-
$notificationManager->registerNotifier(function() {
46-
return new \OCA\User_LDAP\Notification\Notifier(
47-
\OC::$server->getL10NFactory()
48-
);
49-
}, function() {
50-
$l = \OC::$server->getL10N('user_ldap');
51-
return [
52-
'id' => 'user_ldap',
53-
'name' => $l->t('LDAP user and group backend'),
54-
];
55-
});
45+
$notificationManager->registerNotifier(\OCA\User_LDAP\Notification\Notifier::class);
5646
$userSession = \OC::$server->getUserSession();
5747

5848
$userPluginManager = \OC::$server->query('LDAPUserPluginManager');

apps/user_ldap/lib/Notification/Notifier.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,33 @@ public function __construct(\OCP\L10N\IFactory $l10nFactory) {
4242
$this->l10nFactory = $l10nFactory;
4343
}
4444

45+
/**
46+
* Identifier of the notifier, only use [a-z0-9_]
47+
*
48+
* @return string
49+
* @since 17.0.0
50+
*/
51+
public function getID(): string {
52+
return 'user_ldap';
53+
}
54+
55+
/**
56+
* Human readable name describing the notifier
57+
*
58+
* @return string
59+
* @since 17.0.0
60+
*/
61+
public function getName(): string {
62+
return $this->l10nFactory->get('user_ldap')->t('LDAP User backend');
63+
}
64+
4565
/**
4666
* @param INotification $notification
4767
* @param string $languageCode The code of the language that should be used to prepare the notification
4868
* @return INotification
4969
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
5070
*/
51-
public function prepare(INotification $notification, $languageCode) {
71+
public function prepare(INotification $notification, string $languageCode): INotification {
5272
if ($notification->getApp() !== 'user_ldap') {
5373
// Not my app => throw
5474
throw new \InvalidArgumentException();

0 commit comments

Comments
 (0)