Skip to content

Commit

Permalink
Merge pull request #11456 from nextcloud/feat/noid/search-in-conversa…
Browse files Browse the repository at this point in the history
…tion-filter

feat(search): Allow to search in a dedicated other conversation with a filter
  • Loading branch information
nickvergessen authored Jan 26, 2024
2 parents ca08440 + 978922e commit 23b36b9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 15 deletions.
5 changes: 5 additions & 0 deletions lib/Search/CurrentMessageSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public function search(IUser $user, ISearchQuery $query): SearchResult {
return SearchResult::complete($title, []);
}

$filter = $query->getFilter(self::CONVERSATION_FILTER);
if ($filter && $filter->get() !== $currentToken) {
return SearchResult::complete($title, []);
}

try {
$room = $this->roomManager->getRoomForUserByToken(
$currentToken,
Expand Down
40 changes: 36 additions & 4 deletions lib/Search/MessageSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCA\Talk\Chat\ChatManager;
use OCA\Talk\Chat\MessageParser;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Exceptions\UnauthorizedException;
use OCA\Talk\Manager as RoomManager;
use OCA\Talk\Model\Attendee;
Expand All @@ -38,6 +39,7 @@
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Search\FilterDefinition;
use OCP\Search\IFilter;
use OCP\Search\IFilteringProvider;
use OCP\Search\IProvider;
Expand All @@ -47,6 +49,10 @@

class MessageSearch implements IProvider, IFilteringProvider {

public const CONVERSATION_FILTER = 'conversation';

protected bool $isConversationFiltered = false;

public function __construct(
protected RoomManager $roomManager,
protected ParticipantService $participantService,
Expand Down Expand Up @@ -92,6 +98,9 @@ protected function getCurrentConversationToken(ISearchQuery $query): string {
}

protected function getSublineTemplate(): string {
if ($this->isConversationFiltered) {
return $this->l->t('{user}');
}
return $this->l->t('{user} in {conversation}');
}

Expand All @@ -100,12 +109,32 @@ protected function getSublineTemplate(): string {
*/
public function search(IUser $user, ISearchQuery $query): SearchResult {
$title = $this->l->t('Messages');
if ($this->getCurrentConversationToken($query) !== '') {
$currentToken = $this->getCurrentConversationToken($query);
if ($currentToken !== '') {
$title = $this->l->t('Messages in other conversations');
}

$rooms = $this->roomManager->getRoomsForUser($user->getUID());
return $this->performSearch($user, $query, $title, $rooms);
$filter = $query->getFilter(self::CONVERSATION_FILTER);
if ($filter && $filter->get() !== $currentToken) {
$this->isConversationFiltered = true;
$title = $this->l->t('Messages');

try {
$rooms = [$this->roomManager->getRoomForUserByToken(
$filter->get(),
$user->getUID()
)];
} catch (RoomNotFoundException) {
return SearchResult::complete($title, []);
}
} elseif ($filter) {
// The filter is the "Current conversation" so the CurrentMessageSearch will handle it
return SearchResult::complete($title, []);
} else {
$rooms = $this->roomManager->getRoomsForUser($user->getUID());
}

return $this->performSearch($user, $query, $title, $rooms, $this->isConversationFiltered);
}

/**
Expand Down Expand Up @@ -279,6 +308,7 @@ public function getSupportedFilters(): array {
IFilter::BUILTIN_SINCE,
IFilter::BUILTIN_UNTIL,
IFilter::BUILTIN_PERSON,
self::CONVERSATION_FILTER,
];
}

Expand All @@ -287,6 +317,8 @@ public function getAlternateIds(): array {
}

public function getCustomFilters(): array {
return [];
return [
new FilterDefinition(self::CONVERSATION_FILTER)
];
}
}
20 changes: 17 additions & 3 deletions tests/integration/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2437,17 +2437,31 @@ public function userSeesTheFollowingMessagesInRoom(string $user, string $identif
}

/**
* @Then /^user "([^"]*)" searches for "([^"]*)" in room "([^"]*)" with (\d+)(?: \((v1)\))?$/
* @Then /^user "([^"]*)" searches for messages ?(in other rooms)? with "([^"]*)" in room "([^"]*)" with (\d+)(?: \((v1)\))?$/
*
* @param string $user
* @param string $search
* @param string $identifier
* @param string $statusCode
* @param string $apiVersion
*/
public function userSearchesInRoom(string $user, string $search, string $identifier, $statusCode, string $apiVersion = 'v1', TableNode $formData = null): void {
public function userSearchesInRoom(string $user, string $searchProvider, string $search, string $identifier, $statusCode, string $apiVersion = 'v1', TableNode $formData = null): void {
$searchProvider = $searchProvider === 'in other rooms' ? 'talk-message' : 'talk-message-current';

$searchUrl = '/search/providers/' . $searchProvider . '/search?from=/call/' . self::$identifierToToken[$identifier];
if (str_contains($search, 'conversation:ROOM(')) {
if (preg_match('/conversation:ROOM\((?P<name>\w+)\)/', $search, $matches)) {
if (array_key_exists($matches['name'], self::$identifierToToken)) {
$search = trim(preg_replace('/conversation:ROOM\((\w+)\)/', '', $search));
$searchUrl .= '&conversation=' . self::$identifierToToken[$matches['name']];
}
}
}

$searchUrl .= '&term=' . $search;

$this->setCurrentUser($user);
$this->sendRequest('GET', '/search/providers/talk-message-current/search?term=' . $search . '&from=' . '/call/' . self::$identifierToToken[$identifier]);
$this->sendRequest('GET', $searchUrl);
$this->assertStatusCode($this->response, $statusCode);

if ($statusCode !== '200') {
Expand Down
32 changes: 24 additions & 8 deletions tests/integration/features/chat-2/search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,33 @@ Feature: chat-2/search
| roomType | 3 |
| roomName | room |
And user "participant1" sends message "Message 1" to room "room" with 201
When user "participant2" searches for "essa" in room "room" with 200
When user "participant2" searches for messages with "essa" in room "room" with 200

Scenario: Search for message when being a participant
Given user "participant1" creates room "room" (v4)
Given user "participant1" creates room "room1" (v4)
| roomType | 3 |
| roomName | room |
And user "participant1" adds user "participant2" to room "room" with 200 (v4)
And user "participant1" sends message "Message 1" to room "room" with 201
When user "participant2" searches for "essa" in room "room" with 200
| roomName | room1 |
Given user "participant1" creates room "room2" (v4)
| roomType | 3 |
| roomName | room2 |
And user "participant1" adds user "participant2" to room "room1" with 200 (v4)
And user "participant1" adds user "participant2" to room "room2" with 200 (v4)
And user "participant1" sends message "Message 1" to room "room1" with 201
And user "participant1" sends message "Message 2" to room "room2" with 201
When user "participant2" searches for messages with "essa" in room "room1" with 200
| title | subline | attributes.conversation | attributes.messageId |
| participant1-displayname | Message 1 | room1 | Message 1 |
When user "participant2" searches for messages with "essa" in room "room2" with 200
| title | subline | attributes.conversation | attributes.messageId |
| participant1-displayname | Message 2 | room2 | Message 2 |
When user "participant2" searches for messages with "conversation:ROOM(room1) essa" in room "room1" with 200
| title | subline | attributes.conversation | attributes.messageId |
| participant1-displayname | Message 1 | room1 | Message 1 |
When user "participant2" searches for messages in other rooms with "conversation:ROOM(room1) essa" in room "room1" with 200
When user "participant2" searches for messages with "conversation:ROOM(room1) essa" in room "room2" with 200
When user "participant2" searches for messages in other rooms with "conversation:ROOM(room1) essa" in room "room2" with 200
| title | subline | attributes.conversation | attributes.messageId |
| participant1-displayname | Message 1 | room | Message 1 |
| participant1-displayname | Message 1 | room1 | Message 1 |

Scenario: Can not search when being blocked by the lobby
Given user "participant1" creates room "room" (v4)
Expand All @@ -27,4 +43,4 @@ Feature: chat-2/search
And user "participant1" adds user "participant2" to room "room" with 200 (v4)
And user "participant1" sends message "Message 1" to room "room" with 201
And user "participant1" sets lobby state for room "room" to "non moderators" with 200 (v4)
When user "participant2" searches for "essa" in room "room" with 200
When user "participant2" searches for messages with "essa" in room "room" with 200

0 comments on commit 23b36b9

Please sign in to comment.