Skip to content

Commit

Permalink
Clear user notifications from table after they are deleted
Browse files Browse the repository at this point in the history
Clear user notifications from table after they are deleted.

Signed-off-by: Sujith H <sharidasan@owncloud.com>
  • Loading branch information
sharidas committed Aug 7, 2018
1 parent 6d3fa7f commit 644c4dc
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ public function getCount(INotification $notification) {
public function markProcessed(INotification $notification) {
$this->handler->delete($notification);
}

/**
* @param string $uid uid of the user
* @since 10.0.10
*/
public function removeUserNotifications($uid) {
$this->handler->deleteUserNotifications($uid);
}
}
9 changes: 9 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@
namespace OCA\Notifications\AppInfo;

use OCA\Notifications\Capabilities;
use OCA\Notifications\Configuration\OptionsStorage;
use OCA\Notifications\Controller\EndpointController;
use OCA\Notifications\Handler;
use OCA\Notifications\App as NotificationApp;
use OCA\Notifications\Mailer\NotificationMailer;
use OCA\Notifications\Mailer\NotificationMailerAdapter;
use OCA\Notifications\Notifier;
use OCP\AppFramework\App;
use OCP\IContainer;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Notification\Events\RegisterConsumerEvent;
use OCP\Notification\Events\RegisterNotifierEvent;
use Symfony\Component\EventDispatcher\GenericEvent;

class Application extends App {
public function __construct (array $urlParams = array()) {
Expand Down Expand Up @@ -60,6 +64,11 @@ public function __construct (array $urlParams = array()) {
});
$container->registerCapability('Capabilities');

$container->getServer()->getEventDispatcher()->addListener('user.afterdelete', function (GenericEvent $event) use ($container) {
$app = $container->query('OCA\Notifications\App');

$app->removeUserNotifications($event->getArgument('uid'));
});
}

public function setupConsumerAndNotifier() {
Expand Down
13 changes: 13 additions & 0 deletions lib/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ public function delete(INotification $notification) {
$sql->execute();
}

/**
* This method deletes the notifications of the user after the user is deleted
* from the notifications table.
*
* @param string $uid uid of the user
*/
public function deleteUserNotifications($uid) {
$sql = $this->connection->getQueryBuilder();
$sql->delete('notifications')
->where($sql->expr()->eq('user', $sql->createNamedParameter($uid)));
$sql->execute();
}

/**
* Delete the notification matching the given id
*
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/AppInfo/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

namespace OCA\Notifications\Tests\Unit\AppInfo;

use OCA\Notifications\App;
use OCA\Notifications\AppInfo\Application;
use OCA\Notifications\Tests\Unit\TestCase;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class ApplicationTest
Expand Down Expand Up @@ -64,4 +66,9 @@ public function dataContainerQuery() {
public function testContainerQuery($service, $expected) {
$this->assertTrue($this->container->query($service) instanceof $expected);
}

public function testUserDeleteEvent() {
\OC::$server->getEventDispatcher()->dispatch('user.afterdelete', new GenericEvent(null, ['uid' => 'foo']));
$this->assertInstanceOf(App::class, $this->container->query('OCA\Notifications\App'));
}
}
8 changes: 8 additions & 0 deletions tests/Unit/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,12 @@ public function testMarkProcessed() {

$this->app->markProcessed($this->notification);
}

public function testRemoveUserNotifications() {
$this->handler->expects($this->once())
->method('deleteUserNotifications')
->with('foo');

$this->app->removeUserNotifications('foo');
}
}
67 changes: 67 additions & 0 deletions tests/Unit/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OCA\Notifications\Tests\Unit;


use OC\Notification\Notification;
use OCA\Notifications\Handler;

/**
Expand Down Expand Up @@ -110,6 +111,72 @@ public function testFull() {
$this->assertSame(0, $this->handler->count($limitedNotification2), 'Wrong notification count for user2 after deleting');
}

public function testDeleteUserNotifications() {
$notification1 = $this->getNotification([
'getApp' => 'testing_notifications',
'getUser' => 'test_user1',
'getDateTime' => new \DateTime(),
'getObjectType' => 'notification',
'getObjectId' => '1337',
'getSubject' => 'subject',
'getSubjectParameters' => [],
'getMessage' => 'message',
'getMessageParameters' => [],
'getLink' => 'link',
'getActions' => [
[
'getLabel' => 'action_label',
'getLink' => 'action_link',
'getRequestType' => 'GET',
'isPrimary' => true,
]
],
]);
$notification2 = $this->getNotification([
'getApp' => 'testing_notifications',
'getUser' => 'test_user2',
'getDateTime' => new \DateTime(),
'getObjectType' => 'notification',
'getObjectId' => '1337',
'getSubject' => 'subject',
'getSubjectParameters' => [],
'getMessage' => 'message',
'getMessageParameters' => [],
'getLink' => 'link',
'getActions' => [
[
'getLabel' => 'action_label',
'getLink' => 'action_link',
'getRequestType' => 'GET',
'isPrimary' => true,
]
],
]);
$limitedNotification = $this->getNotification([
'getApp' => 'testing_notifications',
'getUser' => 'test_user1',
]);
$limitedNotification2 = $this->getNotification([
'getApp' => 'testing_notifications',
'getUser' => 'test_user2',
]);

$this->handler->add($notification1);
$this->handler->add($notification2);
$notifications = $this->handler->get($limitedNotification);
$notificationId = key($notifications);

$this->handler->deleteUserNotifications($notification1->getUser());

$this->assertNull($this->handler->getById($notificationId, 'test_user1'));

$notifications2 = $this->handler->get($limitedNotification2);
$notificationId2 = key($notifications2);
$result = $this->handler->getById($notificationId2, 'test_user2');
$this->assertInstanceOf(Notification::class, $result);
$this->assertEquals('test_user2', $result->getUser());
}

public function testDeleteById() {
$notification = $this->getNotification([
'getApp' => 'testing_notifications',
Expand Down

0 comments on commit 644c4dc

Please sign in to comment.