2019-08-22 04:17:17 +03:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2019, Joas Schilling <coding@schilljs.com>
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files_Sharing\Notification;
|
|
|
|
|
|
|
|
use OC\Share\Share;
|
2019-08-26 15:40:02 +03:00
|
|
|
use OCP\IGroupManager;
|
2019-08-22 04:17:17 +03:00
|
|
|
use OCP\Notification\IManager;
|
|
|
|
use OCP\Notification\INotification;
|
|
|
|
use OCP\Share\IShare;
|
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
|
|
|
|
|
|
|
class Listener {
|
|
|
|
|
|
|
|
/** @var IManager */
|
|
|
|
protected $notificationManager;
|
2019-08-26 15:40:02 +03:00
|
|
|
/** @var IGroupManager */
|
|
|
|
protected $groupManager;
|
2019-08-22 04:17:17 +03:00
|
|
|
|
|
|
|
public function __construct(
|
2019-08-26 15:40:02 +03:00
|
|
|
IManager $notificationManager,
|
|
|
|
IGroupManager $groupManager
|
2019-08-22 04:17:17 +03:00
|
|
|
) {
|
|
|
|
$this->notificationManager = $notificationManager;
|
2019-08-26 15:40:02 +03:00
|
|
|
$this->groupManager = $groupManager;
|
2019-08-22 04:17:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param GenericEvent $event
|
|
|
|
*/
|
|
|
|
public function shareNotification(GenericEvent $event): void {
|
|
|
|
/** @var IShare $share */
|
|
|
|
$share = $event->getSubject();
|
|
|
|
$notification = $this->instantiateNotification($share);
|
|
|
|
|
|
|
|
if ($share->getShareType() === Share::SHARE_TYPE_USER) {
|
|
|
|
$notification->setSubject('incoming_user_share')
|
|
|
|
->setUser($share->getSharedWith());
|
|
|
|
$this->notificationManager->notify($notification);
|
2019-08-26 15:40:02 +03:00
|
|
|
} else if ($share->getShareType() === Share::SHARE_TYPE_GROUP) {
|
|
|
|
$notification->setSubject('incoming_group_share');
|
|
|
|
$group = $this->groupManager->get($share->getSharedWith());
|
|
|
|
|
|
|
|
foreach ($group->getUsers() as $user) {
|
2019-09-04 17:50:52 +03:00
|
|
|
if ($user->getUID() === $share->getShareOwner() ||
|
|
|
|
$user->getUID() === $share->getSharedBy()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-26 15:40:02 +03:00
|
|
|
$notification->setUser($user->getUID());
|
|
|
|
$this->notificationManager->notify($notification);
|
|
|
|
}
|
2019-08-22 04:17:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IShare $share
|
|
|
|
* @return INotification
|
|
|
|
*/
|
|
|
|
protected function instantiateNotification(IShare $share): INotification {
|
|
|
|
$notification = $this->notificationManager->createNotification();
|
|
|
|
$notification
|
|
|
|
->setApp('files_sharing')
|
|
|
|
->setObject('share', $share->getFullId())
|
|
|
|
->setDateTime($share->getShareTime());
|
|
|
|
|
|
|
|
return $notification;
|
|
|
|
}
|
|
|
|
}
|