Downloads.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Files_Sharing\Activity\Providers;
  7. use OCP\Activity\IEvent;
  8. class Downloads extends Base {
  9. public const SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED = 'public_shared_file_downloaded';
  10. public const SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED = 'public_shared_folder_downloaded';
  11. public const SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED = 'file_shared_with_email_downloaded';
  12. public const SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED = 'folder_shared_with_email_downloaded';
  13. /**
  14. * @param IEvent $event
  15. * @return IEvent
  16. * @throws \InvalidArgumentException
  17. * @since 11.0.0
  18. */
  19. public function parseShortVersion(IEvent $event) {
  20. $parsedParameters = $this->getParsedParameters($event);
  21. if ($event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED ||
  22. $event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED) {
  23. $subject = $this->l->t('Downloaded via public link');
  24. } elseif ($event->getSubject() === self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED ||
  25. $event->getSubject() === self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED) {
  26. $subject = $this->l->t('Downloaded by {email}');
  27. } else {
  28. throw new \InvalidArgumentException();
  29. }
  30. if ($this->activityManager->getRequirePNG()) {
  31. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.png')));
  32. } else {
  33. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
  34. }
  35. $this->setSubjects($event, $subject, $parsedParameters);
  36. return $event;
  37. }
  38. /**
  39. * @param IEvent $event
  40. * @param IEvent|null $previousEvent
  41. * @return IEvent
  42. * @throws \InvalidArgumentException
  43. * @since 11.0.0
  44. */
  45. public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null) {
  46. $parsedParameters = $this->getParsedParameters($event);
  47. if ($event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED ||
  48. $event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED) {
  49. if (!isset($parsedParameters['remote-address-hash']['type'])) {
  50. $subject = $this->l->t('{file} downloaded via public link');
  51. $this->setSubjects($event, $subject, $parsedParameters);
  52. } else {
  53. $subject = $this->l->t('{file} downloaded via public link');
  54. $this->setSubjects($event, $subject, $parsedParameters);
  55. $event = $this->eventMerger->mergeEvents('file', $event, $previousEvent);
  56. }
  57. } elseif ($event->getSubject() === self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED ||
  58. $event->getSubject() === self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED) {
  59. $subject = $this->l->t('{email} downloaded {file}');
  60. $this->setSubjects($event, $subject, $parsedParameters);
  61. } else {
  62. throw new \InvalidArgumentException();
  63. }
  64. if ($this->activityManager->getRequirePNG()) {
  65. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.png')));
  66. } else {
  67. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
  68. }
  69. return $event;
  70. }
  71. /**
  72. * @param IEvent $event
  73. * @return array
  74. * @throws \InvalidArgumentException
  75. */
  76. protected function getParsedParameters(IEvent $event) {
  77. $subject = $event->getSubject();
  78. $parameters = $event->getSubjectParameters();
  79. switch ($subject) {
  80. case self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED:
  81. case self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED:
  82. if (isset($parameters[1])) {
  83. return [
  84. 'file' => $this->getFile($parameters[0], $event),
  85. 'remote-address-hash' => [
  86. 'type' => 'highlight',
  87. 'id' => $parameters[1],
  88. 'name' => $parameters[1],
  89. 'link' => '',
  90. ],
  91. ];
  92. }
  93. return [
  94. 'file' => $this->getFile($parameters[0], $event),
  95. ];
  96. case self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED:
  97. case self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED:
  98. return [
  99. 'file' => $this->getFile($parameters[0], $event),
  100. 'email' => [
  101. 'type' => 'email',
  102. 'id' => $parameters[1],
  103. 'name' => $parameters[1],
  104. ],
  105. ];
  106. }
  107. throw new \InvalidArgumentException();
  108. }
  109. }