Skip to content

Commit af0abb7

Browse files
authored
Merge pull request #3068 from nextcloud/bug/mention-search-issues
fix: mention search issues
2 parents 13af71a + 609719e commit af0abb7

File tree

2 files changed

+123
-9
lines changed

2 files changed

+123
-9
lines changed

lib/Controller/UserApiController.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,18 @@
88
use OCP\Collaboration\Collaborators\ISearch;
99
use \OCP\IRequest;
1010
use OCP\IUserManager;
11-
use OCP\IUserSession;
1211
use OCP\Share\IShare;
1312

1413
class UserApiController extends ApiController {
1514
private ISearch $collaboratorSearch;
16-
private IUserSession $userSession;
1715
private IUserManager $userManager;
1816
private SessionService $sessionService;
1917

20-
public function __construct($appName, IRequest $request, SessionService $sessionService, ISearch $ISearch, IUserManager $userManager, IUserSession $userSession) {
18+
public function __construct($appName, IRequest $request, SessionService $sessionService, ISearch $ISearch, IUserManager $userManager) {
2119
parent::__construct($appName, $request);
2220

2321
$this->sessionService = $sessionService;
2422
$this->collaboratorSearch = $ISearch;
25-
$this->userSession = $userSession;
2623
$this->userManager = $userManager;
2724
}
2825

@@ -43,24 +40,25 @@ public function index(int $documentId, int $sessionId, string $sessionToken, str
4340
foreach ($sessions as $session) {
4441
$sessionUserId = $session['userId'];
4542
if ($sessionUserId !== null && !isset($users[$sessionUserId])) {
46-
$users[$sessionUserId] = $this->userManager->getDisplayName($sessionUserId);
43+
$displayName = $this->userManager->getDisplayName($sessionUserId);
44+
if (stripos($displayName, $filter) !== false) {
45+
$users[$sessionUserId] = $displayName;
46+
}
4747
}
4848
}
4949

5050
$currentSession = $this->sessionService->getSession($documentId, $sessionId, $sessionToken);
5151
if ($currentSession->getUserId() !== null) {
5252
// Add other users to the autocomplete list
5353
[$result] = $this->collaboratorSearch->search($filter, [IShare::TYPE_USER], false, $limit, 0);
54+
$userSearch = array_merge($result['users'], $result['exact']['users']);
5455

55-
foreach ($result['users'] as ['label' => $label, 'value' => $value]) {
56+
foreach ($userSearch as ['label' => $label, 'value' => $value]) {
5657
if (isset($value['shareWith'])) {
5758
$id = $value['shareWith'];
5859
$users[$id] = $label;
5960
}
6061
}
61-
62-
$user = $this->userSession->getUser();
63-
$users[$user->getUID()] = $user->getDisplayName();
6462
}
6563

6664
return new DataResponse($users);
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace OCA\Text\Controller;
4+
5+
use OCA\Text\Service\SessionService;
6+
use OCP\Collaboration\Collaborators\ISearch;
7+
use OCP\AppFramework\Http\DataResponse;
8+
use \OCP\IRequest;
9+
use OCP\IUserManager;
10+
use OCP\IUserSession;
11+
use OCA\Text\Db\Session;
12+
use Test\TestCase;
13+
14+
class UserApiControllerTest extends TestCase {
15+
/**
16+
* @var \PHPUnit\Framework\MockObject\MockObject
17+
*/
18+
private $collaboratorSearch;
19+
/**
20+
* @var \PHPUnit\Framework\MockObject\MockObject
21+
*/
22+
private $userSession;
23+
/**
24+
* @var \PHPUnit\Framework\MockObject\MockObject
25+
*/
26+
private $userManager;
27+
/**
28+
* @var \PHPUnit\Framework\MockObject\MockObject
29+
*/
30+
private $sessionService;
31+
/**
32+
* @var UserApiController
33+
*/
34+
private $userApiController;
35+
/**
36+
* @var IRequest|\PHPUnit\Framework\MockObject\MockObject
37+
*/
38+
private $request;
39+
40+
protected function setUp(): void {
41+
parent::setUp();
42+
$this->request = $this->createMock(IRequest::class);
43+
$this->collaboratorSearch = $this->createMock(ISearch::class);
44+
$this->userSession = $this->createMock(IUserSession::class);
45+
$this->userManager = $this->createMock(IUserManager::class);
46+
$this->sessionService = $this->createMock(SessionService::class);
47+
$this->userApiController = new UserApiController(
48+
'text',
49+
$this->request,
50+
$this->sessionService,
51+
$this->collaboratorSearch,
52+
$this->userManager,
53+
$this->userSession
54+
);
55+
}
56+
57+
/**
58+
* @dataProvider dataTestUsersIndex
59+
*/
60+
public function testUsersIndex(int $documentId, int $sessionId, string $sessionToken, string $filter) {
61+
$session = new Session();
62+
$session->setUserId('admin');
63+
$this->sessionService
64+
->expects($this->once())
65+
->method('isValidSession')->willReturn(true);
66+
$this->sessionService
67+
->expects($this->once())
68+
->method('getAllSessions')->willReturn([[
69+
'userId' => 'admin',
70+
'displayName' => 'admin',
71+
]]);
72+
$this->sessionService
73+
->expects($this->once())
74+
->method('getSession')->willReturn($session);
75+
$this->collaboratorSearch
76+
->expects($this->once())
77+
->method('search')->willReturn([
78+
[
79+
'exact' => [
80+
'users' => []
81+
],
82+
'users' => [
83+
[
84+
'label' => 'admin',
85+
'subline' => '',
86+
'icon' => 'icon-user',
87+
'value' => [
88+
'shareType' => 0,
89+
'shareWith' => 'admin'
90+
],
91+
'shareWithDisplayNameUnique' => 'admin',
92+
'status' => []
93+
],
94+
]
95+
]
96+
]);
97+
98+
$response = $this->userApiController->index(
99+
$documentId,
100+
$sessionId,
101+
$sessionToken,
102+
$filter
103+
);
104+
$this->assertInstanceOf(DataResponse::class, $response);
105+
$this->assertIsArray($response->getData());
106+
$this->assertSame(['admin' => 'admin'], $response->getData());
107+
}
108+
109+
public function dataTestUsersIndex() {
110+
return [
111+
[103, 44, 'token1', 'admin'],
112+
[103, 44, 'token2', 'admin'],
113+
[103, 44, 'token3', 'admin'],
114+
];
115+
}
116+
}

0 commit comments

Comments
 (0)