From 8986910a7d6a0bd7ee08bca68f1f5008612e38dc Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 6 Sep 2019 13:06:21 +0200 Subject: Add notifications for users that are added to the group Signed-off-by: Joas Schilling --- apps/files_sharing/lib/AppInfo/Application.php | 6 +++ apps/files_sharing/lib/Notification/Listener.php | 48 ++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-) (limited to 'apps') diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index 92b3a92d438..5f48088d719 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -43,6 +43,7 @@ use OCP\AppFramework\Utility\IControllerMethodReflector; use OCP\Defaults; use OCP\Federation\ICloudIdManager; use \OCP\IContainer; +use OCP\IGroup; use OCP\IServerContainer; use OCA\Files_Sharing\Capabilities; use OCA\Files_Sharing\External\Manager; @@ -188,5 +189,10 @@ class Application extends App { $listener = $this->getContainer()->query(Listener::class); $listener->shareNotification($event); }); + $dispatcher->addListener(IGroup::class . '::postAddUser', function(GenericEvent $event) { + /** @var Listener $listener */ + $listener = $this->getContainer()->query(Listener::class); + $listener->userAddedToGroup($event); + }); } } diff --git a/apps/files_sharing/lib/Notification/Listener.php b/apps/files_sharing/lib/Notification/Listener.php index fd4daca28e7..1f8358dd3b8 100644 --- a/apps/files_sharing/lib/Notification/Listener.php +++ b/apps/files_sharing/lib/Notification/Listener.php @@ -25,24 +25,31 @@ declare(strict_types=1); namespace OCA\Files_Sharing\Notification; use OC\Share\Share; +use OCP\IGroup; use OCP\IGroupManager; -use OCP\Notification\IManager; +use OCP\IUser; +use OCP\Notification\IManager as INotificationManager; use OCP\Notification\INotification; +use OCP\Share\IManager as IShareManager; use OCP\Share\IShare; use Symfony\Component\EventDispatcher\GenericEvent; class Listener { - /** @var IManager */ + /** @var INotificationManager */ protected $notificationManager; + /** @var IShareManager */ + protected $shareManager; /** @var IGroupManager */ protected $groupManager; public function __construct( - IManager $notificationManager, + INotificationManager $notificationManager, + IShareManager $shareManager, IGroupManager $groupManager ) { $this->notificationManager = $notificationManager; + $this->shareManager = $shareManager; $this->groupManager = $groupManager; } @@ -74,6 +81,41 @@ class Listener { } } + /** + * @param GenericEvent $event + */ + public function userAddedToGroup(GenericEvent $event): void { + /** @var IGroup $group */ + $group = $event->getSubject(); + /** @var IUser $user */ + $user = $event->getArgument('user'); + + $offset = 0; + while (true) { + $shares = $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_GROUP, null, 50, $offset); + if (empty($shares)) { + break; + } + + foreach ($shares as $share) { + if ($share->getSharedWith() !== $group->getGID()) { + continue; + } + + if ($user->getUID() === $share->getShareOwner() || + $user->getUID() === $share->getSharedBy()) { + continue; + } + + $notification = $this->instantiateNotification($share); + $notification->setSubject('incoming_group_share') + ->setUser($user->getUID()); + $this->notificationManager->notify($notification); + } + $offset += 50; + } + } + /** * @param IShare $share * @return INotification -- cgit v1.2.3