GroupProvider.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\IUser;
  33. use OCP\IUserManager;
  34. use OCP\L10N\IFactory as L10nFactory;
  35. class GroupProvider implements IProvider {
  36. public const ADDED_TO_GROUP = 'group_added';
  37. public const REMOVED_FROM_GROUP = 'group_removed';
  38. /** @var L10nFactory */
  39. private $l10n;
  40. /** @var IURLGenerator */
  41. private $urlGenerator;
  42. /** @var IManager */
  43. private $activityManager;
  44. /** @var IUserManager */
  45. protected $userManager;
  46. /** @var IGroupManager */
  47. protected $groupManager;
  48. /** @var string[] */
  49. protected $groupDisplayNames = [];
  50. public function __construct(L10nFactory $l10n,
  51. IURLGenerator $urlGenerator,
  52. IManager $activityManager,
  53. IUserManager $userManager,
  54. IGroupManager $groupManager) {
  55. $this->urlGenerator = $urlGenerator;
  56. $this->l10n = $l10n;
  57. $this->activityManager = $activityManager;
  58. $this->userManager = $userManager;
  59. $this->groupManager = $groupManager;
  60. }
  61. public function parse($language, IEvent $event, IEvent $previousEvent = null) {
  62. if ($event->getType() !== 'group_settings') {
  63. throw new InvalidArgumentException();
  64. }
  65. $l = $this->l10n->get('settings', $language);
  66. $params = $event->getSubjectParameters();
  67. $parsedParameters = [
  68. 'user' => $this->generateUserParameter($params['user']),
  69. 'group' => $this->generateGroupParameter($params['group']),
  70. ];
  71. if (isset($params['actor'])) {
  72. $parsedParameters['actor'] = $this->generateUserParameter($params['actor']);
  73. }
  74. switch ($event->getSubject()) {
  75. case self::ADDED_TO_GROUP:
  76. if (isset($parsedParameters['actor'])) {
  77. if ($this->activityManager->getCurrentUserId() === $params['user']) {
  78. $subject = $l->t('{actor} added you to group {group}');
  79. } elseif (isset($params['actor']) && $this->activityManager->getCurrentUserId() === $params['actor']) {
  80. $subject = $l->t('You added {user} to group {group}');
  81. } else {
  82. $subject = $l->t('{actor} added {user} to group {group}');
  83. }
  84. } elseif ($this->activityManager->getCurrentUserId() === $params['user']) {
  85. $subject = $l->t('An administrator added you to group {group}');
  86. } else {
  87. $subject = $l->t('An administrator added {user} to group {group}');
  88. }
  89. break;
  90. case self::REMOVED_FROM_GROUP:
  91. if (isset($parsedParameters['actor'])) {
  92. if ($this->activityManager->getCurrentUserId() === $params['user']) {
  93. $subject = $l->t('{actor} removed you from group {group}');
  94. } elseif (isset($params['actor']) && $this->activityManager->getCurrentUserId() === $params['actor']) {
  95. $subject = $l->t('You removed {user} from group {group}');
  96. } else {
  97. $subject = $l->t('{actor} removed {user} from group {group}');
  98. }
  99. } elseif ($this->activityManager->getCurrentUserId() === $params['user']) {
  100. $subject = $l->t('An administrator removed you from group {group}');
  101. } else {
  102. $subject = $l->t('An administrator removed {user} from group {group}');
  103. }
  104. break;
  105. default:
  106. throw new InvalidArgumentException();
  107. }
  108. $this->setSubjects($event, $subject, $parsedParameters);
  109. return $event;
  110. }
  111. /**
  112. * @param IEvent $event
  113. * @param string $subject
  114. * @param array $parameters
  115. * @throws \InvalidArgumentException
  116. */
  117. protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
  118. $placeholders = $replacements = [];
  119. foreach ($parameters as $placeholder => $parameter) {
  120. $placeholders[] = '{' . $placeholder . '}';
  121. $replacements[] = $parameter['name'];
  122. }
  123. $event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
  124. ->setRichSubject($subject, $parameters);
  125. }
  126. /**
  127. * @param string $gid
  128. * @return array
  129. */
  130. protected function generateGroupParameter(string $gid): array {
  131. if (!isset($this->groupDisplayNames[$gid])) {
  132. $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
  133. }
  134. return [
  135. 'type' => 'user-group',
  136. 'id' => $gid,
  137. 'name' => $this->groupDisplayNames[$gid],
  138. ];
  139. }
  140. /**
  141. * @param string $gid
  142. * @return string
  143. */
  144. protected function getGroupDisplayName(string $gid): string {
  145. $group = $this->groupManager->get($gid);
  146. if ($group instanceof IGroup) {
  147. return $group->getDisplayName();
  148. }
  149. return $gid;
  150. }
  151. protected function generateUserParameter(string $uid): array {
  152. return [
  153. 'type' => 'user',
  154. 'id' => $uid,
  155. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  156. ];
  157. }
  158. }