Browse Source

Sort mentions with the searched start first

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/1964/head
Joas Schilling 6 years ago
parent
commit
d166ea85a3
No known key found for this signature in database GPG Key ID: 7076EA9751AACDDA
  1. 15
      lib/Chat/AutoComplete/Sorter.php
  2. 1
      lib/Controller/ChatController.php
  3. 24
      tests/php/Chat/AutoComplete/SorterTest.php

15
lib/Chat/AutoComplete/Sorter.php

@ -66,7 +66,20 @@ class Sorter implements ISorter {
return $suggestion['value']['shareWith'];
}, $byType));
usort($byType, function ($a, $b) use ($lastComments) {
$search = $context['search'];
usort($byType, function ($a, $b) use ($lastComments, $search) {
if ($search) {
// If the user searched for "Dani" we make sure "Daniel" comes before "Madani"
if (stripos($a['label'], $search) === 0) {
if (stripos($b['label'], $search) !== 0) {
return -1;
}
} else if (stripos($b['label'], $search) === 0) {
return 1;
}
}
if (!isset($lastComments[$b['value']['shareWith']])) {
return -1;
}

1
lib/Controller/ChatController.php

@ -298,6 +298,7 @@ class ChatController extends AEnvironmentAwareController {
$this->autoCompleteManager->runSorters(['talk_chat_participants'], $results, [
'itemType' => 'chat',
'itemId' => (string) $this->room->getId(),
'search' => $search,
]);
$results = $this->prepareResultArray($results);

24
tests/php/Chat/AutoComplete/SorterTest.php

@ -53,6 +53,14 @@ class SorterTest extends \Test\TestCase {
],
];
protected $user3 = [
'label' => 'ttle Sea',
'value' => [
'shareType' => 'user',
'shareWith' => 'ttle_sea',
],
];
public function setUp() {
parent::setUp();
@ -66,22 +74,25 @@ class SorterTest extends \Test\TestCase {
public function dataSort(): array {
return [
'no user posted' => [['users' => [$this->user1, $this->user2]], [], ['users' => [$this->user1, $this->user2]]],
'second user posted' => [['users' => [$this->user1, $this->user2]], ['new_york' => new \DateTime('2000-01-01')], ['users' => [$this->user2, $this->user1]]],
'second user posted later' => [['users' => [$this->user1, $this->user2]], ['seattle' => new \DateTime('2017-01-01'), 'new_york' => new \DateTime('2018-01-01')], ['users' => [$this->user2, $this->user1]]],
'second user posted earlier' => [['users' => [$this->user1, $this->user2]], ['seattle' => new \DateTime('2018-01-01'), 'new_york' => new \DateTime('2017-01-01')], ['users' => [$this->user1, $this->user2]]],
'no users' => [['groups' => [$this->user1, $this->user2]], null, ['groups' => [$this->user1, $this->user2]]],
'no user posted' => ['', ['users' => [$this->user1, $this->user2]], [], ['users' => [$this->user1, $this->user2]]],
'second user posted' => ['', ['users' => [$this->user1, $this->user2]], ['new_york' => new \DateTime('2000-01-01')], ['users' => [$this->user2, $this->user1]]],
'second user posted later' => ['', ['users' => [$this->user1, $this->user2]], ['seattle' => new \DateTime('2017-01-01'), 'new_york' => new \DateTime('2018-01-01')], ['users' => [$this->user2, $this->user1]]],
'second user posted earlier' => ['', ['users' => [$this->user1, $this->user2]], ['seattle' => new \DateTime('2018-01-01'), 'new_york' => new \DateTime('2017-01-01')], ['users' => [$this->user1, $this->user2]]],
'starting match first1' => ['Sea', ['users' => [$this->user1, $this->user3]], [], ['users' => [$this->user1, $this->user3]]],
'starting match first2' => ['Sea', ['users' => [$this->user3, $this->user1]], [], ['users' => [$this->user1, $this->user3]]],
'no users' => ['', ['groups' => [$this->user1, $this->user2]], [], ['groups' => [$this->user1, $this->user2]]],
];
}
/**
* @dataProvider dataSort
*
* @param string $search
* @param array $toSort
* @param array $comments
* @param array $expected
*/
public function testSort($toSort, $comments, $expected) {
public function testSort(string $search, array $toSort, array $comments, array $expected) {
$this->commentsManager->expects(isset($toSort['users']) ? $this->once() : $this->never())
->method('getLastCommentDateByActor')
->with('chat', '23', 'comment', 'users', $this->anything())
@ -90,6 +101,7 @@ class SorterTest extends \Test\TestCase {
$this->sorter->sort($toSort, [
'itemType' => 'chat',
'itemId' => '23',
'search' => $search,
]);
$this->assertSame($expected, $toSort);
}

Loading…
Cancel
Save