Merge pull request #6355 from nextcloud/6221-12

[stable12] Correctly format OCS response with favorites
This commit is contained in:
blizzz 2017-09-04 12:52:44 +02:00 committed by GitHub
commit ebadf2f21d
3 changed files with 51 additions and 20 deletions

View File

@ -32,6 +32,7 @@
namespace OCA\Files; namespace OCA\Files;
use OCP\Files\FileInfo; use OCP\Files\FileInfo;
use OCP\ITagManager;
/** /**
* Helper class for manipulating file information * Helper class for manipulating file information
@ -206,40 +207,39 @@ class Helper {
* *
* @param array $fileList * @param array $fileList
* @param string $fileIdentifier identifier attribute name for values in $fileList * @param string $fileIdentifier identifier attribute name for values in $fileList
* @param ITagManager $tagManager
* @return array file list populated with tags * @return array file list populated with tags
*/ */
public static function populateTags(array $fileList, $fileIdentifier = 'fileid') { public static function populateTags(array $fileList, $fileIdentifier = 'fileid', ITagManager $tagManager) {
$filesById = []; $ids = [];
foreach ($fileList as $fileData) { foreach ($fileList as $fileData) {
$filesById[$fileData[$fileIdentifier]] = $fileData; $ids[] = $fileData[$fileIdentifier];
} }
$tagger = \OC::$server->getTagManager()->load('files'); $tagger = $tagManager->load('files');
$tags = $tagger->getTagsForObjects(array_keys($filesById)); $tags = $tagger->getTagsForObjects($ids);
if (!is_array($tags)) { if (!is_array($tags)) {
throw new \UnexpectedValueException('$tags must be an array'); throw new \UnexpectedValueException('$tags must be an array');
} }
// Set empty tag array
foreach ($fileList as $key => $fileData) {
$fileList[$key]['tags'] = [];
}
if (!empty($tags)) { if (!empty($tags)) {
foreach ($tags as $fileId => $fileTags) { foreach ($tags as $fileId => $fileTags) {
$filesById[$fileId]['tags'] = $fileTags;
}
foreach ($filesById as $key => $fileWithTags) { foreach ($fileList as $key => $fileData) {
foreach($fileList as $key2 => $file){ if ($fileId !== $fileData[$fileIdentifier]) {
if( $file[$fileIdentifier] == $key){ continue;
$fileList[$key2] = $fileWithTags;
} }
$fileList[$key]['tags'] = $fileTags;
} }
} }
foreach ($fileList as $key => $file) {
if (!array_key_exists('tags', $file)) {
$fileList[$key]['tags'] = [];
}
}
} }
return $fileList; return $fileList;
} }

View File

@ -112,4 +112,35 @@ class HelperTest extends \Test\TestCase {
); );
} }
public function testPopulateTags() {
$tagManager = $this->createMock(\OCP\ITagManager::class);
$tagger = $this->createMock(\OCP\ITags::class);
$tagManager->method('load')
->with('files')
->willReturn($tagger);
$data = [
['id' => 10],
['id' => 22, 'foo' => 'bar'],
['id' => 42, 'x' => 'y'],
];
$tags = [
10 => ['tag3'],
42 => ['tag1', 'tag2'],
];
$tagger->method('getTagsForObjects')
->with([10, 22, 42])
->willReturn($tags);
$result = \OCA\Files\Helper::populateTags($data, 'id', $tagManager);
$this->assertSame([
['id' => 10, 'tags' => ['tag3']],
['id' => 22, 'foo' => 'bar', 'tags' => []],
['id' => 42, 'x' => 'y', 'tags' => ['tag1', 'tag2']],
], $result);
}
} }

View File

@ -512,7 +512,7 @@ class ShareAPIController extends OCSController {
} }
if ($includeTags) { if ($includeTags) {
$formatted = Helper::populateTags($formatted, 'file_source'); $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
} }
return new DataResponse($formatted); return new DataResponse($formatted);
@ -642,7 +642,7 @@ class ShareAPIController extends OCSController {
} }
if ($include_tags) { if ($include_tags) {
$formatted = Helper::populateTags($formatted, 'file_source'); $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
} }
return new DataResponse($formatted); return new DataResponse($formatted);