Base.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CardDAV\Activity\Provider;
  8. use OCA\DAV\CardDAV\CardDavBackend;
  9. use OCP\Activity\IEvent;
  10. use OCP\Activity\IProvider;
  11. use OCP\IGroup;
  12. use OCP\IGroupManager;
  13. use OCP\IL10N;
  14. use OCP\IURLGenerator;
  15. use OCP\IUserManager;
  16. abstract class Base implements IProvider {
  17. /** @var IUserManager */
  18. protected $userManager;
  19. /** @var string[] */
  20. protected $userDisplayNames = [];
  21. /** @var IGroupManager */
  22. protected $groupManager;
  23. /** @var string[] */
  24. protected $groupDisplayNames = [];
  25. /** @var IURLGenerator */
  26. protected $url;
  27. public function __construct(IUserManager $userManager,
  28. IGroupManager $groupManager,
  29. IURLGenerator $urlGenerator) {
  30. $this->userManager = $userManager;
  31. $this->groupManager = $groupManager;
  32. $this->url = $urlGenerator;
  33. }
  34. protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
  35. $event->setRichSubject($subject, $parameters);
  36. }
  37. /**
  38. * @param array $data
  39. * @param IL10N $l
  40. * @return array
  41. */
  42. protected function generateAddressbookParameter(array $data, IL10N $l): array {
  43. if ($data['uri'] === CardDavBackend::PERSONAL_ADDRESSBOOK_URI &&
  44. $data['name'] === CardDavBackend::PERSONAL_ADDRESSBOOK_NAME) {
  45. return [
  46. 'type' => 'addressbook',
  47. 'id' => $data['id'],
  48. 'name' => $l->t('Personal'),
  49. ];
  50. }
  51. return [
  52. 'type' => 'addressbook',
  53. 'id' => $data['id'],
  54. 'name' => $data['name'],
  55. ];
  56. }
  57. protected function generateUserParameter(string $uid): array {
  58. return [
  59. 'type' => 'user',
  60. 'id' => $uid,
  61. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  62. ];
  63. }
  64. /**
  65. * @param string $gid
  66. * @return array
  67. */
  68. protected function generateGroupParameter(string $gid): array {
  69. if (!isset($this->groupDisplayNames[$gid])) {
  70. $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
  71. }
  72. return [
  73. 'type' => 'user-group',
  74. 'id' => $gid,
  75. 'name' => $this->groupDisplayNames[$gid],
  76. ];
  77. }
  78. /**
  79. * @param string $gid
  80. * @return string
  81. */
  82. protected function getGroupDisplayName(string $gid): string {
  83. $group = $this->groupManager->get($gid);
  84. if ($group instanceof IGroup) {
  85. return $group->getDisplayName();
  86. }
  87. return $gid;
  88. }
  89. }