Groups.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 string[] */
  24. protected $groupDisplayNames = [];
  25. public function __construct(
  26. IFactory $languageFactory,
  27. IURLGenerator $url,
  28. IManager $activityManager,
  29. IUserManager $userManager,
  30. ICloudIdManager $cloudIdManager,
  31. IContactsManager $contactsManager,
  32. IEventMerger $eventMerger,
  33. protected IGroupManager $groupManager,
  34. ) {
  35. parent::__construct($languageFactory, $url, $activityManager, $userManager, $cloudIdManager, $contactsManager, $eventMerger);
  36. }
  37. /**
  38. * @param IEvent $event
  39. * @return IEvent
  40. * @throws \InvalidArgumentException
  41. * @since 11.0.0
  42. */
  43. public function parseShortVersion(IEvent $event) {
  44. $parsedParameters = $this->getParsedParameters($event);
  45. if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) {
  46. $subject = $this->l->t('Shared with group {group}');
  47. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) {
  48. $subject = $this->l->t('Removed share for group {group}');
  49. } elseif ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) {
  50. $subject = $this->l->t('{actor} shared with group {group}');
  51. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) {
  52. $subject = $this->l->t('{actor} removed share for group {group}');
  53. } elseif ($event->getSubject() === self::SUBJECT_EXPIRED_GROUP) {
  54. $subject = $this->l->t('Share for group {group} expired');
  55. } else {
  56. throw new \InvalidArgumentException();
  57. }
  58. if ($this->activityManager->getRequirePNG()) {
  59. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  60. } else {
  61. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  62. }
  63. $this->setSubjects($event, $subject, $parsedParameters);
  64. return $event;
  65. }
  66. /**
  67. * @param IEvent $event
  68. * @param IEvent|null $previousEvent
  69. * @return IEvent
  70. * @throws \InvalidArgumentException
  71. * @since 11.0.0
  72. */
  73. public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null) {
  74. $parsedParameters = $this->getParsedParameters($event);
  75. if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) {
  76. $subject = $this->l->t('You shared {file} with group {group}');
  77. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) {
  78. $subject = $this->l->t('You removed group {group} from {file}');
  79. } elseif ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) {
  80. $subject = $this->l->t('{actor} shared {file} with group {group}');
  81. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) {
  82. $subject = $this->l->t('{actor} removed group {group} from {file}');
  83. } elseif ($event->getSubject() === self::SUBJECT_EXPIRED_GROUP) {
  84. $subject = $this->l->t('Share for file {file} with group {group} expired');
  85. } else {
  86. throw new \InvalidArgumentException();
  87. }
  88. if ($this->activityManager->getRequirePNG()) {
  89. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  90. } else {
  91. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  92. }
  93. $this->setSubjects($event, $subject, $parsedParameters);
  94. return $event;
  95. }
  96. protected function getParsedParameters(IEvent $event) {
  97. $subject = $event->getSubject();
  98. $parameters = $event->getSubjectParameters();
  99. switch ($subject) {
  100. case self::SUBJECT_RESHARED_GROUP_BY:
  101. case self::SUBJECT_UNSHARED_GROUP_BY:
  102. return [
  103. 'file' => $this->getFile($parameters[0], $event),
  104. 'group' => $this->generateGroupParameter($parameters[2]),
  105. 'actor' => $this->getUser($parameters[1]),
  106. ];
  107. case self::SUBJECT_SHARED_GROUP_SELF:
  108. case self::SUBJECT_UNSHARED_GROUP_SELF:
  109. case self::SUBJECT_EXPIRED_GROUP:
  110. return [
  111. 'file' => $this->getFile($parameters[0], $event),
  112. 'group' => $this->generateGroupParameter($parameters[1]),
  113. ];
  114. }
  115. return [];
  116. }
  117. /**
  118. * @param string $gid
  119. * @return array
  120. */
  121. protected function generateGroupParameter($gid) {
  122. if (!isset($this->groupDisplayNames[$gid])) {
  123. $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
  124. }
  125. return [
  126. 'type' => 'user-group',
  127. 'id' => $gid,
  128. 'name' => $this->groupDisplayNames[$gid],
  129. ];
  130. }
  131. /**
  132. * @param string $gid
  133. * @return string
  134. */
  135. protected function getGroupDisplayName($gid) {
  136. $group = $this->groupManager->get($gid);
  137. if ($group instanceof IGroup) {
  138. return $group->getDisplayName();
  139. }
  140. return $gid;
  141. }
  142. }