1
0

GroupProvider.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\Settings\Activity;
  7. use InvalidArgumentException;
  8. use OCP\Activity\IEvent;
  9. use OCP\Activity\IManager;
  10. use OCP\Activity\IProvider;
  11. use OCP\IGroup;
  12. use OCP\IGroupManager;
  13. use OCP\IURLGenerator;
  14. use OCP\IUserManager;
  15. use OCP\L10N\IFactory as L10nFactory;
  16. class GroupProvider implements IProvider {
  17. public const ADDED_TO_GROUP = 'group_added';
  18. public const REMOVED_FROM_GROUP = 'group_removed';
  19. /** @var L10nFactory */
  20. private $l10n;
  21. /** @var IURLGenerator */
  22. private $urlGenerator;
  23. /** @var IManager */
  24. private $activityManager;
  25. /** @var IUserManager */
  26. protected $userManager;
  27. /** @var IGroupManager */
  28. protected $groupManager;
  29. /** @var string[] */
  30. protected $groupDisplayNames = [];
  31. public function __construct(L10nFactory $l10n,
  32. IURLGenerator $urlGenerator,
  33. IManager $activityManager,
  34. IUserManager $userManager,
  35. IGroupManager $groupManager) {
  36. $this->urlGenerator = $urlGenerator;
  37. $this->l10n = $l10n;
  38. $this->activityManager = $activityManager;
  39. $this->userManager = $userManager;
  40. $this->groupManager = $groupManager;
  41. }
  42. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  43. if ($event->getType() !== 'group_settings') {
  44. throw new InvalidArgumentException();
  45. }
  46. $l = $this->l10n->get('settings', $language);
  47. $params = $event->getSubjectParameters();
  48. $parsedParameters = [
  49. 'user' => $this->generateUserParameter($params['user']),
  50. 'group' => $this->generateGroupParameter($params['group']),
  51. ];
  52. if (isset($params['actor'])) {
  53. $parsedParameters['actor'] = $this->generateUserParameter($params['actor']);
  54. }
  55. switch ($event->getSubject()) {
  56. case self::ADDED_TO_GROUP:
  57. if (isset($parsedParameters['actor'])) {
  58. if ($this->activityManager->getCurrentUserId() === $params['user']) {
  59. $subject = $l->t('{actor} added you to group {group}');
  60. } elseif (isset($params['actor']) && $this->activityManager->getCurrentUserId() === $params['actor']) {
  61. $subject = $l->t('You added {user} to group {group}');
  62. } else {
  63. $subject = $l->t('{actor} added {user} to group {group}');
  64. }
  65. } elseif ($this->activityManager->getCurrentUserId() === $params['user']) {
  66. $subject = $l->t('An administrator added you to group {group}');
  67. } else {
  68. $subject = $l->t('An administrator added {user} to group {group}');
  69. }
  70. break;
  71. case self::REMOVED_FROM_GROUP:
  72. if (isset($parsedParameters['actor'])) {
  73. if ($this->activityManager->getCurrentUserId() === $params['user']) {
  74. $subject = $l->t('{actor} removed you from group {group}');
  75. } elseif (isset($params['actor']) && $this->activityManager->getCurrentUserId() === $params['actor']) {
  76. $subject = $l->t('You removed {user} from group {group}');
  77. } else {
  78. $subject = $l->t('{actor} removed {user} from group {group}');
  79. }
  80. } elseif ($this->activityManager->getCurrentUserId() === $params['user']) {
  81. $subject = $l->t('An administrator removed you from group {group}');
  82. } else {
  83. $subject = $l->t('An administrator removed {user} from group {group}');
  84. }
  85. break;
  86. default:
  87. throw new InvalidArgumentException();
  88. }
  89. $this->setSubjects($event, $subject, $parsedParameters);
  90. return $event;
  91. }
  92. /**
  93. * @throws \InvalidArgumentException
  94. */
  95. protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
  96. $event->setRichSubject($subject, $parameters);
  97. }
  98. /**
  99. * @param string $gid
  100. * @return array
  101. */
  102. protected function generateGroupParameter(string $gid): array {
  103. if (!isset($this->groupDisplayNames[$gid])) {
  104. $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
  105. }
  106. return [
  107. 'type' => 'user-group',
  108. 'id' => $gid,
  109. 'name' => $this->groupDisplayNames[$gid],
  110. ];
  111. }
  112. /**
  113. * @param string $gid
  114. * @return string
  115. */
  116. protected function getGroupDisplayName(string $gid): string {
  117. $group = $this->groupManager->get($gid);
  118. if ($group instanceof IGroup) {
  119. return $group->getDisplayName();
  120. }
  121. return $gid;
  122. }
  123. protected function generateUserParameter(string $uid): array {
  124. return [
  125. 'type' => 'user',
  126. 'id' => $uid,
  127. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  128. ];
  129. }
  130. }