Groups.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_Sharing\Activity\Providers;
  7. use OCP\Activity\IEvent;
  8. use OCP\Activity\IEventMerger;
  9. use OCP\Activity\IManager;
  10. use OCP\Contacts\IManager as IContactsManager;
  11. use OCP\Federation\ICloudIdManager;
  12. use OCP\IGroup;
  13. use OCP\IGroupManager;
  14. use OCP\IURLGenerator;
  15. use OCP\IUserManager;
  16. use OCP\L10N\IFactory;
  17. class Groups extends Base {
  18. public const SUBJECT_SHARED_GROUP_SELF = 'shared_group_self';
  19. public const SUBJECT_RESHARED_GROUP_BY = 'reshared_group_by';
  20. public const SUBJECT_UNSHARED_GROUP_SELF = 'unshared_group_self';
  21. public const SUBJECT_UNSHARED_GROUP_BY = 'unshared_group_by';
  22. public const SUBJECT_EXPIRED_GROUP = 'expired_group';
  23. /** @var IGroupManager */
  24. protected $groupManager;
  25. /** @var string[] */
  26. protected $groupDisplayNames = [];
  27. public function __construct(IFactory $languageFactory,
  28. IURLGenerator $url,
  29. IManager $activityManager,
  30. IUserManager $userManager,
  31. ICloudIdManager $cloudIdManager,
  32. IContactsManager $contactsManager,
  33. IEventMerger $eventMerger,
  34. IGroupManager $groupManager) {
  35. parent::__construct($languageFactory, $url, $activityManager, $userManager, $cloudIdManager, $contactsManager, $eventMerger);
  36. $this->groupManager = $groupManager;
  37. }
  38. /**
  39. * @param IEvent $event
  40. * @return IEvent
  41. * @throws \InvalidArgumentException
  42. * @since 11.0.0
  43. */
  44. public function parseShortVersion(IEvent $event) {
  45. $parsedParameters = $this->getParsedParameters($event);
  46. if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) {
  47. $subject = $this->l->t('Shared with group {group}');
  48. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) {
  49. $subject = $this->l->t('Removed share for group {group}');
  50. } elseif ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) {
  51. $subject = $this->l->t('{actor} shared with group {group}');
  52. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) {
  53. $subject = $this->l->t('{actor} removed share for group {group}');
  54. } elseif ($event->getSubject() === self::SUBJECT_EXPIRED_GROUP) {
  55. $subject = $this->l->t('Share for group {group} expired');
  56. } else {
  57. throw new \InvalidArgumentException();
  58. }
  59. if ($this->activityManager->getRequirePNG()) {
  60. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  61. } else {
  62. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  63. }
  64. $this->setSubjects($event, $subject, $parsedParameters);
  65. return $event;
  66. }
  67. /**
  68. * @param IEvent $event
  69. * @param IEvent|null $previousEvent
  70. * @return IEvent
  71. * @throws \InvalidArgumentException
  72. * @since 11.0.0
  73. */
  74. public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null) {
  75. $parsedParameters = $this->getParsedParameters($event);
  76. if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) {
  77. $subject = $this->l->t('You shared {file} with group {group}');
  78. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) {
  79. $subject = $this->l->t('You removed group {group} from {file}');
  80. } elseif ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) {
  81. $subject = $this->l->t('{actor} shared {file} with group {group}');
  82. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) {
  83. $subject = $this->l->t('{actor} removed group {group} from {file}');
  84. } elseif ($event->getSubject() === self::SUBJECT_EXPIRED_GROUP) {
  85. $subject = $this->l->t('Share for file {file} with group {group} expired');
  86. } else {
  87. throw new \InvalidArgumentException();
  88. }
  89. if ($this->activityManager->getRequirePNG()) {
  90. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  91. } else {
  92. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  93. }
  94. $this->setSubjects($event, $subject, $parsedParameters);
  95. return $event;
  96. }
  97. protected function getParsedParameters(IEvent $event) {
  98. $subject = $event->getSubject();
  99. $parameters = $event->getSubjectParameters();
  100. switch ($subject) {
  101. case self::SUBJECT_RESHARED_GROUP_BY:
  102. case self::SUBJECT_UNSHARED_GROUP_BY:
  103. return [
  104. 'file' => $this->getFile($parameters[0], $event),
  105. 'group' => $this->generateGroupParameter($parameters[2]),
  106. 'actor' => $this->getUser($parameters[1]),
  107. ];
  108. case self::SUBJECT_SHARED_GROUP_SELF:
  109. case self::SUBJECT_UNSHARED_GROUP_SELF:
  110. case self::SUBJECT_EXPIRED_GROUP:
  111. return [
  112. 'file' => $this->getFile($parameters[0], $event),
  113. 'group' => $this->generateGroupParameter($parameters[1]),
  114. ];
  115. }
  116. return [];
  117. }
  118. /**
  119. * @param string $gid
  120. * @return array
  121. */
  122. protected function generateGroupParameter($gid) {
  123. if (!isset($this->groupDisplayNames[$gid])) {
  124. $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
  125. }
  126. return [
  127. 'type' => 'user-group',
  128. 'id' => $gid,
  129. 'name' => $this->groupDisplayNames[$gid],
  130. ];
  131. }
  132. /**
  133. * @param string $gid
  134. * @return string
  135. */
  136. protected function getGroupDisplayName($gid) {
  137. $group = $this->groupManager->get($gid);
  138. if ($group instanceof IGroup) {
  139. return $group->getDisplayName();
  140. }
  141. return $gid;
  142. }
  143. }