Todo.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 OCP\Activity\Exceptions\UnknownActivityException;
  8. use OCP\Activity\IEvent;
  9. class Todo extends Event {
  10. /**
  11. * @param string $language
  12. * @param IEvent $event
  13. * @param IEvent|null $previousEvent
  14. * @return IEvent
  15. * @throws UnknownActivityException
  16. * @since 11.0.0
  17. */
  18. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  19. if ($event->getApp() !== 'dav' || $event->getType() !== 'calendar_todo') {
  20. throw new UnknownActivityException();
  21. }
  22. $this->l = $this->languageFactory->get('dav', $language);
  23. if ($this->activityManager->getRequirePNG()) {
  24. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/checkmark.png')));
  25. } else {
  26. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/checkmark.svg')));
  27. }
  28. if ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_todo') {
  29. $subject = $this->l->t('{actor} created to-do {todo} in list {calendar}');
  30. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_todo_self') {
  31. $subject = $this->l->t('You created to-do {todo} in list {calendar}');
  32. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_todo') {
  33. $subject = $this->l->t('{actor} deleted to-do {todo} from list {calendar}');
  34. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_todo_self') {
  35. $subject = $this->l->t('You deleted to-do {todo} from list {calendar}');
  36. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo') {
  37. $subject = $this->l->t('{actor} updated to-do {todo} in list {calendar}');
  38. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_self') {
  39. $subject = $this->l->t('You updated to-do {todo} in list {calendar}');
  40. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_completed') {
  41. $subject = $this->l->t('{actor} solved to-do {todo} in list {calendar}');
  42. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_completed_self') {
  43. $subject = $this->l->t('You solved to-do {todo} in list {calendar}');
  44. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action') {
  45. $subject = $this->l->t('{actor} reopened to-do {todo} in list {calendar}');
  46. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self') {
  47. $subject = $this->l->t('You reopened to-do {todo} in list {calendar}');
  48. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE . '_todo') {
  49. $subject = $this->l->t('{actor} moved to-do {todo} from list {sourceCalendar} to list {targetCalendar}');
  50. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE . '_todo_self') {
  51. $subject = $this->l->t('You moved to-do {todo} from list {sourceCalendar} to list {targetCalendar}');
  52. } else {
  53. throw new UnknownActivityException();
  54. }
  55. $parsedParameters = $this->getParameters($event);
  56. $this->setSubjects($event, $subject, $parsedParameters);
  57. $event = $this->eventMerger->mergeEvents('todo', $event, $previousEvent);
  58. return $event;
  59. }
  60. /**
  61. * @param IEvent $event
  62. * @return array
  63. */
  64. protected function getParameters(IEvent $event) {
  65. $subject = $event->getSubject();
  66. $parameters = $event->getSubjectParameters();
  67. // Nextcloud 13+
  68. if (isset($parameters['calendar'])) {
  69. switch ($subject) {
  70. case self::SUBJECT_OBJECT_ADD . '_todo':
  71. case self::SUBJECT_OBJECT_DELETE . '_todo':
  72. case self::SUBJECT_OBJECT_UPDATE . '_todo':
  73. case self::SUBJECT_OBJECT_UPDATE . '_todo_completed':
  74. case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action':
  75. return [
  76. 'actor' => $this->generateUserParameter($parameters['actor']),
  77. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  78. 'todo' => $this->generateObjectParameter($parameters['object'], $event->getAffectedUser()),
  79. ];
  80. case self::SUBJECT_OBJECT_ADD . '_todo_self':
  81. case self::SUBJECT_OBJECT_DELETE . '_todo_self':
  82. case self::SUBJECT_OBJECT_UPDATE . '_todo_self':
  83. case self::SUBJECT_OBJECT_UPDATE . '_todo_completed_self':
  84. case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self':
  85. return [
  86. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  87. 'todo' => $this->generateObjectParameter($parameters['object'], $event->getAffectedUser()),
  88. ];
  89. }
  90. }
  91. if (isset($parameters['sourceCalendar']) && isset($parameters['targetCalendar'])) {
  92. switch ($subject) {
  93. case self::SUBJECT_OBJECT_MOVE . '_todo':
  94. return [
  95. 'actor' => $this->generateUserParameter($parameters['actor']),
  96. 'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
  97. 'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
  98. 'todo' => $this->generateObjectParameter($parameters['object'], $event->getAffectedUser()),
  99. ];
  100. case self::SUBJECT_OBJECT_MOVE . '_todo_self':
  101. return [
  102. 'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
  103. 'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
  104. 'todo' => $this->generateObjectParameter($parameters['object'], $event->getAffectedUser()),
  105. ];
  106. }
  107. }
  108. // Legacy - Do NOT Remove unless necessary
  109. // Removing this will break parsing of activities that were created on
  110. // Nextcloud 12, so we should keep this as long as it's acceptable.
  111. // Otherwise if people upgrade over multiple releases in a short period,
  112. // they will get the dead entries in their stream.
  113. switch ($subject) {
  114. case self::SUBJECT_OBJECT_ADD . '_todo':
  115. case self::SUBJECT_OBJECT_DELETE . '_todo':
  116. case self::SUBJECT_OBJECT_UPDATE . '_todo':
  117. case self::SUBJECT_OBJECT_UPDATE . '_todo_completed':
  118. case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action':
  119. return [
  120. 'actor' => $this->generateUserParameter($parameters[0]),
  121. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  122. 'todo' => $this->generateObjectParameter($parameters[2], $event->getAffectedUser()),
  123. ];
  124. case self::SUBJECT_OBJECT_ADD . '_todo_self':
  125. case self::SUBJECT_OBJECT_DELETE . '_todo_self':
  126. case self::SUBJECT_OBJECT_UPDATE . '_todo_self':
  127. case self::SUBJECT_OBJECT_UPDATE . '_todo_completed_self':
  128. case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self':
  129. return [
  130. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  131. 'todo' => $this->generateObjectParameter($parameters[2], $event->getAffectedUser()),
  132. ];
  133. }
  134. throw new \InvalidArgumentException();
  135. }
  136. }