Skip to content

Commit f44c1ed

Browse files
nickvergessenbackportbot[bot]
authored andcommitted
Add a config to use the cached mount points
Signed-off-by: Joas Schilling <[email protected]>
1 parent e0bca37 commit f44c1ed

File tree

2 files changed

+106
-61
lines changed

2 files changed

+106
-61
lines changed

lib/FilesHooks.php

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use OCP\Files\Mount\IMountPoint;
3838
use OCP\Files\Node;
3939
use OCP\Files\NotFoundException;
40+
use OCP\IConfig;
4041
use OCP\IDBConnection;
4142
use OCP\IGroup;
4243
use OCP\IGroupManager;
@@ -85,6 +86,10 @@ class FilesHooks {
8586

8687
/** @var CurrentUser */
8788
protected $currentUser;
89+
/** @var IUserMountCache */
90+
protected $userMountCache;
91+
/** @var IConfig */
92+
protected $config;
8893

8994
/** @var string|bool */
9095
protected $moveCase = false;
@@ -96,25 +101,7 @@ class FilesHooks {
96101
protected $oldParentOwner;
97102
/** @var string */
98103
protected $oldParentId;
99-
/** @var IUserMountCache */
100-
protected $userMountCache;
101104

102-
/**
103-
* Constructor
104-
*
105-
* @param IManager $manager
106-
* @param Data $activityData
107-
* @param UserSettings $userSettings
108-
* @param IGroupManager $groupManager
109-
* @param View $view
110-
* @param IRootFolder $rootFolder
111-
* @param IShareHelper $shareHelper
112-
* @param IDBConnection $connection
113-
* @param IURLGenerator $urlGenerator
114-
* @param ILogger $logger
115-
* @param CurrentUser $currentUser
116-
* @param IUserMountCache $userMountCache
117-
*/
118105
public function __construct(IManager $manager,
119106
Data $activityData,
120107
UserSettings $userSettings,
@@ -126,7 +113,8 @@ public function __construct(IManager $manager,
126113
IURLGenerator $urlGenerator,
127114
ILogger $logger,
128115
CurrentUser $currentUser,
129-
IUserMountCache $userMountCache) {
116+
IUserMountCache $userMountCache,
117+
IConfig $config) {
130118
$this->manager = $manager;
131119
$this->activityData = $activityData;
132120
$this->userSettings = $userSettings;
@@ -139,6 +127,7 @@ public function __construct(IManager $manager,
139127
$this->logger = $logger;
140128
$this->currentUser = $currentUser;
141129
$this->userMountCache = $userMountCache;
130+
$this->config = $config;
142131
}
143132

144133
/**
@@ -205,14 +194,19 @@ protected function addNotificationsForFileAction($filePath, $activityType, $subj
205194

206195
$this->generateRemoteActivity($accessList['remotes'], $activityType, time(), $this->currentUser->getCloudId(), $accessList['ownerPath']);
207196

208-
$mountsForFile = $this->userMountCache->getMountsForFileId($fileId);
209-
$affectedUserIds = array_map(function (ICachedMountInfo $mount) {
210-
return $mount->getUser()->getUID();
211-
}, $mountsForFile);
212-
$affectedPaths = array_map(function (ICachedMountFileInfo $mount) {
213-
return $this->getVisiblePath($mount->getPath());
214-
}, $mountsForFile);
215-
$affectedUsers = array_combine($affectedUserIds, $affectedPaths);
197+
if ($this->config->getSystemValueBool('activity_use_cached_mountpoints', false)) {
198+
$mountsForFile = $this->userMountCache->getMountsForFileId($fileId);
199+
$affectedUserIds = array_map(function (ICachedMountInfo $mount) {
200+
return $mount->getUser()->getUID();
201+
}, $mountsForFile);
202+
$affectedPaths = array_map(function (ICachedMountFileInfo $mount) {
203+
return $this->getVisiblePath($mount->getPath());
204+
}, $mountsForFile);
205+
$affectedUsers = array_combine($affectedUserIds, $affectedPaths);
206+
} else {
207+
$affectedUsers = $accessList['users'];
208+
}
209+
216210
$filteredStreamUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'stream', $activityType);
217211
$filteredEmailUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'email', $activityType);
218212

tests/FilesHooksTest.php

Lines changed: 85 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCP\Files\Config\IUserMountCache;
3030
use OCP\Files\IRootFolder;
3131
use OCP\Files\NotFoundException;
32+
use OCP\IConfig;
3233
use OCP\ILogger;
3334
use OCP\Share;
3435
use OCP\Share\IShareHelper;
@@ -63,8 +64,10 @@ class FilesHooksTest extends TestCase {
6364
protected $shareHelper;
6465
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
6566
protected $urlGenerator;
66-
/** @var IUserMountCache|\PHPUnit_Framework_MockObject_MockObject */
67+
/** @var IUserMountCache|MockObject */
6768
protected $userMountCache;
69+
/** @var IConfig|MockObject */
70+
protected $config;
6871

6972
protected function setUp(): void {
7073
parent::setUp();
@@ -78,6 +81,7 @@ protected function setUp(): void {
7881
$this->shareHelper = $this->createMock(IShareHelper::class);
7982
$this->urlGenerator = $this->createMock(IURLGenerator::class);
8083
$this->userMountCache = $this->createMock(IUserMountCache::class);
84+
$this->config = $this->createMock(IConfig::class);
8185

8286
$this->filesHooks = $this->getFilesHooks();
8387
}
@@ -112,7 +116,8 @@ protected function getFilesHooks(array $mockedMethods = [], $user = 'user') {
112116
$this->urlGenerator,
113117
$logger,
114118
$currentUser,
115-
$this->userMountCache
119+
$this->userMountCache,
120+
$this->config,
116121
])
117122
->setMethods($mockedMethods)
118123
->getMock();
@@ -130,7 +135,8 @@ protected function getFilesHooks(array $mockedMethods = [], $user = 'user') {
130135
$this->urlGenerator,
131136
$logger,
132137
$currentUser,
133-
$this->userMountCache
138+
$this->userMountCache,
139+
$this->config
134140
);
135141
}
136142

@@ -240,6 +246,39 @@ public function dataAddNotificationsForFileAction() {
240246
[['user', 'user1', 'user2'], 'stream', Files::TYPE_SHARE_RESTORED, ['user' => true]],
241247
[['user', 'user1', 'user2'], 'email', Files::TYPE_SHARE_RESTORED, ['user' => 42]],
242248
],
249+
'mountcache_used' => false,
250+
[
251+
'user' => [
252+
'subject' => 'restored_self',
253+
'subject_params' => [[1337 => '/user/files/path']],
254+
'path' => '/user/files/path',
255+
'stream' => true,
256+
'email' => 42,
257+
],
258+
],
259+
],
260+
[
261+
[
262+
[['user', 'user1', 'user2'], 'stream', Files::TYPE_SHARE_RESTORED, ['user1' => true]],
263+
[['user', 'user1', 'user2'], 'email', Files::TYPE_SHARE_RESTORED, []],
264+
],
265+
'mountcache_used' => false,
266+
[
267+
'user1' => [
268+
'subject' => 'restored_by',
269+
'subject_params' => [[1337 => '/user1/files/path'], 'user'],
270+
'path' => '/user1/files/path',
271+
'stream' => true,
272+
'email' => 0,
273+
],
274+
],
275+
],
276+
[
277+
[
278+
[['user', 'user1', 'user2'], 'stream', Files::TYPE_SHARE_RESTORED, ['user' => true]],
279+
[['user', 'user1', 'user2'], 'email', Files::TYPE_SHARE_RESTORED, ['user' => 42]],
280+
],
281+
'mountcache_used' => true,
243282
[
244283
'user' => [
245284
'subject' => 'restored_self',
@@ -255,6 +294,7 @@ public function dataAddNotificationsForFileAction() {
255294
[['user', 'user1', 'user2'], 'stream', Files::TYPE_SHARE_RESTORED, ['user1' => true]],
256295
[['user', 'user1', 'user2'], 'email', Files::TYPE_SHARE_RESTORED, []],
257296
],
297+
'mountcache_used' => true,
258298
[
259299
'user1' => [
260300
'subject' => 'restored_by',
@@ -272,6 +312,7 @@ public function dataAddNotificationsForFileAction() {
272312
* @dataProvider dataAddNotificationsForFileAction
273313
*
274314
* @param array $filterUsers
315+
* @param bool $mountCacheUsed
275316
* @param array $addNotifications
276317
*/
277318
public function testAddNotificationsForFileAction($filterUsers, $addNotifications) {
@@ -298,37 +339,47 @@ public function testAddNotificationsForFileAction($filterUsers, $addNotification
298339
'remotes' => [],
299340
]);
300341

301-
$this->userMountCache->expects($this->once())
302-
->method('getMountsForFileId')
303-
->willReturn([
304-
new CachedMountFileInfo(
305-
$this->getUserMock('user'),
306-
1,
307-
1,
308-
'/user/files/',
309-
null,
310-
'',
311-
'path'
312-
),
313-
new CachedMountFileInfo(
314-
$this->getUserMock('user1'),
315-
1,
316-
1,
317-
'/user1/files/',
318-
null,
319-
'',
320-
'path'
321-
),
322-
new CachedMountFileInfo(
323-
$this->getUserMock('user2'),
324-
1,
325-
1,
326-
'/user2/files/',
327-
null,
328-
'',
329-
'path'
330-
)
331-
]);
342+
$this->config->expects($this->once())
343+
->method('getSystemValueBool')
344+
->with('activity_use_cached_mountpoints', false)
345+
->willReturn($mountCacheUsed);
346+
347+
if ($mountCacheUsed) {
348+
$this->userMountCache->expects($this->once())
349+
->method('getMountsForFileId')
350+
->willReturn([
351+
new CachedMountFileInfo(
352+
$this->getUserMock('user'),
353+
1,
354+
1,
355+
'/user/files/',
356+
null,
357+
'',
358+
'path'
359+
),
360+
new CachedMountFileInfo(
361+
$this->getUserMock('user1'),
362+
1,
363+
1,
364+
'/user1/files/',
365+
null,
366+
'',
367+
'path'
368+
),
369+
new CachedMountFileInfo(
370+
$this->getUserMock('user2'),
371+
1,
372+
1,
373+
'/user2/files/',
374+
null,
375+
'',
376+
'path'
377+
)
378+
]);
379+
} else {
380+
$this->userMountCache->expects($this->never())
381+
->method('getMountsForFileId');
382+
}
332383

333384
$this->settings->expects($this->exactly(2))
334385
->method('filterUsersBySetting')

0 commit comments

Comments
 (0)