Todo.php 6.7 KB

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