Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Use the usermountcache to get all users who have access to a file
  • Loading branch information
icewind1991 committed May 25, 2020
commit 044ffb742312b325c2f4d437b4dc3fbcd6b287e1
19 changes: 17 additions & 2 deletions lib/FilesHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
use OCA\Activity\Extension\Files;
use OCA\Activity\Extension\Files_Sharing;
use OCP\Activity\IManager;
use OCP\Files\Config\ICachedMountFileInfo;
use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node;
Expand Down Expand Up @@ -93,6 +96,8 @@ class FilesHooks {
protected $oldParentOwner;
/** @var string */
protected $oldParentId;
/** @var IUserMountCache */
protected $userMountCache;

/**
* Constructor
Expand All @@ -108,6 +113,7 @@ class FilesHooks {
* @param IURLGenerator $urlGenerator
* @param ILogger $logger
* @param CurrentUser $currentUser
* @param IUserMountCache $userMountCache
*/
public function __construct(IManager $manager,
Data $activityData,
Expand All @@ -119,7 +125,8 @@ public function __construct(IManager $manager,
IDBConnection $connection,
IURLGenerator $urlGenerator,
ILogger $logger,
CurrentUser $currentUser) {
CurrentUser $currentUser,
IUserMountCache $userMountCache) {
$this->manager = $manager;
$this->activityData = $activityData;
$this->userSettings = $userSettings;
Expand All @@ -131,6 +138,7 @@ public function __construct(IManager $manager,
$this->urlGenerator = $urlGenerator;
$this->logger = $logger;
$this->currentUser = $currentUser;
$this->userMountCache = $userMountCache;
}

/**
Expand Down Expand Up @@ -197,7 +205,14 @@ protected function addNotificationsForFileAction($filePath, $activityType, $subj

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

$affectedUsers = $accessList['users'];
$mountsForFile = $this->userMountCache->getMountsForFileId($fileId);
$affectedUserIds = array_map(function (ICachedMountInfo $mount) {
return $mount->getUser()->getUID();
}, $mountsForFile);
$affectedPaths = array_map(function (ICachedMountFileInfo $mount) {
return $mount->getPath();
}, $mountsForFile);
$affectedUsers = array_combine($affectedUserIds, $affectedPaths);
$filteredStreamUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'stream', $activityType);
$filteredEmailUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'email', $activityType);

Expand Down
57 changes: 48 additions & 9 deletions tests/FilesHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@

namespace OCA\Activity;

use OC\Files\Config\CachedMountFileInfo;
use OCA\Activity\Extension\Files;
use OCA\Activity\Extension\Files_Sharing;
use OCA\Activity\Tests\TestCase;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\ILogger;
Expand Down Expand Up @@ -61,6 +63,8 @@ class FilesHooksTest extends TestCase {
protected $shareHelper;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
protected $urlGenerator;
/** @var IUserMountCache|\PHPUnit_Framework_MockObject_MockObject */
protected $userMountCache;

protected function setUp(): void {
parent::setUp();
Expand All @@ -73,6 +77,7 @@ protected function setUp(): void {
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->shareHelper = $this->createMock(IShareHelper::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->userMountCache = $this->createMock(IUserMountCache::class);

$this->filesHooks = $this->getFilesHooks();
}
Expand Down Expand Up @@ -107,6 +112,7 @@ protected function getFilesHooks(array $mockedMethods = [], $user = 'user') {
$this->urlGenerator,
$logger,
$currentUser,
$this->userMountCache
])
->setMethods($mockedMethods)
->getMock();
Expand All @@ -123,7 +129,8 @@ protected function getFilesHooks(array $mockedMethods = [], $user = 'user') {
\OC::$server->getDatabaseConnection(),
$this->urlGenerator,
$logger,
$currentUser
$currentUser,
$this->userMountCache
);
}

Expand Down Expand Up @@ -236,8 +243,8 @@ public function dataAddNotificationsForFileAction() {
[
'user' => [
'subject' => 'restored_self',
'subject_params' => [[1337 => '/user/path']],
'path' => '/user/path',
'subject_params' => [[1337 => '/user/files/path']],
'path' => '/user/files/path',
'stream' => true,
'email' => 42,
],
Expand All @@ -251,8 +258,8 @@ public function dataAddNotificationsForFileAction() {
[
'user1' => [
'subject' => 'restored_by',
'subject_params' => [[1337 => '/user1/path'], 'user'],
'path' => '/user1/path',
'subject_params' => [[1337 => '/user1/files/path'], 'user'],
'path' => '/user1/files/path',
'stream' => true,
'email' => 0,
],
Expand Down Expand Up @@ -284,13 +291,45 @@ public function testAddNotificationsForFileAction($filterUsers, $addNotification
->willReturn([
'ownerPath' => '/owner/path',
'users' => [
'user' => '/user/path',
'user1' => '/user1/path',
'user2' => '/user2/path',
'user' => '/user/files/path',
'user1' => '/user1/files/path',
'user2' => '/user2/files/path',
],
'remotes' => [],
]);

$this->userMountCache->expects($this->once())
->method('getMountsForFileId')
->willReturn([
new CachedMountFileInfo(
$this->getUserMock('user'),
1,
1,
'/user/files/',
null,
'',
'path'
),
new CachedMountFileInfo(
$this->getUserMock('user1'),
1,
1,
'/user1/files/',
null,
'',
'path'
),
new CachedMountFileInfo(
$this->getUserMock('user2'),
1,
1,
'/user2/files/',
null,
'',
'path'
)
]);

$this->settings->expects($this->exactly(2))
->method('filterUsersBySetting')
->willReturnMap($filterUsers);
Expand Down Expand Up @@ -852,7 +891,7 @@ public function dataAddNotificationsForUser() {
['notAuthor', 'subject', ['parameter'], 42, 'path/subpath', 'path', true, false, true, Files::TYPE_SHARE_CREATED, false, false, 'files', false, true],
['notAuthor', 'subject', ['parameter'], 0, 'path/subpath', 'path', true, false, true, Files::TYPE_SHARE_CREATED, false, false, 'files', false, true],
['notAuthor', 'subject', ['parameter'], 0, 'path/subpath', 'path', true, false, true, Files::TYPE_SHARE_CREATED, false, false, 'files', false, true],
['notAuthor', 'subject', ['parameter'], 0, 'path/subpath','path/subpath', false, false, true, Files::TYPE_SHARE_CREATED, false, false, 'files', false, true],
['notAuthor', 'subject', ['parameter'], 0, 'path/subpath', 'path/subpath', false, false, true, Files::TYPE_SHARE_CREATED, false, false, 'files', false, true],
];
}

Expand Down