RemoteShares.php 4.1 KB

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