Skip to content

Commit

Permalink
Merge pull request #15040 from nextcloud/feature/13980/push-for-delet…
Browse files Browse the repository at this point in the history
…ed-notifications

Notifications overhaul
  • Loading branch information
MorrisJobke committed Jul 17, 2019
2 parents 782554d + 594efca commit 5b604ea
Show file tree
Hide file tree
Showing 36 changed files with 712 additions and 735 deletions.
10 changes: 1 addition & 9 deletions apps/comments/lib/AppInfo/Application.php
Expand Up @@ -76,15 +76,7 @@ protected function registerDavEntity(EventDispatcherInterface $dispatcher) {
}

protected function registerNotifier() {
$this->getContainer()->getServer()->getNotificationManager()->registerNotifier(
function() {
return $this->getContainer()->query(Notifier::class);
},
function () {
$l = $this->getContainer()->getServer()->getL10NFactory()->get('comments');
return ['id' => 'comments', 'name' => $l->t('Comments')];
}
);
$this->getContainer()->getServer()->getNotificationManager()->registerNotifierService(Notifier::class);
}

protected function registerCommentsEventHandler() {
Expand Down
27 changes: 25 additions & 2 deletions apps/comments/lib/Notification/Notifier.php
Expand Up @@ -32,6 +32,7 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Notification\AlreadyProcessedException;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;

Expand Down Expand Up @@ -66,13 +67,35 @@ public function __construct(
$this->userManager = $userManager;
}

/**
* Identifier of the notifier, only use [a-z0-9_]
*
* @return string
* @since 17.0.0
*/
public function getID(): string {
return 'comments';
}

/**
* Human readable name describing the notifier
*
* @return string
* @since 17.0.0
*/
public function getName(): string {
return $this->l10nFactory->get('comments')->t('Comments');
}

/**
* @param INotification $notification
* @param string $languageCode The code of the language that should be used to prepare the notification
* @return INotification
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
* @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
* @since 9.0.0
*/
public function prepare(INotification $notification, $languageCode) {
public function prepare(INotification $notification, string $languageCode): INotification {
if($notification->getApp() !== 'comments') {
throw new \InvalidArgumentException();
}
Expand Down Expand Up @@ -101,7 +124,7 @@ public function prepare(INotification $notification, $languageCode) {
$userFolder = $this->rootFolder->getUserFolder($notification->getUser());
$nodes = $userFolder->getById((int)$parameters[1]);
if(empty($nodes)) {
throw new \InvalidArgumentException('Cannot resolve file ID to node instance');
throw new AlreadyProcessedException();
}
$node = $nodes[0];

Expand Down
6 changes: 6 additions & 0 deletions apps/comments/tests/Unit/Controller/NotificationsTest.php
Expand Up @@ -117,6 +117,9 @@ public function testViewSuccess() {
$comment->expects($this->any())
->method('getObjectType')
->willReturn('files');
$comment->expects($this->any())
->method('getId')
->willReturn('1234');

$this->commentsManager->expects($this->any())
->method('get')
Expand Down Expand Up @@ -192,6 +195,9 @@ public function testViewNoFile() {
$comment->expects($this->any())
->method('getObjectType')
->willReturn('files');
$comment->expects($this->any())
->method('getId')
->willReturn('1234');

$this->commentsManager->expects($this->any())
->method('get')
Expand Down
6 changes: 6 additions & 0 deletions apps/comments/tests/Unit/Notification/ListenerTest.php
Expand Up @@ -91,6 +91,9 @@ public function testEvaluate($eventType, $notificationMethod) {
[ 'type' => 'user', 'id' => '23452-4333-54353-2342'],
[ 'type' => 'user', 'id' => 'yolo'],
]);
$comment->expects($this->atLeastOnce())
->method('getId')
->willReturn('1234');

/** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */
$event = $this->getMockBuilder(CommentsEvent::class)
Expand Down Expand Up @@ -186,6 +189,9 @@ public function testEvaluateUserDoesNotExist() {
$comment->expects($this->once())
->method('getMentions')
->willReturn([[ 'type' => 'user', 'id' => 'foobar']]);
$comment->expects($this->atLeastOnce())
->method('getId')
->willReturn('1234');

/** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */
$event = $this->getMockBuilder(CommentsEvent::class)
Expand Down
5 changes: 4 additions & 1 deletion apps/comments/tests/Unit/Notification/NotifierTest.php
Expand Up @@ -195,6 +195,9 @@ public function testPrepareSuccess() {
->expects($this->any())
->method('getMentions')
->willReturn([['type' => 'user', 'id' => 'you']]);
$this->comment->expects($this->atLeastOnce())
->method('getId')
->willReturn('1234');

$this->commentsManager
->expects($this->once())
Expand Down Expand Up @@ -539,7 +542,7 @@ public function testPrepareNotFiles() {
}

/**
* @expectedException \InvalidArgumentException
* @expectedException \OCP\Notification\AlreadyProcessedException
*/
public function testPrepareUnresolvableFileID() {
$displayName = 'Huraga';
Expand Down
10 changes: 1 addition & 9 deletions apps/federatedfilesharing/appinfo/app.php
Expand Up @@ -29,15 +29,7 @@
$eventDispatcher = \OC::$server->getEventDispatcher();

$manager = \OC::$server->getNotificationManager();
$manager->registerNotifier(function() {
return \OC::$server->query(Notifier::class);
}, function() {
$l = \OC::$server->getL10N('files_sharing');
return [
'id' => 'files_sharing',
'name' => $l->t('Federated sharing'),
];
});
$manager->registerNotifierService(Notifier::class);

$federatedShareProvider = $app->getFederatedShareProvider();

Expand Down
22 changes: 21 additions & 1 deletion apps/federatedfilesharing/lib/Notifier.php
Expand Up @@ -59,13 +59,33 @@ public function __construct(IFactory $factory, IManager $contactsManager, IURLGe
$this->cloudIdManager = $cloudIdManager;
}

/**
* Identifier of the notifier, only use [a-z0-9_]
*
* @return string
* @since 17.0.0
*/
public function getID(): string {
return 'federatedfilesharing';
}

/**
* Human readable name describing the notifier
*
* @return string
* @since 17.0.0
*/
public function getName(): string {
return $this->factory->get('federatedfilesharing')->t('Federated sharing');
}

/**
* @param INotification $notification
* @param string $languageCode The code of the language that should be used to prepare the notification
* @return INotification
* @throws \InvalidArgumentException
*/
public function prepare(INotification $notification, $languageCode) {
public function prepare(INotification $notification, string $languageCode): INotification {
if ($notification->getApp() !== 'files_sharing') {
// Not my app => throw
throw new \InvalidArgumentException();
Expand Down
10 changes: 1 addition & 9 deletions apps/twofactor_backupcodes/lib/AppInfo/Application.php
Expand Up @@ -74,15 +74,7 @@ public function registerNotification() {
$container = $this->getContainer();
/** @var IManager $manager */
$manager = $container->query(IManager::class);
$manager->registerNotifier(
function() use ($container) {
return $container->query(Notifier::class);
},
function () use ($container) {
$l = $container->query(IL10N::class);
return ['id' => 'twofactor_backupcodes', 'name' => $l->t('Second-factor backup codes')];
}
);
$manager->registerNotifierService(Notifier::class);
}

public function deleteUser($params) {
Expand Down
23 changes: 21 additions & 2 deletions apps/twofactor_backupcodes/lib/Notifications/Notifier.php
Expand Up @@ -42,7 +42,27 @@ public function __construct(IFactory $factory, IURLGenerator $url) {
$this->url = $url;
}

public function prepare(INotification $notification, $languageCode) {
/**
* Identifier of the notifier, only use [a-z0-9_]
*
* @return string
* @since 17.0.0
*/
public function getID(): string {
return 'twofactor_backupcodes';
}

/**
* Human readable name describing the notifier
*
* @return string
* @since 17.0.0
*/
public function getName(): string {
return $this->factory->get('twofactor_backupcodes')->t('Second-factor backup codes');
}

public function prepare(INotification $notification, string $languageCode): INotification {
if ($notification->getApp() !== 'twofactor_backupcodes') {
// Not my app => throw
throw new \InvalidArgumentException();
Expand Down Expand Up @@ -70,5 +90,4 @@ public function prepare(INotification $notification, $languageCode) {
throw new \InvalidArgumentException();
}
}

}
10 changes: 1 addition & 9 deletions apps/updatenotification/lib/AppInfo/Application.php
Expand Up @@ -71,14 +71,6 @@ public function register() {

public function registerNotifier() {
$notificationsManager = $this->getContainer()->getServer()->getNotificationManager();
$notificationsManager->registerNotifier(function() {
return $this->getContainer()->query(Notifier::class);
}, function() {
$l = $this->getContainer()->getServer()->getL10N('updatenotification');
return [
'id' => 'updatenotification',
'name' => $l->t('Update notifications'),
];
});
$notificationsManager->registerNotifierService(Notifier::class);
}
}
29 changes: 25 additions & 4 deletions apps/updatenotification/lib/Notification/Notifier.php
Expand Up @@ -31,6 +31,7 @@
use OCP\IUser;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Notification\AlreadyProcessedException;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
Expand Down Expand Up @@ -79,14 +80,35 @@ public function __construct(IURLGenerator $url, IConfig $config, IManager $notif
$this->appVersions = $this->getAppVersions();
}

/**
* Identifier of the notifier, only use [a-z0-9_]
*
* @return string
* @since 17.0.0
*/
public function getID(): string {
return 'updatenotification';
}

/**
* Human readable name describing the notifier
*
* @return string
* @since 17.0.0
*/
public function getName(): string {
return $this->l10NFactory->get('updatenotification')->t('Update notifications');
}

/**
* @param INotification $notification
* @param string $languageCode The code of the language that should be used to prepare the notification
* @return INotification
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
* @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
* @since 9.0.0
*/
public function prepare(INotification $notification, $languageCode): INotification {
public function prepare(INotification $notification, string $languageCode): INotification {
if ($notification->getApp() !== 'updatenotification') {
throw new \InvalidArgumentException('Unknown app id');
}
Expand Down Expand Up @@ -142,12 +164,11 @@ public function prepare(INotification $notification, $languageCode): INotificati
*
* @param INotification $notification
* @param string $installedVersion
* @throws \InvalidArgumentException When the update is already installed
* @throws AlreadyProcessedException When the update is already installed
*/
protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) {
if (version_compare($notification->getObjectId(), $installedVersion, '<=')) {
$this->notificationManager->markProcessed($notification);
throw new \InvalidArgumentException('Update already installed');
throw new AlreadyProcessedException();
}
}

Expand Down
12 changes: 2 additions & 10 deletions apps/updatenotification/tests/Notification/NotifierTest.php
Expand Up @@ -30,6 +30,7 @@
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Notification\AlreadyProcessedException;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use Test\TestCase;
Expand Down Expand Up @@ -112,21 +113,12 @@ public function testUpdateAlreadyInstalledCheck(string $versionNotification, str
->method('getObjectId')
->willReturn($versionNotification);

if ($exception) {
$this->notificationManager->expects($this->once())
->method('markProcessed')
->with($notification);
} else {
$this->notificationManager->expects($this->never())
->method('markProcessed');
}

try {
self::invokePrivate($notifier, 'updateAlreadyInstalledCheck', [$notification, $versionInstalled]);
$this->assertFalse($exception);
} catch (\Exception $e) {
$this->assertTrue($exception);
$this->assertInstanceOf('InvalidArgumentException', $e);
$this->assertInstanceOf(AlreadyProcessedException::class, $e);
}
}
}
12 changes: 1 addition & 11 deletions apps/user_ldap/appinfo/app.php
Expand Up @@ -42,17 +42,7 @@
$ldapWrapper = new OCA\User_LDAP\LDAP();
$ocConfig = \OC::$server->getConfig();
$notificationManager = \OC::$server->getNotificationManager();
$notificationManager->registerNotifier(function() {
return new \OCA\User_LDAP\Notification\Notifier(
\OC::$server->getL10NFactory()
);
}, function() {
$l = \OC::$server->getL10N('user_ldap');
return [
'id' => 'user_ldap',
'name' => $l->t('LDAP user and group backend'),
];
});
$notificationManager->registerNotifierService(\OCA\User_LDAP\Notification\Notifier::class);
$userSession = \OC::$server->getUserSession();

$userPluginManager = \OC::$server->query('LDAPUserPluginManager');
Expand Down
22 changes: 21 additions & 1 deletion apps/user_ldap/lib/Notification/Notifier.php
Expand Up @@ -42,13 +42,33 @@ public function __construct(\OCP\L10N\IFactory $l10nFactory) {
$this->l10nFactory = $l10nFactory;
}

/**
* Identifier of the notifier, only use [a-z0-9_]
*
* @return string
* @since 17.0.0
*/
public function getID(): string {
return 'user_ldap';
}

/**
* Human readable name describing the notifier
*
* @return string
* @since 17.0.0
*/
public function getName(): string {
return $this->l10nFactory->get('user_ldap')->t('LDAP User backend');
}

/**
* @param INotification $notification
* @param string $languageCode The code of the language that should be used to prepare the notification
* @return INotification
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
*/
public function prepare(INotification $notification, $languageCode) {
public function prepare(INotification $notification, string $languageCode): INotification {
if ($notification->getApp() !== 'user_ldap') {
// Not my app => throw
throw new \InvalidArgumentException();
Expand Down

0 comments on commit 5b604ea

Please sign in to comment.