You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Listener.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  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, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Comments\Activity;
  26. use OCP\Activity\IManager;
  27. use OCP\App\IAppManager;
  28. use OCP\Comments\CommentsEvent;
  29. use OCP\Files\Config\IMountProviderCollection;
  30. use OCP\Files\IRootFolder;
  31. use OCP\Files\Node;
  32. use OCP\IUser;
  33. use OCP\IUserSession;
  34. use OCP\Share\IShareHelper;
  35. class Listener {
  36. /** @var IManager */
  37. protected $activityManager;
  38. /** @var IUserSession */
  39. protected $session;
  40. /** @var \OCP\App\IAppManager */
  41. protected $appManager;
  42. /** @var \OCP\Files\Config\IMountProviderCollection */
  43. protected $mountCollection;
  44. /** @var \OCP\Files\IRootFolder */
  45. protected $rootFolder;
  46. /** @var IShareHelper */
  47. protected $shareHelper;
  48. /**
  49. * Listener constructor.
  50. *
  51. * @param IManager $activityManager
  52. * @param IUserSession $session
  53. * @param IAppManager $appManager
  54. * @param IMountProviderCollection $mountCollection
  55. * @param IRootFolder $rootFolder
  56. * @param IShareHelper $shareHelper
  57. */
  58. public function __construct(IManager $activityManager,
  59. IUserSession $session,
  60. IAppManager $appManager,
  61. IMountProviderCollection $mountCollection,
  62. IRootFolder $rootFolder,
  63. IShareHelper $shareHelper) {
  64. $this->activityManager = $activityManager;
  65. $this->session = $session;
  66. $this->appManager = $appManager;
  67. $this->mountCollection = $mountCollection;
  68. $this->rootFolder = $rootFolder;
  69. $this->shareHelper = $shareHelper;
  70. }
  71. /**
  72. * @param CommentsEvent $event
  73. */
  74. public function commentEvent(CommentsEvent $event) {
  75. if ($event->getComment()->getObjectType() !== 'files'
  76. || $event->getEvent() !== CommentsEvent::EVENT_ADD
  77. || !$this->appManager->isInstalled('activity')) {
  78. // Comment not for file, not adding a comment or no activity-app enabled (save the energy)
  79. return;
  80. }
  81. // Get all mount point owners
  82. $cache = $this->mountCollection->getMountCache();
  83. $mounts = $cache->getMountsForFileId((int)$event->getComment()->getObjectId());
  84. if (empty($mounts)) {
  85. return;
  86. }
  87. $users = [];
  88. foreach ($mounts as $mount) {
  89. $owner = $mount->getUser()->getUID();
  90. $ownerFolder = $this->rootFolder->getUserFolder($owner);
  91. $nodes = $ownerFolder->getById((int)$event->getComment()->getObjectId());
  92. if (!empty($nodes)) {
  93. /** @var Node $node */
  94. $node = array_shift($nodes);
  95. $al = $this->shareHelper->getPathsForAccessList($node);
  96. $users += $al['users'];
  97. }
  98. }
  99. $actor = $this->session->getUser();
  100. if ($actor instanceof IUser) {
  101. $actor = $actor->getUID();
  102. } else {
  103. $actor = '';
  104. }
  105. $activity = $this->activityManager->generateEvent();
  106. $activity->setApp('comments')
  107. ->setType('comments')
  108. ->setAuthor($actor)
  109. ->setObject($event->getComment()->getObjectType(), (int) $event->getComment()->getObjectId())
  110. ->setMessage('add_comment_message', [
  111. 'commentId' => $event->getComment()->getId(),
  112. ]);
  113. foreach ($users as $user => $path) {
  114. // numerical user ids end up as integers from array keys, but string
  115. // is required
  116. $activity->setAffectedUser((string)$user);
  117. $activity->setSubject('add_comment_subject', [
  118. 'actor' => $actor,
  119. 'fileId' => (int) $event->getComment()->getObjectId(),
  120. 'filePath' => trim($path, '/'),
  121. ]);
  122. $this->activityManager->publish($activity);
  123. }
  124. }
  125. }