Base.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\DAV\CalDAV\Activity\Provider;
  7. use OCA\DAV\CalDAV\CalDavBackend;
  8. use OCP\Activity\IEvent;
  9. use OCP\Activity\IProvider;
  10. use OCP\IGroup;
  11. use OCP\IGroupManager;
  12. use OCP\IL10N;
  13. use OCP\IURLGenerator;
  14. use OCP\IUserManager;
  15. abstract class Base implements IProvider {
  16. /** @var string[] */
  17. protected $groupDisplayNames = [];
  18. /**
  19. * @param IUserManager $userManager
  20. * @param IGroupManager $groupManager
  21. * @param IURLGenerator $url
  22. */
  23. public function __construct(
  24. protected IUserManager $userManager,
  25. protected IGroupManager $groupManager,
  26. protected IURLGenerator $url,
  27. ) {
  28. }
  29. protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
  30. $event->setRichSubject($subject, $parameters);
  31. }
  32. /**
  33. * @param array $data
  34. * @param IL10N $l
  35. * @return array
  36. */
  37. protected function generateCalendarParameter($data, IL10N $l) {
  38. if ($data['uri'] === CalDavBackend::PERSONAL_CALENDAR_URI &&
  39. $data['name'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
  40. return [
  41. 'type' => 'calendar',
  42. 'id' => (string)$data['id'],
  43. 'name' => $l->t('Personal'),
  44. ];
  45. }
  46. return [
  47. 'type' => 'calendar',
  48. 'id' => (string)$data['id'],
  49. 'name' => $data['name'],
  50. ];
  51. }
  52. /**
  53. * @param int $id
  54. * @param string $name
  55. * @return array
  56. */
  57. protected function generateLegacyCalendarParameter($id, $name) {
  58. return [
  59. 'type' => 'calendar',
  60. 'id' => (string)$id,
  61. 'name' => $name,
  62. ];
  63. }
  64. protected function generateUserParameter(string $uid): array {
  65. return [
  66. 'type' => 'user',
  67. 'id' => $uid,
  68. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  69. ];
  70. }
  71. /**
  72. * @param string $gid
  73. * @return array
  74. */
  75. protected function generateGroupParameter($gid) {
  76. if (!isset($this->groupDisplayNames[$gid])) {
  77. $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
  78. }
  79. return [
  80. 'type' => 'user-group',
  81. 'id' => $gid,
  82. 'name' => $this->groupDisplayNames[$gid],
  83. ];
  84. }
  85. /**
  86. * @param string $gid
  87. * @return string
  88. */
  89. protected function getGroupDisplayName($gid) {
  90. $group = $this->groupManager->get($gid);
  91. if ($group instanceof IGroup) {
  92. return $group->getDisplayName();
  93. }
  94. return $gid;
  95. }
  96. }