RemoteShares.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files_Sharing\Activity\Providers;
  25. use OCP\Activity\IEvent;
  26. use OCP\Activity\IManager;
  27. use OCP\Federation\ICloudIdManager;
  28. use OCP\IURLGenerator;
  29. use OCP\IUserManager;
  30. use OCP\L10N\IFactory;
  31. class RemoteShares extends Base {
  32. protected $cloudIdManager;
  33. const SUBJECT_REMOTE_SHARE_ACCEPTED = 'remote_share_accepted';
  34. const SUBJECT_REMOTE_SHARE_DECLINED = 'remote_share_declined';
  35. const SUBJECT_REMOTE_SHARE_RECEIVED = 'remote_share_received';
  36. const SUBJECT_REMOTE_SHARE_UNSHARED = 'remote_share_unshared';
  37. /**
  38. * @param IFactory $languageFactory
  39. * @param IURLGenerator $url
  40. * @param IManager $activityManager
  41. * @param IUserManager $userManager
  42. * @param ICloudIdManager $cloudIdManager
  43. */
  44. public function __construct(IFactory $languageFactory,
  45. IURLGenerator $url,
  46. IManager $activityManager,
  47. IUserManager $userManager,
  48. ICloudIdManager $cloudIdManager
  49. ) {
  50. parent::__construct($languageFactory, $url, $activityManager, $userManager);
  51. $this->cloudIdManager = $cloudIdManager;
  52. }
  53. /**
  54. * @param IEvent $event
  55. * @return IEvent
  56. * @throws \InvalidArgumentException
  57. * @since 11.0.0
  58. */
  59. public function parseShortVersion(IEvent $event) {
  60. $parsedParameters = $this->getParsedParameters($event);
  61. if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_ACCEPTED) {
  62. $subject = $this->l->t('{user} accepted the remote share');
  63. } else if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_DECLINED) {
  64. $subject = $this->l->t('{user} declined the remote share');
  65. } else {
  66. throw new \InvalidArgumentException();
  67. }
  68. if ($this->activityManager->getRequirePNG()) {
  69. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  70. } else {
  71. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  72. }
  73. $this->setSubjects($event, $subject, $parsedParameters);
  74. return $event;
  75. }
  76. /**
  77. * @param IEvent $event
  78. * @return IEvent
  79. * @throws \InvalidArgumentException
  80. * @since 11.0.0
  81. */
  82. public function parseLongVersion(IEvent $event) {
  83. $parsedParameters = $this->getParsedParameters($event);
  84. if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_RECEIVED) {
  85. $subject = $this->l->t('You received a new remote share {file} from {user}');
  86. } else if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_ACCEPTED) {
  87. $subject = $this->l->t('{user} accepted the remote share of {file}');
  88. } else if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_DECLINED) {
  89. $subject = $this->l->t('{user} declined the remote share of {file}');
  90. } else if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_UNSHARED) {
  91. $subject = $this->l->t('{user} unshared {file} from you');
  92. } else {
  93. throw new \InvalidArgumentException();
  94. }
  95. if ($this->activityManager->getRequirePNG()) {
  96. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  97. } else {
  98. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  99. }
  100. $this->setSubjects($event, $subject, $parsedParameters);
  101. return $event;
  102. }
  103. protected function getParsedParameters(IEvent $event) {
  104. $subject = $event->getSubject();
  105. $parameters = $event->getSubjectParameters();
  106. switch ($subject) {
  107. case self::SUBJECT_REMOTE_SHARE_RECEIVED:
  108. case self::SUBJECT_REMOTE_SHARE_UNSHARED:
  109. return [
  110. 'file' => [
  111. 'type' => 'pending-federated-share',
  112. 'id' => $parameters[1],
  113. 'name' => $parameters[1],
  114. ],
  115. 'user' => $this->getFederatedUser($parameters[0]),
  116. ];
  117. case self::SUBJECT_REMOTE_SHARE_ACCEPTED:
  118. case self::SUBJECT_REMOTE_SHARE_DECLINED:
  119. $fileParameter = $parameters[1];
  120. if (!is_array($fileParameter)) {
  121. $fileParameter = [$event->getObjectId() => $event->getObjectName()];
  122. }
  123. return [
  124. 'file' => $this->getFile($fileParameter),
  125. 'user' => $this->getFederatedUser($parameters[0]),
  126. ];
  127. }
  128. throw new \InvalidArgumentException();
  129. }
  130. /**
  131. * @param string $cloudId
  132. * @return array
  133. */
  134. protected function getFederatedUser($cloudId) {
  135. $remoteUser = $this->cloudIdManager->resolveCloudId($cloudId);
  136. return [
  137. 'type' => 'user',
  138. 'id' => $remoteUser->getUser(),
  139. 'name' => $cloudId,// Todo display name from contacts
  140. 'server' => $remoteUser->getRemote(),
  141. ];
  142. }
  143. }