Groups.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.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\Activity\Providers;
  26. use OCP\Activity\IEvent;
  27. use OCP\Activity\IEventMerger;
  28. use OCP\Activity\IManager;
  29. use OCP\Contacts\IManager as IContactsManager;
  30. use OCP\Federation\ICloudIdManager;
  31. use OCP\IGroup;
  32. use OCP\IGroupManager;
  33. use OCP\IURLGenerator;
  34. use OCP\IUserManager;
  35. use OCP\L10N\IFactory;
  36. class Groups extends Base {
  37. public const SUBJECT_SHARED_GROUP_SELF = 'shared_group_self';
  38. public const SUBJECT_RESHARED_GROUP_BY = 'reshared_group_by';
  39. public const SUBJECT_UNSHARED_GROUP_SELF = 'unshared_group_self';
  40. public const SUBJECT_UNSHARED_GROUP_BY = 'unshared_group_by';
  41. public const SUBJECT_EXPIRED_GROUP = 'expired_group';
  42. /** @var IGroupManager */
  43. protected $groupManager;
  44. /** @var string[] */
  45. protected $groupDisplayNames = [];
  46. public function __construct(IFactory $languageFactory,
  47. IURLGenerator $url,
  48. IManager $activityManager,
  49. IUserManager $userManager,
  50. ICloudIdManager $cloudIdManager,
  51. IContactsManager $contactsManager,
  52. IEventMerger $eventMerger,
  53. IGroupManager $groupManager) {
  54. parent::__construct($languageFactory, $url, $activityManager, $userManager, $cloudIdManager, $contactsManager, $eventMerger);
  55. $this->groupManager = $groupManager;
  56. }
  57. /**
  58. * @param IEvent $event
  59. * @return IEvent
  60. * @throws \InvalidArgumentException
  61. * @since 11.0.0
  62. */
  63. public function parseShortVersion(IEvent $event) {
  64. $parsedParameters = $this->getParsedParameters($event);
  65. if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) {
  66. $subject = $this->l->t('Shared with group {group}');
  67. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) {
  68. $subject = $this->l->t('Removed share for group {group}');
  69. } elseif ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) {
  70. $subject = $this->l->t('{actor} shared with group {group}');
  71. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) {
  72. $subject = $this->l->t('{actor} removed share for group {group}');
  73. } elseif ($event->getSubject() === self::SUBJECT_EXPIRED_GROUP) {
  74. $subject = $this->l->t('Share for group {group} expired');
  75. } else {
  76. throw new \InvalidArgumentException();
  77. }
  78. if ($this->activityManager->getRequirePNG()) {
  79. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  80. } else {
  81. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  82. }
  83. $this->setSubjects($event, $subject, $parsedParameters);
  84. return $event;
  85. }
  86. /**
  87. * @param IEvent $event
  88. * @param IEvent|null $previousEvent
  89. * @return IEvent
  90. * @throws \InvalidArgumentException
  91. * @since 11.0.0
  92. */
  93. public function parseLongVersion(IEvent $event, IEvent $previousEvent = null) {
  94. $parsedParameters = $this->getParsedParameters($event);
  95. if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) {
  96. $subject = $this->l->t('You shared {file} with group {group}');
  97. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) {
  98. $subject = $this->l->t('You removed group {group} from {file}');
  99. } elseif ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) {
  100. $subject = $this->l->t('{actor} shared {file} with group {group}');
  101. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) {
  102. $subject = $this->l->t('{actor} removed group {group} from {file}');
  103. } elseif ($event->getSubject() === self::SUBJECT_EXPIRED_GROUP) {
  104. $subject = $this->l->t('Share for file {file} with group {group} expired');
  105. } else {
  106. throw new \InvalidArgumentException();
  107. }
  108. if ($this->activityManager->getRequirePNG()) {
  109. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  110. } else {
  111. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  112. }
  113. $this->setSubjects($event, $subject, $parsedParameters);
  114. return $event;
  115. }
  116. protected function getParsedParameters(IEvent $event) {
  117. $subject = $event->getSubject();
  118. $parameters = $event->getSubjectParameters();
  119. switch ($subject) {
  120. case self::SUBJECT_RESHARED_GROUP_BY:
  121. case self::SUBJECT_UNSHARED_GROUP_BY:
  122. return [
  123. 'file' => $this->getFile($parameters[0], $event),
  124. 'group' => $this->generateGroupParameter($parameters[2]),
  125. 'actor' => $this->getUser($parameters[1]),
  126. ];
  127. case self::SUBJECT_SHARED_GROUP_SELF:
  128. case self::SUBJECT_UNSHARED_GROUP_SELF:
  129. case self::SUBJECT_EXPIRED_GROUP:
  130. return [
  131. 'file' => $this->getFile($parameters[0], $event),
  132. 'group' => $this->generateGroupParameter($parameters[1]),
  133. ];
  134. }
  135. return [];
  136. }
  137. /**
  138. * @param string $gid
  139. * @return array
  140. */
  141. protected function generateGroupParameter($gid) {
  142. if (!isset($this->groupDisplayNames[$gid])) {
  143. $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
  144. }
  145. return [
  146. 'type' => 'user-group',
  147. 'id' => $gid,
  148. 'name' => $this->groupDisplayNames[$gid],
  149. ];
  150. }
  151. /**
  152. * @param string $gid
  153. * @return string
  154. */
  155. protected function getGroupDisplayName($gid) {
  156. $group = $this->groupManager->get($gid);
  157. if ($group instanceof IGroup) {
  158. return $group->getDisplayName();
  159. }
  160. return $gid;
  161. }
  162. }