Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix OCS Share API response for requests contain "include_tags" parameter #37088

Merged
merged 1 commit into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 13 additions & 27 deletions apps/files/lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,42 +207,28 @@ public static function getFiles($dir, $sortAttribute = 'name', $sortDescending =
/**
* Populate the result set with file tags
*
* @param array $fileList
* @param string $fileIdentifier identifier attribute name for values in $fileList
* @return array file list populated with tags
* @param array $shareList
* @return array share list populated with tags
*/
public static function populateTags(array $fileList, $fileIdentifier = 'fileid') {
$filesById = [];
foreach ($fileList as $fileData) {
$filesById[$fileData[$fileIdentifier]] = $fileData;
public static function populateTagsForShares($shareList) {
$fileIdList = [];
foreach ($shareList as $share) {
$fileIdList[] = $share['file_source'];
}
$fileIdList = \array_unique($fileIdList);
$tagger = \OC::$server->getTagManager()->load('files');
$tags = $tagger->getTagsForObjects(\array_keys($filesById));

$tags = $tagger->getTagsForObjects($fileIdList);
if (!\is_array($tags)) {
throw new \UnexpectedValueException('$tags must be an array');
}

if (!empty($tags)) {
foreach ($tags as $fileId => $fileTags) {
$filesById[$fileId]['tags'] = $fileTags;
}

foreach ($filesById as $key => $fileWithTags) {
foreach ($fileList as $key2 => $file) {
if ($file[$fileIdentifier] == $key) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember the reason for such complex code back then :-/

$fileList[$key2] = $fileWithTags;
}
}
}

foreach ($fileList as $key => $file) {
if (!\array_key_exists('tags', $file)) {
$fileList[$key]['tags'] = [];
}
foreach ($shareList as $key => $share) {
$shareList[$key]['tags'] = [];
if (\array_key_exists($share['file_source'], $tags)) {
$shareList[$key]['tags'] = $tags[$share['file_source']];
}
}
return $fileList;
return $shareList;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions apps/files_sharing/lib/Controller/Share20OcsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ private function getSharedWithMe($node = null, $includeTags, $stateFilter = 0) {
}

if ($includeTags) {
$formatted = \OCA\Files\Helper::populateTags($formatted, 'file_source');
$formatted = \OCA\Files\Helper::populateTagsForShares($formatted);
}

return new Result($formatted);
Expand Down Expand Up @@ -716,7 +716,7 @@ public function getShares() {
}

if ($includeTags) {
$formatted = \OCA\Files\Helper::populateTags($formatted, 'file_source');
$formatted = \OCA\Files\Helper::populateTagsForShares($formatted);
}

if ($path !== null) {
Expand Down
13 changes: 11 additions & 2 deletions apps/files_sharing/tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,15 @@ public function testGetAllShares() {

$share = $this->shareManager->createShare($share);

$request = $this->createRequest([]);
$request = $this->createRequest(['include_tags' => true]);
$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER1);
$result = $ocs->getShares();

$this->assertTrue($result->succeeded());
$this->assertCount(1, $result->getData());
foreach ($result->getData() as $shareWithTags) {
$this->assertArrayHasKey('tags', $shareWithTags);
}

$this->shareManager->deleteShare($share);
}
Expand All @@ -469,12 +472,18 @@ public function testGetAllSharesWithMe() {
->setPermissions(31);
$share2 = $this->shareManager->createShare($share2);

$request = $this->createRequest(['shared_with_me' => 'true']);
$request = $this->createRequest([
'shared_with_me' => 'true',
'include_tags' => true
]);
$ocs = $this->createOCS($request, self::TEST_FILES_SHARING_API_USER2);
$result = $ocs->getShares();

$this->assertTrue($result->succeeded());
$this->assertCount(2, $result->getData());
foreach ($result->getData() as $shareWithTags) {
$this->assertArrayHasKey('tags', $shareWithTags);
}

$this->shareManager->deleteShare($share1);
$this->shareManager->deleteShare($share2);
Expand Down
8 changes: 8 additions & 0 deletions changelog/unreleased/37088
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Bugfix: fix OCS Share API response for requests contain "include_tags" parameter

Sending "include_tags" request parameter for OCS Share API was led to duplicated share entries in API response.
This bug has been fixed by using share_id instead of file_id when populating tags.
Also, the tag generation helper method simplified by customizing it for only shares.

https://github.com/owncloud/core/issues/37084
https://github.com/owncloud/core/pull/37088