From d191a0daccfa923dcef4ffa9c020fe01f3caf129 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 1 Sep 2015 17:59:01 +0200 Subject: [PATCH] Add notifications for remote shares --- apps/files_sharing/api/server2server.php | 22 ++++++ apps/files_sharing/appinfo/app.php | 7 ++ apps/files_sharing/lib/notifier.php | 86 ++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 apps/files_sharing/lib/notifier.php diff --git a/apps/files_sharing/api/server2server.php b/apps/files_sharing/api/server2server.php index 4328e3830b..d5f1dce505 100644 --- a/apps/files_sharing/api/server2server.php +++ b/apps/files_sharing/api/server2server.php @@ -82,6 +82,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 295d013bef..20f1b046d3 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/lib/notifier.php b/apps/files_sharing/lib/notifier.php new file mode 100644 index 0000000000..e050d4d3c1 --- /dev/null +++ b/apps/files_sharing/lib/notifier.php @@ -0,0 +1,86 @@ + + * + * @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 + * + */ + +namespace OCA\Files_Sharing; + + +use OCP\Notification\INotification; +use OCP\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(); + } + } +}