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

Set the link of the notification on render instead of creation #2982

Merged
merged 5 commits into from
Jan 10, 2017
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
15 changes: 2 additions & 13 deletions apps/comments/lib/Notification/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

use OCP\Comments\CommentsEvent;
use OCP\Comments\IComment;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Notification\IManager;

Expand All @@ -34,25 +33,19 @@ class Listener {
/** @var IUserManager */
protected $userManager;

/** @var IURLGenerator */
protected $urlGenerator;

/**
* Listener constructor.
*
* @param IManager $notificationManager
* @param IUserManager $userManager
* @param IURLGenerator $urlGenerator
*/
public function __construct(
IManager $notificationManager,
IUserManager $userManager,
IURLGenerator $urlGenerator
IUserManager $userManager
) {

$this->notificationManager = $notificationManager;
$this->userManager = $userManager;
$this->urlGenerator = $urlGenerator;
}

/**
Expand Down Expand Up @@ -100,11 +93,7 @@ public function instantiateNotification(IComment $comment) {
->setApp('comments')
->setObject('comment', $comment->getId())
->setSubject('mention', [ $comment->getObjectType(), $comment->getObjectId() ])
->setDateTime($comment->getCreationDateTime())
->setLink($this->urlGenerator->linkToRouteAbsolute(
'comments.Notifications.view',
['id' => $comment->getId()])
);
->setDateTime($comment->getCreationDateTime());

return $notification;
}
Expand Down
6 changes: 5 additions & 1 deletion apps/comments/lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ public function prepare(INotification $notification, $languageCode) {
]
);
}
$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg')));
$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg')))
->setLink($this->url->linkToRouteAbsolute(
'comments.Notifications.view',
['id' => $comment->getId()])
);

return $notification;
break;
Expand Down
8 changes: 3 additions & 5 deletions apps/comments/tests/Unit/Notification/ListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ class ListenerTest extends TestCase {
protected function setUp() {
parent::setUp();

$this->notificationManager = $this->getMockBuilder('\OCP\Notification\IManager')->getMock();
$this->userManager = $this->getMockBuilder('\OCP\IUserManager')->getMock();
$this->urlGenerator = $this->getMockBuilder('OCP\IURLGenerator')->getMock();
$this->notificationManager = $this->createMock(\OCP\Notification\IManager::class);
$this->userManager = $this->createMock(\OCP\IUserManager::class);

$this->listener = new Listener(
$this->notificationManager,
$this->userManager,
$this->urlGenerator
$this->userManager
);
}

Expand Down
23 changes: 6 additions & 17 deletions apps/updatenotification/lib/Notification/BackgroundJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Notification\IManager;

class BackgroundJob extends TimedJob {
Expand All @@ -51,10 +49,7 @@ class BackgroundJob extends TimedJob {
/** @var IClientService */
protected $client;

/** @var IURLGenerator */
protected $urlGenerator;

/** @var IUser[] */
/** @var string[] */
protected $users;

/**
Expand All @@ -65,9 +60,8 @@ class BackgroundJob extends TimedJob {
* @param IGroupManager $groupManager
* @param IAppManager $appManager
* @param IClientService $client
* @param IURLGenerator $urlGenerator
*/
public function __construct(IConfig $config, IManager $notificationManager, IGroupManager $groupManager, IAppManager $appManager, IClientService $client, IURLGenerator $urlGenerator) {
public function __construct(IConfig $config, IManager $notificationManager, IGroupManager $groupManager, IAppManager $appManager, IClientService $client) {
// Run once a day
$this->setInterval(60 * 60 * 24);

Expand All @@ -76,7 +70,6 @@ public function __construct(IConfig $config, IManager $notificationManager, IGro
$this->groupManager = $groupManager;
$this->appManager = $appManager;
$this->client = $client;
$this->urlGenerator = $urlGenerator;
}

protected function run($argument) {
Expand All @@ -97,8 +90,7 @@ protected function checkCoreUpdate() {

$status = $updater->check();
if (isset($status['version'])) {
$url = $this->urlGenerator->linkToRouteAbsolute('settings.AdminSettings.index') . '#updater';
$this->createNotifications('core', $status['version'], $url, $status['versionstring']);
$this->createNotifications('core', $status['version'], $status['versionstring']);
}
}

Expand All @@ -110,8 +102,7 @@ protected function checkAppUpdates() {
foreach ($apps as $app) {
$update = $this->isUpdateAvailable($app);
if ($update !== false) {
$url = $this->urlGenerator->linkToRouteAbsolute('settings.AppSettings.viewApps') . '#app-' . $app;
$this->createNotifications($app, $update, $url);
$this->createNotifications($app, $update);
}
}
}
Expand All @@ -121,10 +112,9 @@ protected function checkAppUpdates() {
*
* @param string $app
* @param string $version
* @param string $url
* @param string $visibleVersion
*/
protected function createNotifications($app, $version, $url, $visibleVersion = '') {
protected function createNotifications($app, $version, $visibleVersion = '') {
$lastNotification = $this->config->getAppValue('updatenotification', $app, false);
if ($lastNotification === $version) {
// We already notified about this update
Expand All @@ -138,8 +128,7 @@ protected function createNotifications($app, $version, $url, $visibleVersion = '
$notification = $this->notificationManager->createNotification();
$notification->setApp('updatenotification')
->setDateTime(new \DateTime())
->setObject($app, $version)
->setLink($url);
->setObject($app, $version);

if ($visibleVersion !== '') {
$notification->setSubject('update_available', ['version' => $visibleVersion]);
Expand Down
36 changes: 35 additions & 1 deletion apps/updatenotification/lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
namespace OCA\UpdateNotification\Notification;


use OCP\IGroupManager;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
Expand All @@ -41,6 +44,12 @@ class Notifier implements INotifier {
/** @var IFactory */
protected $l10NFactory;

/** @var IUserSession */
protected $userSession;

/** @var IGroupManager */
protected $groupManager;

/** @var string[] */
protected $appVersions;

Expand All @@ -50,11 +59,15 @@ class Notifier implements INotifier {
* @param IURLGenerator $url
* @param IManager $notificationManager
* @param IFactory $l10NFactory
* @param IUserSession $userSession
* @param IGroupManager $groupManager
*/
public function __construct(IURLGenerator $url, IManager $notificationManager, IFactory $l10NFactory) {
public function __construct(IURLGenerator $url, IManager $notificationManager, IFactory $l10NFactory, IUserSession $userSession, IGroupManager $groupManager) {
$this->url = $url;
$this->notificationManager = $notificationManager;
$this->l10NFactory = $l10NFactory;
$this->userSession = $userSession;
$this->groupManager = $groupManager;
$this->appVersions = $this->getAppVersions();
}

Expand All @@ -76,6 +89,10 @@ public function prepare(INotification $notification, $languageCode) {

$parameters = $notification->getSubjectParameters();
$notification->setParsedSubject($l->t('Update to %1$s is available.', [$parameters['version']]));

if ($this->isAdmin()) {
$notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index') . '#updater');
}
} else {
$appInfo = $this->getAppInfo($notification->getObjectType());
$appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name'];
Expand All @@ -92,6 +109,10 @@ public function prepare(INotification $notification, $languageCode) {
'name' => $appName,
]
]);

if ($this->isAdmin()) {
$notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps') . '#app-' . $notification->getObjectType());
}
}

$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg')));
Expand All @@ -113,6 +134,19 @@ protected function updateAlreadyInstalledCheck(INotification $notification, $ins
}
}

/**
* @return bool
*/
protected function isAdmin() {
$user = $this->userSession->getUser();

if ($user instanceof IUser) {
return $this->groupManager->isAdmin($user->getUID());
}

return false;
}

protected function getCoreVersions() {
return implode('.', \OCP\Util::getVersion());
}
Expand Down
49 changes: 13 additions & 36 deletions apps/updatenotification/tests/Notification/BackgroundJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,15 @@ class BackgroundJobTest extends TestCase {
protected $appManager;
/** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */
protected $client;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
protected $urlGenerator;

public function setUp() {
parent::setUp();

$this->config = $this->getMockBuilder('OCP\IConfig')->getMock();
$this->notificationManager = $this->getMockBuilder('OCP\Notification\IManager')->getMock();
$this->groupManager = $this->getMockBuilder('OCP\IGroupManager')->getMock();
$this->appManager = $this->getMockBuilder('OCP\App\IAppManager')->getMock();
$this->client = $this->getMockBuilder('OCP\Http\Client\IClientService')->getMock();
$this->urlGenerator = $this->getMockBuilder('OCP\IURLGenerator')->getMock();
$this->config = $this->createMock(\OCP\IConfig::class);
$this->notificationManager = $this->createMock(\OCP\Notification\IManager::class);
$this->groupManager = $this->createMock(\OCP\IGroupManager::class);
$this->appManager = $this->createMock(\OCP\App\IAppManager::class);
$this->client = $this->createMock(\OCP\Http\Client\IClientService::class);
}

/**
Expand All @@ -70,8 +67,7 @@ protected function getJob(array $methods = []) {
$this->notificationManager,
$this->groupManager,
$this->appManager,
$this->client,
$this->urlGenerator
$this->client
);
} {
return $this->getMockBuilder('OCA\UpdateNotification\Notification\BackgroundJob')
Expand All @@ -81,7 +77,6 @@ protected function getJob(array $methods = []) {
$this->groupManager,
$this->appManager,
$this->client,
$this->urlGenerator,
])
->setMethods($methods)
->getMock();
Expand Down Expand Up @@ -160,20 +155,12 @@ public function testCheckCoreUpdate($channel, $versionCheck, $notification, $rea
}

if ($notification === null) {
$this->urlGenerator->expects($this->never())
->method('linkToRouteAbsolute');

$job->expects($this->never())
->method('createNotifications');
} else {
$this->urlGenerator->expects($this->once())
->method('linkToRouteAbsolute')
->with('settings.AdminSettings.index')
->willReturn('admin-url');

$job->expects($this->once())
->method('createNotifications')
->willReturn('core', $notification, 'admin-url#updater', $readableVersion);
->willReturn('core', $notification, $readableVersion);
}

$this->invokePrivate($job, 'checkCoreUpdate');
Expand All @@ -188,7 +175,7 @@ public function dataCheckAppUpdates() {
['app2', '1.9.2'],
],
[
['app2', '1.9.2', 'apps-url#app-app2'],
['app2', '1.9.2'],
],
],
];
Expand All @@ -215,11 +202,6 @@ public function testCheckAppUpdates(array $apps, array $isUpdateAvailable, array
->method('isUpdateAvailable')
->willReturnMap($isUpdateAvailable);

$this->urlGenerator->expects($this->exactly(sizeof($notifications)))
->method('linkToRouteAbsolute')
->with('settings.AppSettings.viewApps')
->willReturn('apps-url');

$mockedMethod = $job->expects($this->exactly(sizeof($notifications)))
->method('createNotifications');
call_user_func_array([$mockedMethod, 'withConsecutive'], $notifications);
Expand All @@ -229,9 +211,9 @@ public function testCheckAppUpdates(array $apps, array $isUpdateAvailable, array

public function dataCreateNotifications() {
return [
['app1', '1.0.0', 'link1', '1.0.0', false, false, null, null],
['app2', '1.0.1', 'link2', '1.0.0', '1.0.0', true, ['user1'], [['user1']]],
['app3', '1.0.1', 'link3', false, false, true, ['user2', 'user3'], [['user2'], ['user3']]],
['app1', '1.0.0', '1.0.0', false, false, null, null],
['app2', '1.0.1', '1.0.0', '1.0.0', true, ['user1'], [['user1']]],
['app3', '1.0.1', false, false, true, ['user2', 'user3'], [['user2'], ['user3']]],
];
}

Expand All @@ -240,14 +222,13 @@ public function dataCreateNotifications() {
*
* @param string $app
* @param string $version
* @param string $url
* @param string|false $lastNotification
* @param string|false $callDelete
* @param bool $createNotification
* @param string[]|null $users
* @param array|null $userNotifications
*/
public function testCreateNotifications($app, $version, $url, $lastNotification, $callDelete, $createNotification, $users, $userNotifications) {
public function testCreateNotifications($app, $version, $lastNotification, $callDelete, $createNotification, $users, $userNotifications) {
$job = $this->getJob([
'deleteOutdatedNotifications',
'getUsersToNotify',
Expand Down Expand Up @@ -299,10 +280,6 @@ public function testCreateNotifications($app, $version, $url, $lastNotification,
->method('setSubject')
->with('update_available')
->willReturnSelf();
$notification->expects($this->once())
->method('setLink')
->with($url)
->willReturnSelf();

if ($userNotifications !== null) {
$mockedMethod = $notification->expects($this->exactly(sizeof($userNotifications)))
Expand All @@ -323,7 +300,7 @@ public function testCreateNotifications($app, $version, $url, $lastNotification,
->method('createNotification');
}

$this->invokePrivate($job, 'createNotifications', [$app, $version, $url]);
$this->invokePrivate($job, 'createNotifications', [$app, $version]);
}

public function dataGetUsersToNotify() {
Expand Down
Loading