Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
use OCA\Talk\PublicShareAuth\TemplateLoader as PublicShareAuthTemplateLoader;
use OCA\Talk\Room;
use OCA\Talk\Search\ConversationSearch;
use OCA\Talk\Search\CurrentMessageSearch;
use OCA\Talk\Search\MessageSearch;
use OCA\Talk\Search\UnifiedSearchCSSLoader;
use OCA\Talk\Settings\Personal;
use OCA\Talk\Share\Listener as ShareListener;
Expand Down Expand Up @@ -96,6 +98,8 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(\OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent::class, UnifiedSearchCSSLoader::class);

$context->registerSearchProvider(ConversationSearch::class);
$context->registerSearchProvider(CurrentMessageSearch::class);
$context->registerSearchProvider(MessageSearch::class);

$context->registerDashboardWidget(TalkWidget::class);
}
Expand Down
14 changes: 14 additions & 0 deletions lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,18 @@ public function deleteMessages(Room $chat): void {

$this->notifier->removePendingNotificationsForRoom($chat);
}

/**
* Search for comments with a given content
*
* @param string $search content to search for
* @param array $objectIds Limit the search by object ids
* @param string $verb Limit the verb of the comment
* @param int $offset
* @param int $limit
* @return IComment[]
*/
public function searchForObjects(string $search, array $objectIds, string $verb = '', int $offset = 0, int $limit = 50): array {
return $this->commentsManager->searchForObjects($search, 'chat', $objectIds, $verb, $offset, $limit);
}
}
48 changes: 48 additions & 0 deletions lib/Chat/CommentsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,52 @@ public function getLastCommentBeforeDate(string $objectType, string $objectId, \

return (int) ($data['id'] ?? 0);
}

/**
* Search for comments with a given content
*
* @param string $search content to search for
* @param string $objectType Limit the search by object type
* @param array $objectIds Limit the search by object ids
* @param string $verb Limit the verb of the comment
* @param int $offset
* @param int $limit
* @return IComment[]
*/
public function searchForObjects(string $search, string $objectType, array $objectIds, string $verb, int $offset, int $limit = 50): array {
$query = $this->dbConn->getQueryBuilder();

$query->select('*')
->from('comments')
->where($query->expr()->iLike('message', $query->createNamedParameter(
'%' . $this->dbConn->escapeLikeParameter($search). '%'
)))
->orderBy('creation_timestamp', 'DESC')
->addOrderBy('id', 'DESC')
->setMaxResults($limit);

if ($objectType !== '') {
$query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType)));
}
if (!empty($objectIds)) {
$query->andWhere($query->expr()->in('object_id', $query->createNamedParameter($objectIds, IQueryBuilder::PARAM_STR_ARRAY)));
}
if ($verb !== '') {
$query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb)));
}
if ($offset !== 0) {
$query->setFirstResult($offset);
}

$comments = [];
$result = $query->execute();
while ($data = $result->fetch()) {
$comment = $this->getCommentFromData($data);
$this->cache($comment);
$comments[] = $comment;
}
$result->closeCursor();

return $comments;
}
}
4 changes: 2 additions & 2 deletions lib/Search/ConversationSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(
* @inheritDoc
*/
public function getId(): string {
return 'talk_conversations';
return 'talk-conversations';
}

/**
Expand All @@ -76,7 +76,7 @@ public function getOrder(string $route, array $routeParameters): int {
return -1;
}

return 15;
return 25;
}

/**
Expand Down
116 changes: 116 additions & 0 deletions lib/Search/CurrentMessageSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Talk\Search;

use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Exceptions\UnauthorizedException;
use OCP\IUser;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;

class CurrentMessageSearch extends MessageSearch {

/**
* @inheritDoc
*/
public function getId(): string {
return 'talk-message-current';
}

/**
* @inheritDoc
*/
public function getName(): string {
return $this->l->t('Messages');
}

/**
* @inheritDoc
*/
public function getOrder(string $route, array $routeParameters): int {
if ($route === 'spreed.Page.showCall') {
// In conversation, prefer this search results
return -3;
}

// We are not returning something anyway.
return 999;
}

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

/**
* @inheritDoc
*/
public function search(IUser $user, ISearchQuery $query): SearchResult {
$currentToken = $this->getCurrentConversationToken($query);
if ($currentToken === '') {
return SearchResult::complete(
$this->l->t('Messages'),
[]
);
}

try {
$room = $this->roomManager->getRoomForParticipantByToken(
$currentToken,
$user->getUID()
);
} catch (RoomNotFoundException $e) {
return SearchResult::complete(
$this->l->t('Messages'),
[]
);
}

$comments = $this->chatManager->searchForObjects(
$query->getTerm(),
[(string) $room->getId()],
'comment',
0,
$query->getLimit()
);

$result = [];
foreach ($comments as $comment) {
try {
$result[] = $this->commentToSearchResultEntry($room, $user, $comment, $query);
} catch (UnauthorizedException $e) {
} catch (ParticipantNotFoundException $e) {
}
}

return SearchResult::complete(
str_replace(
'{conversation}',
$room->getDisplayName($user->getUID()),
$this->l->t('Messages in {conversation}')
),
$result
);
}
}
Loading