RemoteShares.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Robin Appelman <robin@icewind.nl>
  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\Files_Sharing\Activity\Providers;
  26. use OCP\Activity\IEvent;
  27. use OCP\Activity\IEventMerger;
  28. use OCP\Activity\IManager;
  29. use OCP\Contacts\IManager as IContactsManager;
  30. use OCP\Federation\ICloudIdManager;
  31. use OCP\IURLGenerator;
  32. use OCP\IUserManager;
  33. use OCP\L10N\IFactory;
  34. class RemoteShares extends Base {
  35. public const SUBJECT_REMOTE_SHARE_ACCEPTED = 'remote_share_accepted';
  36. public const SUBJECT_REMOTE_SHARE_DECLINED = 'remote_share_declined';
  37. public const SUBJECT_REMOTE_SHARE_RECEIVED = 'remote_share_received';
  38. public const SUBJECT_REMOTE_SHARE_UNSHARED = 'remote_share_unshared';
  39. public function __construct(IFactory $languageFactory,
  40. IURLGenerator $url,
  41. IManager $activityManager,
  42. IUserManager $userManager,
  43. ICloudIdManager $cloudIdManager,
  44. IContactsManager $contactsManager,
  45. IEventMerger $eventMerger) {
  46. parent::__construct($languageFactory, $url, $activityManager, $userManager, $cloudIdManager, $contactsManager, $eventMerger);
  47. }
  48. /**
  49. * @param IEvent $event
  50. * @return IEvent
  51. * @throws \InvalidArgumentException
  52. * @since 11.0.0
  53. */
  54. public function parseShortVersion(IEvent $event) {
  55. $parsedParameters = $this->getParsedParameters($event);
  56. if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_ACCEPTED) {
  57. $subject = $this->l->t('{user} accepted the remote share');
  58. } elseif ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_DECLINED) {
  59. $subject = $this->l->t('{user} declined the remote share');
  60. } else {
  61. throw new \InvalidArgumentException();
  62. }
  63. if ($this->activityManager->getRequirePNG()) {
  64. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  65. } else {
  66. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  67. }
  68. $this->setSubjects($event, $subject, $parsedParameters);
  69. return $event;
  70. }
  71. /**
  72. * @param IEvent $event
  73. * @param IEvent|null $previousEvent
  74. * @return IEvent
  75. * @throws \InvalidArgumentException
  76. * @since 11.0.0
  77. */
  78. public function parseLongVersion(IEvent $event, IEvent $previousEvent = null) {
  79. $parsedParameters = $this->getParsedParameters($event);
  80. if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_RECEIVED) {
  81. $subject = $this->l->t('You received a new remote share {file} from {user}');
  82. } elseif ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_ACCEPTED) {
  83. $subject = $this->l->t('{user} accepted the remote share of {file}');
  84. } elseif ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_DECLINED) {
  85. $subject = $this->l->t('{user} declined the remote share of {file}');
  86. } elseif ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_UNSHARED) {
  87. $subject = $this->l->t('{user} unshared {file} from you');
  88. } else {
  89. throw new \InvalidArgumentException();
  90. }
  91. if ($this->activityManager->getRequirePNG()) {
  92. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  93. } else {
  94. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  95. }
  96. $this->setSubjects($event, $subject, $parsedParameters);
  97. return $event;
  98. }
  99. protected function getParsedParameters(IEvent $event) {
  100. $subject = $event->getSubject();
  101. $parameters = $event->getSubjectParameters();
  102. switch ($subject) {
  103. case self::SUBJECT_REMOTE_SHARE_RECEIVED:
  104. case self::SUBJECT_REMOTE_SHARE_UNSHARED:
  105. $displayName = (count($parameters) > 2) ? $parameters[2] : '';
  106. return [
  107. 'file' => [
  108. 'type' => 'pending-federated-share',
  109. 'id' => $parameters[1],
  110. 'name' => $parameters[1],
  111. ],
  112. 'user' => $this->getUser($parameters[0], $displayName)
  113. ];
  114. case self::SUBJECT_REMOTE_SHARE_ACCEPTED:
  115. case self::SUBJECT_REMOTE_SHARE_DECLINED:
  116. $fileParameter = $parameters[1];
  117. if (!is_array($fileParameter)) {
  118. $fileParameter = [$event->getObjectId() => $event->getObjectName()];
  119. }
  120. return [
  121. 'file' => $this->getFile($fileParameter),
  122. 'user' => $this->getUser($parameters[0]),
  123. ];
  124. }
  125. throw new \InvalidArgumentException();
  126. }
  127. }