diff options
author | Lukas Reschke <lukas@owncloud.com> | 2015-09-10 15:26:36 +0200 |
---|---|---|
committer | Lukas Reschke <lukas@owncloud.com> | 2015-09-10 15:26:36 +0200 |
commit | fb717f254fe81065912c13e0907ba374c8167f98 (patch) | |
tree | 1a34b7c8bed92bb5686d73a1521998b2057276cf /apps/files_sharing | |
parent | 89cd9295600f6707579240f3f010d4b75a2d85cd (diff) | |
parent | 38001d824b142ca9c47f2afafce91ab8b76f1a9a (diff) | |
download | nextcloud-server-fb717f254fe81065912c13e0907ba374c8167f98.tar.gz nextcloud-server-fb717f254fe81065912c13e0907ba374c8167f98.zip |
Merge pull request #18699 from owncloud/notification-manager
Notification manager
Diffstat (limited to 'apps/files_sharing')
-rw-r--r-- | apps/files_sharing/ajax/external.php | 1 | ||||
-rw-r--r-- | apps/files_sharing/api/remote.php | 4 | ||||
-rw-r--r-- | apps/files_sharing/api/server2server.php | 23 | ||||
-rw-r--r-- | apps/files_sharing/appinfo/app.php | 7 | ||||
-rw-r--r-- | apps/files_sharing/appinfo/application.php | 1 | ||||
-rw-r--r-- | apps/files_sharing/lib/external/manager.php | 24 | ||||
-rw-r--r-- | apps/files_sharing/lib/hooks.php | 1 | ||||
-rw-r--r-- | apps/files_sharing/lib/notifier.php | 86 | ||||
-rw-r--r-- | apps/files_sharing/tests/external/managertest.php | 1 | ||||
-rw-r--r-- | apps/files_sharing/tests/server2server.php | 1 |
10 files changed, 147 insertions, 2 deletions
diff --git a/apps/files_sharing/ajax/external.php b/apps/files_sharing/ajax/external.php index d26a64d3aec..66cfd8e9d1a 100644 --- a/apps/files_sharing/ajax/external.php +++ b/apps/files_sharing/ajax/external.php @@ -52,6 +52,7 @@ $externalManager = new \OCA\Files_Sharing\External\Manager( \OC\Files\Filesystem::getMountManager(), \OC\Files\Filesystem::getLoader(), \OC::$server->getHTTPHelper(), + \OC::$server->getNotificationManager(), \OC::$server->getUserSession()->getUser()->getUID() ); diff --git a/apps/files_sharing/api/remote.php b/apps/files_sharing/api/remote.php index f6cb0a29d8b..0f6d2dc265a 100644 --- a/apps/files_sharing/api/remote.php +++ b/apps/files_sharing/api/remote.php @@ -38,6 +38,7 @@ class Remote { Filesystem::getMountManager(), Filesystem::getLoader(), \OC::$server->getHTTPHelper(), + \OC::$server->getNotificationManager(), \OC_User::getUser() ); @@ -56,6 +57,7 @@ class Remote { Filesystem::getMountManager(), Filesystem::getLoader(), \OC::$server->getHTTPHelper(), + \OC::$server->getNotificationManager(), \OC_User::getUser() ); @@ -78,6 +80,7 @@ class Remote { Filesystem::getMountManager(), Filesystem::getLoader(), \OC::$server->getHTTPHelper(), + \OC::$server->getNotificationManager(), \OC_User::getUser() ); @@ -87,5 +90,4 @@ class Remote { return new \OC_OCS_Result(null, 404, "wrong share ID, share doesn't exist."); } - } diff --git a/apps/files_sharing/api/server2server.php b/apps/files_sharing/api/server2server.php index 4328e3830ba..6ecaea20535 100644 --- a/apps/files_sharing/api/server2server.php +++ b/apps/files_sharing/api/server2server.php @@ -70,6 +70,7 @@ class Server2Server { \OC\Files\Filesystem::getMountManager(), \OC\Files\Filesystem::getLoader(), \OC::$server->getHTTPHelper(), + \OC::$server->getNotificationManager(), $shareWith ); @@ -82,6 +83,28 @@ class Server2Server { Activity::FILES_SHARING_APP, Activity::SUBJECT_REMOTE_SHARE_RECEIVED, array($user, trim($name, '/')), '', array(), '', '', $shareWith, Activity::TYPE_REMOTE_SHARE, Activity::PRIORITY_LOW); + $urlGenerator = \OC::$server->getURLGenerator(); + + $notificationManager = \OC::$server->getNotificationManager(); + $notification = $notificationManager->createNotification(); + $notification->setApp('files_sharing') + ->setUser($shareWith) + ->setTimestamp(time()) + ->setObject('remote_share', $remoteId) + ->setSubject('remote_share', [$user, trim($name, '/')]); + + $acceptAction = $notification->createAction(); + $acceptAction->setLabel('accept') + ->setLink($urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/remote_shares/' . $remoteId), 'POST'); + $declineAction = $notification->createAction(); + $declineAction->setLabel('decline') + ->setLink($urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/remote_shares/' . $remoteId), 'DELETE'); + + $notification->addAction($acceptAction) + ->addAction($declineAction); + + $notificationManager->notify($notification); + return new \OC_OCS_Result(); } catch (\Exception $e) { \OCP\Util::writeLog('files_sharing', 'server can not add remote share, ' . $e->getMessage(), \OCP\Util::ERROR); diff --git a/apps/files_sharing/appinfo/app.php b/apps/files_sharing/appinfo/app.php index 295d013beff..20f1b046d35 100644 --- a/apps/files_sharing/appinfo/app.php +++ b/apps/files_sharing/appinfo/app.php @@ -103,3 +103,10 @@ if ($config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes') { } } } + +$manager = \OC::$server->getNotificationManager(); +$manager->registerNotifier(function() { + return new \OCA\Files_Sharing\Notifier( + \OC::$server->getL10NFactory() + ); +}); diff --git a/apps/files_sharing/appinfo/application.php b/apps/files_sharing/appinfo/application.php index 530195c65fa..d0dcadb77e8 100644 --- a/apps/files_sharing/appinfo/application.php +++ b/apps/files_sharing/appinfo/application.php @@ -93,6 +93,7 @@ class Application extends App { \OC\Files\Filesystem::getMountManager(), \OC\Files\Filesystem::getLoader(), $server->getHTTPHelper(), + $server->getNotificationManager(), $uid ); }); diff --git a/apps/files_sharing/lib/external/manager.php b/apps/files_sharing/lib/external/manager.php index 67a26c096c2..17142e95099 100644 --- a/apps/files_sharing/lib/external/manager.php +++ b/apps/files_sharing/lib/external/manager.php @@ -28,6 +28,7 @@ namespace OCA\Files_Sharing\External; use OC\Files\Filesystem; use OCP\Files; +use OC\Notification\IManager; class Manager { const STORAGE = '\OCA\Files_Sharing\External\Storage'; @@ -58,19 +59,26 @@ class Manager { private $httpHelper; /** + * @var IManager + */ + private $notificationManager; + + /** * @param \OCP\IDBConnection $connection * @param \OC\Files\Mount\Manager $mountManager * @param \OCP\Files\Storage\IStorageFactory $storageLoader * @param \OC\HTTPHelper $httpHelper + * @param IManager $notificationManager * @param string $uid */ public function __construct(\OCP\IDBConnection $connection, \OC\Files\Mount\Manager $mountManager, - \OCP\Files\Storage\IStorageFactory $storageLoader, \OC\HTTPHelper $httpHelper, $uid) { + \OCP\Files\Storage\IStorageFactory $storageLoader, \OC\HTTPHelper $httpHelper, IManager $notificationManager, $uid) { $this->connection = $connection; $this->mountManager = $mountManager; $this->storageLoader = $storageLoader; $this->httpHelper = $httpHelper; $this->uid = $uid; + $this->notificationManager = $notificationManager; } /** @@ -206,6 +214,7 @@ class Manager { $acceptShare->execute(array(1, $mountPoint, $hash, $id, $this->uid)); $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'accept'); + $this->scrapNotification($share['remote_id']); return true; } @@ -228,6 +237,7 @@ class Manager { $removeShare->execute(array($id, $this->uid)); $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline'); + $this->scrapNotification($share['remote_id']); return true; } @@ -235,6 +245,17 @@ class Manager { } /** + * @param int $remoteShare + */ + protected function scrapNotification($remoteShare) { + $filter = $this->notificationManager->createNotification(); + $filter->setApp('files_sharing') + ->setUser($this->uid) + ->setObject('remote_share', (int) $remoteShare); + $this->notificationManager->markProcessed($filter); + } + + /** * inform remote server whether server-to-server share was accepted/declined * * @param string $remote @@ -265,6 +286,7 @@ class Manager { \OC\Files\Filesystem::getMountManager(), \OC\Files\Filesystem::getLoader(), \OC::$server->getHTTPHelper(), + \OC::$server->getNotificationManager(), $params['user'] ); diff --git a/apps/files_sharing/lib/hooks.php b/apps/files_sharing/lib/hooks.php index 7dd04f2f4a0..1937010f390 100644 --- a/apps/files_sharing/lib/hooks.php +++ b/apps/files_sharing/lib/hooks.php @@ -33,6 +33,7 @@ class Hooks { \OC\Files\Filesystem::getMountManager(), \OC\Files\Filesystem::getLoader(), \OC::$server->getHTTPHelper(), + \OC::$server->getNotificationManager(), $params['uid']); $manager->removeUserShares($params['uid']); diff --git a/apps/files_sharing/lib/notifier.php b/apps/files_sharing/lib/notifier.php new file mode 100644 index 00000000000..cc2deb3f439 --- /dev/null +++ b/apps/files_sharing/lib/notifier.php @@ -0,0 +1,86 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\Files_Sharing; + + +use OC\Notification\INotification; +use OC\Notification\INotifier; + +class Notifier implements INotifier { + /** @var \OCP\L10N\IFactory */ + protected $factory; + + /** + * @param \OCP\L10N\IFactory $factory + */ + public function __construct(\OCP\L10N\IFactory $factory) { + $this->factory = $factory; + } + + /** + * @param INotification $notification + * @param string $languageCode The code of the language that should be used to prepare the notification + * @return INotification + */ + public function prepare(INotification $notification, $languageCode) { + if ($notification->getApp() !== 'files_sharing') { + // Not my app => throw + throw new \InvalidArgumentException(); + } + + // Read the language from the notification + $l = $this->factory->get('files_sharing', $languageCode); + + switch ($notification->getSubject()) { + // Deal with known subjects + case 'remote_share': + $params = $notification->getSubjectParameters(); + $notification->setParsedSubject( + (string) $l->t('You received %s as a remote share from %s', $params) + ); + + // Deal with the actions for a known subject + foreach ($notification->getActions() as $action) { + switch ($action->getLabel()) { + case 'accept': + $action->setParsedLabel( + (string) $l->t('Accept') + ); + break; + + case 'decline': + $action->setParsedLabel( + (string) $l->t('Decline') + ); + break; + } + + $notification->addParsedAction($action); + } + return $notification; + + default: + // Unknown subject => Unknown notification => throw + throw new \InvalidArgumentException(); + } + } +} diff --git a/apps/files_sharing/tests/external/managertest.php b/apps/files_sharing/tests/external/managertest.php index df01ea0f738..8e03c67a9a3 100644 --- a/apps/files_sharing/tests/external/managertest.php +++ b/apps/files_sharing/tests/external/managertest.php @@ -50,6 +50,7 @@ class ManagerTest extends TestCase { $this->mountManager, new StorageFactory(), $httpHelper, + \OC::$server->getNotificationManager(), $this->uid ); } diff --git a/apps/files_sharing/tests/server2server.php b/apps/files_sharing/tests/server2server.php index a1c87d73393..a4cc8209a8e 100644 --- a/apps/files_sharing/tests/server2server.php +++ b/apps/files_sharing/tests/server2server.php @@ -154,6 +154,7 @@ class Test_Files_Sharing_S2S_OCS_API extends TestCase { \OC\Files\Filesystem::getMountManager(), \OC\Files\Filesystem::getLoader(), \OC::$server->getHTTPHelper(), + \OC::$server->getNotificationManager(), $toDelete ); |