GroupProvider.php 3.9 KB

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