diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index 92b3a92d43..5f48088d71 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 fd4daca28e..1f8358dd3b 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