You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Base.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\DAV\CalDAV\Activity\Provider;
  24. use OCA\DAV\CalDAV\CalDavBackend;
  25. use OCP\Activity\IEvent;
  26. use OCP\Activity\IProvider;
  27. use OCP\IGroup;
  28. use OCP\IGroupManager;
  29. use OCP\IL10N;
  30. use OCP\IUser;
  31. use OCP\IUserManager;
  32. abstract class Base implements IProvider {
  33. /** @var IUserManager */
  34. protected $userManager;
  35. /** @var string[] */
  36. protected $userDisplayNames = [];
  37. /** @var IGroupManager */
  38. protected $groupManager;
  39. /** @var string[] */
  40. protected $groupDisplayNames = [];
  41. /**
  42. * @param IUserManager $userManager
  43. * @param IGroupManager $groupManager
  44. */
  45. public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
  46. $this->userManager = $userManager;
  47. $this->groupManager = $groupManager;
  48. }
  49. /**
  50. * @param IEvent $event
  51. * @param string $subject
  52. * @param array $parameters
  53. */
  54. protected function setSubjects(IEvent $event, $subject, array $parameters) {
  55. $placeholders = $replacements = [];
  56. foreach ($parameters as $placeholder => $parameter) {
  57. $placeholders[] = '{' . $placeholder . '}';
  58. $replacements[] = $parameter['name'];
  59. }
  60. $event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
  61. ->setRichSubject($subject, $parameters);
  62. }
  63. /**
  64. * @param array $eventData
  65. * @return array
  66. */
  67. protected function generateObjectParameter($eventData) {
  68. if (!is_array($eventData) || !isset($eventData['id']) || !isset($eventData['name'])) {
  69. throw new \InvalidArgumentException();
  70. }
  71. return [
  72. 'type' => 'calendar-event',
  73. 'id' => $eventData['id'],
  74. 'name' => $eventData['name'],
  75. ];
  76. }
  77. /**
  78. * @param array $data
  79. * @param IL10N $l
  80. * @return array
  81. */
  82. protected function generateCalendarParameter($data, IL10N $l) {
  83. if ($data['uri'] === CalDavBackend::PERSONAL_CALENDAR_URI &&
  84. $data['name'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
  85. return [
  86. 'type' => 'calendar',
  87. 'id' => $data['id'],
  88. 'name' => $l->t('Personal'),
  89. ];
  90. }
  91. return [
  92. 'type' => 'calendar',
  93. 'id' => $data['id'],
  94. 'name' => $data['name'],
  95. ];
  96. }
  97. /**
  98. * @param int $id
  99. * @param string $name
  100. * @return array
  101. */
  102. protected function generateLegacyCalendarParameter($id, $name) {
  103. return [
  104. 'type' => 'calendar',
  105. 'id' => $id,
  106. 'name' => $name,
  107. ];
  108. }
  109. /**
  110. * @param string $uid
  111. * @return array
  112. */
  113. protected function generateUserParameter($uid) {
  114. if (!isset($this->userDisplayNames[$uid])) {
  115. $this->userDisplayNames[$uid] = $this->getUserDisplayName($uid);
  116. }
  117. return [
  118. 'type' => 'user',
  119. 'id' => $uid,
  120. 'name' => $this->userDisplayNames[$uid],
  121. ];
  122. }
  123. /**
  124. * @param string $uid
  125. * @return string
  126. */
  127. protected function getUserDisplayName($uid) {
  128. $user = $this->userManager->get($uid);
  129. if ($user instanceof IUser) {
  130. return $user->getDisplayName();
  131. }
  132. return $uid;
  133. }
  134. /**
  135. * @param string $gid
  136. * @return array
  137. */
  138. protected function generateGroupParameter($gid) {
  139. if (!isset($this->groupDisplayNames[$gid])) {
  140. $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
  141. }
  142. return [
  143. 'type' => 'user-group',
  144. 'id' => $gid,
  145. 'name' => $this->groupDisplayNames[$gid],
  146. ];
  147. }
  148. /**
  149. * @param string $gid
  150. * @return string
  151. */
  152. protected function getGroupDisplayName($gid) {
  153. $group = $this->groupManager->get($gid);
  154. if ($group instanceof IGroup) {
  155. return $group->getDisplayName();
  156. }
  157. return $gid;
  158. }
  159. }