]> source.dussan.org Git - nextcloud-server.git/commitdiff
Avoid duplicate matches in wide and exact results 21408/head
authorJulius Härtl <jus@bitgrid.net>
Mon, 15 Jun 2020 07:29:30 +0000 (09:29 +0200)
committerJulius Härtl <jus@bitgrid.net>
Mon, 15 Jun 2020 07:34:57 +0000 (09:34 +0200)
Signed-off-by: Julius Härtl <jus@bitgrid.net>
lib/private/Collaboration/Collaborators/UserPlugin.php
tests/lib/Collaboration/Collaborators/UserPluginTest.php

index 4809585dca74af68d8905c9870aa66615e8a57b6..7c02e3c79ac0e963c95f0b934f4d1f99e2572496 100644 (file)
@@ -71,7 +71,6 @@ class UserPlugin implements ISearchPlugin {
        public function search($search, $limit, $offset, ISearchResult $searchResult) {
                $result = ['wide' => [], 'exact' => []];
                $users = [];
-               $autoCompleteUsers = [];
                $hasMoreResults = false;
 
                $userGroups = [];
@@ -91,28 +90,6 @@ class UserPlugin implements ISearchPlugin {
                        foreach ($usersTmp as $user) {
                                if ($user->isEnabled()) { // Don't keep deactivated users
                                        $users[$user->getUID()] = $user;
-
-                                       $addToWideResults = false;
-                                       if ($this->shareeEnumeration && !$this->shareeEnumerationInGroupOnly) {
-                                               $addToWideResults = true;
-                                       }
-
-                                       if ($this->shareeEnumerationInGroupOnly) {
-                                               $commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
-                                               if (!empty($commonGroups)) {
-                                                       $addToWideResults = true;
-                                               }
-                                       }
-
-                                       if ($addToWideResults) {
-                                               $autoCompleteUsers[] = [
-                                                       'label' => $user->getDisplayName(),
-                                                       'value' => [
-                                                               'shareType' => IShare::TYPE_USER,
-                                                               'shareWith' => (string)$user->getUID(),
-                                                       ],
-                                               ];
-                                       }
                                }
                        }
                }
@@ -145,13 +122,27 @@ class UserPlugin implements ISearchPlugin {
                                        ],
                                ];
                        } else {
-                               $result['wide'][] = [
-                                       'label' => $userDisplayName,
-                                       'value' => [
-                                               'shareType' => Share::SHARE_TYPE_USER,
-                                               'shareWith' => $uid,
-                                       ],
-                               ];
+                               $addToWideResults = false;
+                               if ($this->shareeEnumeration && !$this->shareeEnumerationInGroupOnly) {
+                                       $addToWideResults = true;
+                               }
+
+                               if ($this->shareeEnumerationInGroupOnly) {
+                                       $commonGroups = array_intersect($currentUserGroups, $this->groupManager->getUserGroupIds($user));
+                                       if (!empty($commonGroups)) {
+                                               $addToWideResults = true;
+                                       }
+                               }
+
+                               if ($addToWideResults) {
+                                       $result['wide'][] = [
+                                               'label' => $userDisplayName,
+                                               'value' => [
+                                                       'shareType' => IShare::TYPE_USER,
+                                                       'shareWith' => $uid,
+                                               ],
+                                       ];
+                               }
                        }
                }
 
@@ -183,10 +174,7 @@ class UserPlugin implements ISearchPlugin {
                        }
                }
 
-               // overwrite wide matches if they are limited
-               if (!$this->shareeEnumeration || $this->shareeEnumerationInGroupOnly) {
-                       $result['wide'] = $autoCompleteUsers;
-               }
+
 
                $type = new SearchResultType('users');
                $searchResult->addResultSet($type, $result['wide'], $result['exact']);
index 07bcce935299fe89fdac0d02ec71a031c40bca5d..0256c1eb157c7f5b03f3474b85710f4d298cd2bb 100644 (file)
@@ -522,7 +522,16 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test1', 'groups' => ['groupA']],
                                        ['uid' => 'test2', 'groups' => ['groupB']],
                                ],
-                               ['test1'],
+                               ['exact' => [], 'wide' => ['test1']],
+                       ],
+                       [
+                               'test1',
+                               ['groupA'],
+                               [
+                                       ['uid' => 'test1', 'groups' => ['groupA']],
+                                       ['uid' => 'test2', 'groups' => ['groupB']],
+                               ],
+                               ['exact' => ['test1'], 'wide' => []],
                        ],
                        [
                                'test',
@@ -531,7 +540,7 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test1', 'groups' => ['groupA']],
                                        ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
                                ],
-                               ['test1', 'test2'],
+                               ['exact' => [], 'wide' => ['test1', 'test2']],
                        ],
                        [
                                'test',
@@ -540,7 +549,7 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test1', 'groups' => ['groupA', 'groupC']],
                                        ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
                                ],
-                               ['test1', 'test2'],
+                               ['exact' => [], 'wide' => ['test1', 'test2']],
                        ],
                        [
                                'test',
@@ -549,7 +558,7 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test1', 'groups' => ['groupA', 'groupC']],
                                        ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
                                ],
-                               ['test1', 'test2'],
+                               ['exact' => [], 'wide' => ['test1', 'test2']],
                        ],
                        [
                                'test',
@@ -558,7 +567,7 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test1', 'groups' => ['groupA']],
                                        ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
                                ],
-                               [],
+                               ['exact' => [], 'wide' => []],
                        ],
                        [
                                'test',
@@ -567,7 +576,16 @@ class UserPluginTest extends TestCase {
                                        ['uid' => 'test1', 'groups' => []],
                                        ['uid' => 'test2', 'groups' => []],
                                ],
-                               [],
+                               ['exact' => [], 'wide' => []],
+                       ],
+                       [
+                               'test',
+                               ['groupC', 'groupB'],
+                               [
+                                       ['uid' => 'test1', 'groups' => []],
+                                       ['uid' => 'test2', 'groups' => []],
+                               ],
+                               ['exact' => [], 'wide' => []],
                        ],
                ];
        }
@@ -582,9 +600,12 @@ class UserPluginTest extends TestCase {
                        return $this->getUserMock($user['uid'], $user['uid']);
                }, $matchingUsers);
 
-               $mappedResult = array_map(function ($user) {
+               $mappedResultExact = array_map(function ($user) {
+                       return ['label' => $user, 'value' => ['shareType' => 0, 'shareWith' => $user]];
+               }, $result['exact']);
+               $mappedResultWide = array_map(function ($user) {
                        return ['label' => $user, 'value' => ['shareType' => 0, 'shareWith' => $user]];
-               }, $result);
+               }, $result['wide']);
 
                $this->userManager->expects($this->once())
                        ->method('searchDisplayName')
@@ -615,7 +636,7 @@ class UserPluginTest extends TestCase {
                $this->plugin->search($search, $this->limit, $this->offset, $this->searchResult);
                $result = $this->searchResult->asArray();
 
-               $this->assertEquals([], $result['exact']['users']);
-               $this->assertEquals($mappedResult, $result['users']);
+               $this->assertEquals($mappedResultExact, $result['exact']['users']);
+               $this->assertEquals($mappedResultWide, $result['users']);
        }
 }