Skip to content

Commit

Permalink
fix(search): Paginate unified search results
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Dec 19, 2024
1 parent 7f3b693 commit 80fcb91
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions lib/Search/ConversationSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function getOrder(string $route, array $routeParameters): ?int {
public function search(IUser $user, ISearchQuery $query): SearchResult {
$rooms = $this->manager->getRoomsForUser($user->getUID());

$cursorKey = null;
$result = [];
foreach ($rooms as $room) {
if ($room->getType() === Room::TYPE_CHANGELOG) {
Expand All @@ -82,18 +83,19 @@ public function search(IUser $user, ISearchQuery $query): SearchResult {
$parameters['token'] === $room->getToken() &&
str_starts_with($query->getRoute(), Application::APP_ID . '.')) {
// Don't search the current conversation.
//User most likely looks for other things with the same name
// User most likely looks for other things with the same name
continue;
}

$displayName = $room->getDisplayName($user->getUID());
if ($room->getType() === Room::TYPE_ONE_TO_ONE || $room->getType() === Room::TYPE_ONE_TO_ONE_FORMER) {
$otherUserId = str_replace(
json_encode($user->getUID()),
'',
$room->getName()
);
if (stripos($otherUserId, $query->getTerm()) === false
&& stripos($room->getDisplayName($user->getUID()), $query->getTerm()) === false) {
&& stripos($displayName, $query->getTerm()) === false) {
// Neither name nor displayname (one-to-one) match, skip
continue;
}
Expand All @@ -103,7 +105,7 @@ public function search(IUser $user, ISearchQuery $query): SearchResult {

$entry = new SearchResultEntry(
$this->avatarService->getAvatarUrl($room),
$room->getDisplayName($user->getUID()),
$displayName,
'',
$this->url->linkToRouteAbsolute('spreed.Page.showCall', ['token' => $room->getToken()]),
'',
Expand All @@ -112,12 +114,51 @@ public function search(IUser $user, ISearchQuery $query): SearchResult {

$entry->addAttribute('conversation', $room->getToken());

$result[] = $entry;
$result[strtolower($displayName . '#' . $room->getToken())] = $entry;

if ($query->getCursor() === $room->getToken()) {
$cursorKey = strtolower($displayName . '#' . $room->getToken());
}
}

return SearchResult::complete(
if (count($result) <= $query->getLimit()) {
return SearchResult::complete(
$this->l->t('Conversations'),
array_values($result),
);
}
ksort($result);

$newCursorWithName = '#';
if ($cursorKey !== null) {
$foundCursor = false;
$filteredResults = [];
foreach ($result as $key => $entry) {
if ($cursorKey === $key) {
$foundCursor = true;
continue;
}
if (!$foundCursor) {
continue;
}
$filteredResults[] = $entry;
if ($query->getLimit() === count($filteredResults)) {
$newCursorWithName = $key;
break;
}
}
} else {
$filteredResults = array_slice($result, 0, $query->getLimit());
$newCursorWithName = array_key_last($filteredResults);
}

$parts = explode('#', $newCursorWithName);
$newCursor = end($parts);

return SearchResult::paginated(
$this->l->t('Conversations'),
$result
array_values($filteredResults),
$newCursor
);
}
}

0 comments on commit 80fcb91

Please sign in to comment.