1
0

GroupProvider.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\Activity;
  25. use InvalidArgumentException;
  26. use OCP\Activity\IEvent;
  27. use OCP\Activity\IManager;
  28. use OCP\Activity\IProvider;
  29. use OCP\IGroup;
  30. use OCP\IGroupManager;
  31. use OCP\IURLGenerator;
  32. use OCP\IUserManager;
  33. use OCP\L10N\IFactory as L10nFactory;
  34. class GroupProvider implements IProvider {
  35. public const ADDED_TO_GROUP = 'group_added';
  36. public const REMOVED_FROM_GROUP = 'group_removed';
  37. /** @var L10nFactory */
  38. private $l10n;
  39. /** @var IURLGenerator */
  40. private $urlGenerator;
  41. /** @var IManager */
  42. private $activityManager;
  43. /** @var IUserManager */
  44. protected $userManager;
  45. /** @var IGroupManager */
  46. protected $groupManager;
  47. /** @var string[] */
  48. protected $groupDisplayNames = [];
  49. public function __construct(L10nFactory $l10n,
  50. IURLGenerator $urlGenerator,
  51. IManager $activityManager,
  52. IUserManager $userManager,
  53. IGroupManager $groupManager) {
  54. $this->urlGenerator = $urlGenerator;
  55. $this->l10n = $l10n;
  56. $this->activityManager = $activityManager;
  57. $this->userManager = $userManager;
  58. $this->groupManager = $groupManager;
  59. }
  60. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  61. if ($event->getType() !== 'group_settings') {
  62. throw new InvalidArgumentException();
  63. }
  64. $l = $this->l10n->get('settings', $language);
  65. $params = $event->getSubjectParameters();
  66. $parsedParameters = [
  67. 'user' => $this->generateUserParameter($params['user']),
  68. 'group' => $this->generateGroupParameter($params['group']),
  69. ];
  70. if (isset($params['actor'])) {
  71. $parsedParameters['actor'] = $this->generateUserParameter($params['actor']);
  72. }
  73. switch ($event->getSubject()) {
  74. case self::ADDED_TO_GROUP:
  75. if (isset($parsedParameters['actor'])) {
  76. if ($this->activityManager->getCurrentUserId() === $params['user']) {
  77. $subject = $l->t('{actor} added you to group {group}');
  78. } elseif (isset($params['actor']) && $this->activityManager->getCurrentUserId() === $params['actor']) {
  79. $subject = $l->t('You added {user} to group {group}');
  80. } else {
  81. $subject = $l->t('{actor} added {user} to group {group}');
  82. }
  83. } elseif ($this->activityManager->getCurrentUserId() === $params['user']) {
  84. $subject = $l->t('An administrator added you to group {group}');
  85. } else {
  86. $subject = $l->t('An administrator added {user} to group {group}');
  87. }
  88. break;
  89. case self::REMOVED_FROM_GROUP:
  90. if (isset($parsedParameters['actor'])) {
  91. if ($this->activityManager->getCurrentUserId() === $params['user']) {
  92. $subject = $l->t('{actor} removed you from group {group}');
  93. } elseif (isset($params['actor']) && $this->activityManager->getCurrentUserId() === $params['actor']) {
  94. $subject = $l->t('You removed {user} from group {group}');
  95. } else {
  96. $subject = $l->t('{actor} removed {user} from group {group}');
  97. }
  98. } elseif ($this->activityManager->getCurrentUserId() === $params['user']) {
  99. $subject = $l->t('An administrator removed you from group {group}');
  100. } else {
  101. $subject = $l->t('An administrator removed {user} from group {group}');
  102. }
  103. break;
  104. default:
  105. throw new InvalidArgumentException();
  106. }
  107. $this->setSubjects($event, $subject, $parsedParameters);
  108. return $event;
  109. }
  110. /**
  111. * @throws \InvalidArgumentException
  112. */
  113. protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
  114. $event->setRichSubject($subject, $parameters);
  115. }
  116. /**
  117. * @param string $gid
  118. * @return array
  119. */
  120. protected function generateGroupParameter(string $gid): array {
  121. if (!isset($this->groupDisplayNames[$gid])) {
  122. $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
  123. }
  124. return [
  125. 'type' => 'user-group',
  126. 'id' => $gid,
  127. 'name' => $this->groupDisplayNames[$gid],
  128. ];
  129. }
  130. /**
  131. * @param string $gid
  132. * @return string
  133. */
  134. protected function getGroupDisplayName(string $gid): string {
  135. $group = $this->groupManager->get($gid);
  136. if ($group instanceof IGroup) {
  137. return $group->getDisplayName();
  138. }
  139. return $gid;
  140. }
  141. protected function generateUserParameter(string $uid): array {
  142. return [
  143. 'type' => 'user',
  144. 'id' => $uid,
  145. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  146. ];
  147. }
  148. }