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.

recipientpropagator.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Robin Appelman <icewind@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\Files_Sharing\Propagation;
  23. use OC\Files\Cache\ChangePropagator;
  24. use OC\Files\View;
  25. use OC\Share\Share;
  26. /**
  27. * Propagate etags for share recipients
  28. */
  29. class RecipientPropagator {
  30. /**
  31. * @var string
  32. */
  33. protected $userId;
  34. /**
  35. * @var \OC\Files\Cache\ChangePropagator
  36. */
  37. protected $changePropagator;
  38. /**
  39. * @var \OCP\IConfig
  40. */
  41. protected $config;
  42. /**
  43. * @param string $userId current user, must match the propagator's
  44. * user
  45. * @param \OC\Files\Cache\ChangePropagator $changePropagator change propagator
  46. * initialized with a view for $user
  47. * @param \OCP\IConfig $config
  48. */
  49. public function __construct($userId, $changePropagator, $config) {
  50. $this->userId = $userId;
  51. $this->changePropagator = $changePropagator;
  52. $this->config = $config;
  53. }
  54. /**
  55. * Propagate the etag changes for all shares marked as dirty and mark the shares as clean
  56. *
  57. * @param array $shares the shares for the users
  58. * @param int $time
  59. */
  60. public function propagateDirtyMountPoints(array $shares, $time = null) {
  61. if ($time === null) {
  62. $time = microtime(true);
  63. }
  64. $dirtyShares = $this->getDirtyShares($shares);
  65. foreach ($dirtyShares as $share) {
  66. $this->changePropagator->addChange($share['file_target']);
  67. }
  68. if (count($dirtyShares)) {
  69. $this->config->setUserValue($this->userId, 'files_sharing', 'last_propagate', $time);
  70. $this->changePropagator->propagateChanges(floor($time));
  71. }
  72. }
  73. /**
  74. * Get all shares we need to update the etag for
  75. *
  76. * @param array $shares the shares for the users
  77. * @return string[]
  78. */
  79. protected function getDirtyShares($shares) {
  80. $dirty = [];
  81. $userTime = $this->config->getUserValue($this->userId, 'files_sharing', 'last_propagate', 0);
  82. foreach ($shares as $share) {
  83. $updateTime = $this->config->getAppValue('files_sharing', $share['id'], 0);
  84. if ($updateTime >= $userTime) {
  85. $dirty[] = $share;
  86. }
  87. }
  88. return $dirty;
  89. }
  90. /**
  91. * @param array $share
  92. * @param int $time
  93. */
  94. public function markDirty($share, $time = null) {
  95. if ($time === null) {
  96. $time = microtime(true);
  97. }
  98. $this->config->setAppValue('files_sharing', $share['id'], $time);
  99. }
  100. /**
  101. * Listen on the propagator for updates made to shares owned by a user
  102. *
  103. * @param \OC\Files\Cache\ChangePropagator $propagator
  104. * @param string $owner
  105. */
  106. public function attachToPropagator(ChangePropagator $propagator, $owner) {
  107. $propagator->listen('\OC\Files', 'propagate', function ($path, $entry) use ($owner) {
  108. $shares = Share::getAllSharesForFileId($entry['fileid']);
  109. foreach ($shares as $share) {
  110. // propagate down the share tree
  111. $this->markDirty($share, microtime(true));
  112. // propagate up the share tree
  113. $user = $share['uid_owner'];
  114. $view = new View('/' . $user . '/files');
  115. $path = $view->getPath($share['file_source']);
  116. $watcher = new ChangeWatcher($view);
  117. $watcher->writeHook(['path' => $path]);
  118. }
  119. });
  120. }
  121. }