Todo.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\CalDAV\Activity\Provider;
  26. use OCP\Activity\IEvent;
  27. class Todo extends Event {
  28. /**
  29. * @param string $language
  30. * @param IEvent $event
  31. * @param IEvent|null $previousEvent
  32. * @return IEvent
  33. * @throws \InvalidArgumentException
  34. * @since 11.0.0
  35. */
  36. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  37. if ($event->getApp() !== 'dav' || $event->getType() !== 'calendar_todo') {
  38. throw new \InvalidArgumentException();
  39. }
  40. $this->l = $this->languageFactory->get('dav', $language);
  41. if ($this->activityManager->getRequirePNG()) {
  42. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/checkmark.png')));
  43. } else {
  44. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/checkmark.svg')));
  45. }
  46. if ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_todo') {
  47. $subject = $this->l->t('{actor} created to-do {todo} in list {calendar}');
  48. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_todo_self') {
  49. $subject = $this->l->t('You created to-do {todo} in list {calendar}');
  50. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_todo') {
  51. $subject = $this->l->t('{actor} deleted to-do {todo} from list {calendar}');
  52. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_todo_self') {
  53. $subject = $this->l->t('You deleted to-do {todo} from list {calendar}');
  54. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo') {
  55. $subject = $this->l->t('{actor} updated to-do {todo} in list {calendar}');
  56. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_self') {
  57. $subject = $this->l->t('You updated to-do {todo} in list {calendar}');
  58. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_completed') {
  59. $subject = $this->l->t('{actor} solved to-do {todo} in list {calendar}');
  60. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_completed_self') {
  61. $subject = $this->l->t('You solved to-do {todo} in list {calendar}');
  62. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action') {
  63. $subject = $this->l->t('{actor} reopened to-do {todo} in list {calendar}');
  64. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self') {
  65. $subject = $this->l->t('You reopened to-do {todo} in list {calendar}');
  66. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE . '_todo') {
  67. $subject = $this->l->t('{actor} moved to-do {todo} from list {sourceCalendar} to list {targetCalendar}');
  68. } elseif ($event->getSubject() === self::SUBJECT_OBJECT_MOVE . '_todo_self') {
  69. $subject = $this->l->t('You moved to-do {todo} from list {sourceCalendar} to list {targetCalendar}');
  70. } else {
  71. throw new \InvalidArgumentException();
  72. }
  73. $parsedParameters = $this->getParameters($event);
  74. $this->setSubjects($event, $subject, $parsedParameters);
  75. $event = $this->eventMerger->mergeEvents('todo', $event, $previousEvent);
  76. return $event;
  77. }
  78. /**
  79. * @param IEvent $event
  80. * @return array
  81. */
  82. protected function getParameters(IEvent $event) {
  83. $subject = $event->getSubject();
  84. $parameters = $event->getSubjectParameters();
  85. // Nextcloud 13+
  86. if (isset($parameters['calendar'])) {
  87. switch ($subject) {
  88. case self::SUBJECT_OBJECT_ADD . '_todo':
  89. case self::SUBJECT_OBJECT_DELETE . '_todo':
  90. case self::SUBJECT_OBJECT_UPDATE . '_todo':
  91. case self::SUBJECT_OBJECT_UPDATE . '_todo_completed':
  92. case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action':
  93. return [
  94. 'actor' => $this->generateUserParameter($parameters['actor']),
  95. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  96. 'todo' => $this->generateObjectParameter($parameters['object'], $event->getAffectedUser()),
  97. ];
  98. case self::SUBJECT_OBJECT_ADD . '_todo_self':
  99. case self::SUBJECT_OBJECT_DELETE . '_todo_self':
  100. case self::SUBJECT_OBJECT_UPDATE . '_todo_self':
  101. case self::SUBJECT_OBJECT_UPDATE . '_todo_completed_self':
  102. case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self':
  103. return [
  104. 'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
  105. 'todo' => $this->generateObjectParameter($parameters['object'], $event->getAffectedUser()),
  106. ];
  107. }
  108. }
  109. if (isset($parameters['sourceCalendar']) && isset($parameters['targetCalendar'])) {
  110. switch ($subject) {
  111. case self::SUBJECT_OBJECT_MOVE . '_todo':
  112. return [
  113. 'actor' => $this->generateUserParameter($parameters['actor']),
  114. 'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
  115. 'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
  116. 'todo' => $this->generateObjectParameter($parameters['object'], $event->getAffectedUser()),
  117. ];
  118. case self::SUBJECT_OBJECT_MOVE . '_todo_self':
  119. return [
  120. 'sourceCalendar' => $this->generateCalendarParameter($parameters['sourceCalendar'], $this->l),
  121. 'targetCalendar' => $this->generateCalendarParameter($parameters['targetCalendar'], $this->l),
  122. 'todo' => $this->generateObjectParameter($parameters['object'], $event->getAffectedUser()),
  123. ];
  124. }
  125. }
  126. // Legacy - Do NOT Remove unless necessary
  127. // Removing this will break parsing of activities that were created on
  128. // Nextcloud 12, so we should keep this as long as it's acceptable.
  129. // Otherwise if people upgrade over multiple releases in a short period,
  130. // they will get the dead entries in their stream.
  131. switch ($subject) {
  132. case self::SUBJECT_OBJECT_ADD . '_todo':
  133. case self::SUBJECT_OBJECT_DELETE . '_todo':
  134. case self::SUBJECT_OBJECT_UPDATE . '_todo':
  135. case self::SUBJECT_OBJECT_UPDATE . '_todo_completed':
  136. case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action':
  137. return [
  138. 'actor' => $this->generateUserParameter($parameters[0]),
  139. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  140. 'todo' => $this->generateObjectParameter($parameters[2], $event->getAffectedUser()),
  141. ];
  142. case self::SUBJECT_OBJECT_ADD . '_todo_self':
  143. case self::SUBJECT_OBJECT_DELETE . '_todo_self':
  144. case self::SUBJECT_OBJECT_UPDATE . '_todo_self':
  145. case self::SUBJECT_OBJECT_UPDATE . '_todo_completed_self':
  146. case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self':
  147. return [
  148. 'calendar' => $this->generateLegacyCalendarParameter($event->getObjectId(), $parameters[1]),
  149. 'todo' => $this->generateObjectParameter($parameters[2], $event->getAffectedUser()),
  150. ];
  151. }
  152. throw new \InvalidArgumentException();
  153. }
  154. }