nextcloud/apps/files_sharing/lib/Notification/Listener.php

86 lines
2.5 KiB
PHP
Raw Normal View History

<?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;
use OCP\IGroupManager;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use OCP\Share\IShare;
use Symfony\Component\EventDispatcher\GenericEvent;
class Listener {
/** @var IManager */
protected $notificationManager;
/** @var IGroupManager */
protected $groupManager;
public function __construct(
IManager $notificationManager,
IGroupManager $groupManager
) {
$this->notificationManager = $notificationManager;
$this->groupManager = $groupManager;
}
/**
* @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);
} else if ($share->getShareType() === Share::SHARE_TYPE_GROUP) {
$notification->setSubject('incoming_group_share');
$group = $this->groupManager->get($share->getSharedWith());
foreach ($group->getUsers() as $user) {
$notification->setUser($user->getUID());
$this->notificationManager->notify($notification);
}
}
}
/**
* @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;
}
}