You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Listener.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files_Sharing\Notification;
  26. use OCP\IGroup;
  27. use OCP\IGroupManager;
  28. use OCP\IUser;
  29. use OCP\Notification\IManager as INotificationManager;
  30. use OCP\Notification\INotification;
  31. use OCP\Share\IManager as IShareManager;
  32. use OCP\Share\IShare;
  33. use Symfony\Component\EventDispatcher\GenericEvent;
  34. class Listener {
  35. /** @var INotificationManager */
  36. protected $notificationManager;
  37. /** @var IShareManager */
  38. protected $shareManager;
  39. /** @var IGroupManager */
  40. protected $groupManager;
  41. public function __construct(
  42. INotificationManager $notificationManager,
  43. IShareManager $shareManager,
  44. IGroupManager $groupManager
  45. ) {
  46. $this->notificationManager = $notificationManager;
  47. $this->shareManager = $shareManager;
  48. $this->groupManager = $groupManager;
  49. }
  50. /**
  51. * @param GenericEvent $event
  52. */
  53. public function shareNotification(GenericEvent $event): void {
  54. /** @var IShare $share */
  55. $share = $event->getSubject();
  56. $notification = $this->instantiateNotification($share);
  57. if ($share->getShareType() === IShare::TYPE_USER) {
  58. $notification->setSubject(Notifier::INCOMING_USER_SHARE)
  59. ->setUser($share->getSharedWith());
  60. $this->notificationManager->notify($notification);
  61. } elseif ($share->getShareType() === IShare::TYPE_GROUP) {
  62. $notification->setSubject(Notifier::INCOMING_GROUP_SHARE);
  63. $group = $this->groupManager->get($share->getSharedWith());
  64. foreach ($group->getUsers() as $user) {
  65. if ($user->getUID() === $share->getShareOwner() ||
  66. $user->getUID() === $share->getSharedBy()) {
  67. continue;
  68. }
  69. $notification->setUser($user->getUID());
  70. $this->notificationManager->notify($notification);
  71. }
  72. }
  73. }
  74. /**
  75. * @param GenericEvent $event
  76. */
  77. public function userAddedToGroup(GenericEvent $event): void {
  78. /** @var IGroup $group */
  79. $group = $event->getSubject();
  80. /** @var IUser $user */
  81. $user = $event->getArgument('user');
  82. $offset = 0;
  83. while (true) {
  84. $shares = $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_GROUP, null, 50, $offset);
  85. if (empty($shares)) {
  86. break;
  87. }
  88. foreach ($shares as $share) {
  89. if ($share->getSharedWith() !== $group->getGID()) {
  90. continue;
  91. }
  92. if ($user->getUID() === $share->getShareOwner() ||
  93. $user->getUID() === $share->getSharedBy()) {
  94. continue;
  95. }
  96. $notification = $this->instantiateNotification($share);
  97. $notification->setSubject(Notifier::INCOMING_GROUP_SHARE)
  98. ->setUser($user->getUID());
  99. $this->notificationManager->notify($notification);
  100. }
  101. $offset += 50;
  102. }
  103. }
  104. /**
  105. * @param IShare $share
  106. * @return INotification
  107. */
  108. protected function instantiateNotification(IShare $share): INotification {
  109. $notification = $this->notificationManager->createNotification();
  110. $notification
  111. ->setApp('files_sharing')
  112. ->setObject('share', $share->getFullId())
  113. ->setDateTime($share->getShareTime());
  114. return $notification;
  115. }
  116. }