GroupProvider.php 5.9 KB

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