Base.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Joas Schilling <coding@schilljs.com>
  5. *
  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\DAV\CardDAV\Activity\Provider;
  25. use OCA\DAV\CardDAV\CardDavBackend;
  26. use OCP\Activity\IEvent;
  27. use OCP\Activity\IProvider;
  28. use OCP\IGroup;
  29. use OCP\IGroupManager;
  30. use OCP\IL10N;
  31. use OCP\IURLGenerator;
  32. use OCP\IUserManager;
  33. abstract class Base implements IProvider {
  34. /** @var IUserManager */
  35. protected $userManager;
  36. /** @var string[] */
  37. protected $userDisplayNames = [];
  38. /** @var IGroupManager */
  39. protected $groupManager;
  40. /** @var string[] */
  41. protected $groupDisplayNames = [];
  42. /** @var IURLGenerator */
  43. protected $url;
  44. public function __construct(IUserManager $userManager,
  45. IGroupManager $groupManager,
  46. IURLGenerator $urlGenerator) {
  47. $this->userManager = $userManager;
  48. $this->groupManager = $groupManager;
  49. $this->url = $urlGenerator;
  50. }
  51. protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
  52. $event->setRichSubject($subject, $parameters);
  53. }
  54. /**
  55. * @param array $data
  56. * @param IL10N $l
  57. * @return array
  58. */
  59. protected function generateAddressbookParameter(array $data, IL10N $l): array {
  60. if ($data['uri'] === CardDavBackend::PERSONAL_ADDRESSBOOK_URI &&
  61. $data['name'] === CardDavBackend::PERSONAL_ADDRESSBOOK_NAME) {
  62. return [
  63. 'type' => 'addressbook',
  64. 'id' => $data['id'],
  65. 'name' => $l->t('Personal'),
  66. ];
  67. }
  68. return [
  69. 'type' => 'addressbook',
  70. 'id' => $data['id'],
  71. 'name' => $data['name'],
  72. ];
  73. }
  74. protected function generateUserParameter(string $uid): array {
  75. return [
  76. 'type' => 'user',
  77. 'id' => $uid,
  78. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  79. ];
  80. }
  81. /**
  82. * @param string $gid
  83. * @return array
  84. */
  85. protected function generateGroupParameter(string $gid): array {
  86. if (!isset($this->groupDisplayNames[$gid])) {
  87. $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
  88. }
  89. return [
  90. 'type' => 'user-group',
  91. 'id' => $gid,
  92. 'name' => $this->groupDisplayNames[$gid],
  93. ];
  94. }
  95. /**
  96. * @param string $gid
  97. * @return string
  98. */
  99. protected function getGroupDisplayName(string $gid): string {
  100. $group = $this->groupManager->get($gid);
  101. if ($group instanceof IGroup) {
  102. return $group->getDisplayName();
  103. }
  104. return $gid;
  105. }
  106. }