Browse Source

Add message search v1

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/4017/head
Joas Schilling 5 years ago
parent
commit
7b3f1f37a2
No known key found for this signature in database GPG Key ID: 7076EA9751AACDDA
  1. 2
      lib/AppInfo/Application.php
  2. 14
      lib/Chat/ChatManager.php
  3. 48
      lib/Chat/CommentsManager.php
  4. 176
      lib/Search/MessageSearch.php

2
lib/AppInfo/Application.php

@ -55,6 +55,7 @@ use OCA\Talk\PublicShareAuth\Listener as PublicShareAuthListener;
use OCA\Talk\PublicShareAuth\TemplateLoader as PublicShareAuthTemplateLoader;
use OCA\Talk\Room;
use OCA\Talk\Search\ConversationSearch;
use OCA\Talk\Search\MessageSearch;
use OCA\Talk\Search\UnifiedSearchCSSLoader;
use OCA\Talk\Settings\Personal;
use OCA\Talk\Share\Listener as ShareListener;
@ -96,6 +97,7 @@ class Application extends App implements IBootstrap {
$context->registerEventListener(\OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent::class, UnifiedSearchCSSLoader::class);
$context->registerSearchProvider(ConversationSearch::class);
$context->registerSearchProvider(MessageSearch::class);
$context->registerDashboardWidget(TalkWidget::class);
}

14
lib/Chat/ChatManager.php

@ -299,4 +299,18 @@ class ChatManager {
$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
lib/Chat/CommentsManager.php

@ -226,4 +226,52 @@ class CommentsManager extends Manager {
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;
}
}

176
lib/Search/MessageSearch.php

@ -0,0 +1,176 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
*
* @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\AppInfo\Application;
use OCA\Talk\Chat\ChatManager;
use OCA\Talk\Chat\MessageParser;
use OCA\Talk\Manager as RoomManager;
use OCA\Talk\Room;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
class MessageSearch implements IProvider {
/** @var RoomManager */
protected $roomManager;
/** @var ChatManager */
protected $chatManager;
/** @var MessageParser */
protected $messageParser;
/** @var IURLGenerator */
protected $url;
/** @var IL10N */
protected $l;
public function __construct(
RoomManager $roomManager,
ChatManager $chatManager,
MessageParser $messageParser,
IURLGenerator $url,
IL10N $l
) {
$this->roomManager = $roomManager;
$this->chatManager = $chatManager;
$this->messageParser = $messageParser;
$this->url = $url;
$this->l = $l;
}
/**
* @inheritDoc
*/
public function getId(): string {
return 'talk_message';
}
/**
* @inheritDoc
*/
public function getName(): string {
return $this->l->t('Messages');
}
/**
* @inheritDoc
*/
public function getOrder(string $route, array $routeParameters): int {
if (strpos($route, Application::APP_ID . '.') === 0) {
// Active app, prefer Talk results
return -1;
}
return 15;
}
/**
* @inheritDoc
*/
public function search(IUser $user, ISearchQuery $query): SearchResult {
$rooms = $this->roomManager->getRoomsForParticipant($user->getUID());
$subline = $this->l->t('{user} in {conversation}');
$roomMap = [];
foreach ($rooms as $room) {
if ($room->getType() === Room::CHANGELOG_CONVERSATION) {
continue;
}
$roomMap[(string) $room->getId()] = $room;
}
if (empty($roomMap)) {
return SearchResult::complete(
$this->l->t('Messages'),
[]
);
}
$comments = $this->chatManager->searchForObjects(
$query->getTerm(),
array_keys($roomMap),
'comment',
0,
$query->getLimit()
);
$result = [];
foreach ($comments as $comment) {
$room = $roomMap[$comment->getObjectId()];
$participant = $room->getParticipant($user->getUID());
$id = (int) $comment->getId();
$message = $this->messageParser->createMessage($room, $participant, $comment, $this->l);
$this->messageParser->parseMessage($message);
$messageStr = $message->getMessage();
$search = $replace = [];
foreach ($message->getMessageParameters() as $key => $parameter) {
$search = '{' . $key . '}';
if ($parameter['type'] === 'user') {
$replace = '@' . $parameter['name'];
} else {
$replace = $parameter['name'];
}
}
$messageStr = str_replace($search, $replace, $messageStr);
if (!$message->getVisibility()) {
$commentIdToIndex[$id] = null;
continue;
}
$iconUrl = '';
if ($message->getActorType() === 'users') {
$iconUrl = $this->url->linkToRouteAbsolute('core.avatar.getAvatar', [
'userId' => $message->getActorId(),
'size' => 64,
]);
}
$result[] = new SearchResultEntry(
$iconUrl,
$messageStr,
str_replace(
['{user}', '{conversation}'],
[$message->getActorDisplayName(), $room->getDisplayName($user->getUID())],
$subline
),
$this->url->linkToRouteAbsolute('spreed.Page.showCall', ['token' => $room->getId()]) . '#m' . $id,
'icon-talk', // $iconClass,
true
);
}
return SearchResult::complete(
$this->l->t('Messages'),
$result
);
}
}
Loading…
Cancel
Save