Skip to content

Commit

Permalink
Merge pull request #33 from nextcloud/feat/search-messages
Browse files Browse the repository at this point in the history
feat: add Zulip messages to unified search
  • Loading branch information
edward-ly authored Dec 2, 2024
2 parents 1cea0f4 + 9b9e63e commit 4d6f8a2
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 0 deletions.
9 changes: 9 additions & 0 deletions css/zulip-search.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.icon-zulip-search-fallback {
background-image: url('../img/app-dark.svg');
filter: var(--background-invert-if-dark);
}

/* for NC <= 24 */
body.theme--dark .icon-zulip-search-fallback {
background-image: url('../img/app.svg');
}
4 changes: 4 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
namespace OCA\Zulip\AppInfo;

use OCA\Zulip\Listener\FilesMenuListener;
use OCA\Zulip\Search\ZulipSearchMessagesProvider;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent;

class Application extends App implements IBootstrap {
Expand All @@ -34,6 +36,8 @@ public function __construct(array $urlParams = []) {

public function register(IRegistrationContext $context): void {
$context->registerEventListener(LoadAdditionalScriptsEvent::class, FilesMenuListener::class);
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
$context->registerSearchProvider(ZulipSearchMessagesProvider::class);
}

public function boot(IBootContext $context): void {
Expand Down
22 changes: 22 additions & 0 deletions lib/AppInfo/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace OCA\Zulip\AppInfo;

use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;

/** @template-implements IEventListener<BeforeTemplateRenderedEvent|Event> */
class BeforeTemplateRenderedListener implements IEventListener {
public function handle(Event $event): void {
if (!($event instanceof BeforeTemplateRenderedEvent)) {
return;
}
if (!$event->isLoggedIn()) {
return;
}
\OCP\Util::addStyle(Application::APP_ID, 'zulip-search');
}
}
181 changes: 181 additions & 0 deletions lib/Search/ZulipSearchMessagesProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024, Edward Ly
*
* @author Edward Ly <[email protected]>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Zulip\Search;

use OCA\Zulip\AppInfo\Application;
use OCA\Zulip\Service\SecretService;
use OCA\Zulip\Service\ZulipAPIService;
use OCP\App\IAppManager;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\IDateTimeZone;
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 ZulipSearchMessagesProvider implements IProvider {

public function __construct(
private IAppManager $appManager,
private IL10N $l10n,
private IConfig $config,
private IURLGenerator $urlGenerator,
private IDateTimeFormatter $dateTimeFormatter,
private IDateTimeZone $dateTimeZone,
private SecretService $secretService,
private ZulipAPIService $apiService
) {
}

/**
* @inheritDoc
*/
public function getId(): string {
return 'zulip-search-messages';
}

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

/**
* @inheritDoc
*/
public function getOrder(string $route, array $routeParameters): int {
if (strpos($route, Application::APP_ID . '.') === 0) {
// Active app, prefer Zulip results
return -1;
}

return 20;
}

/**
* @inheritDoc
*/
public function search(IUser $user, ISearchQuery $query): SearchResult {
if (!$this->appManager->isEnabledForUser(Application::APP_ID, $user)) {
return SearchResult::complete($this->getName(), []);
}

$limit = $query->getLimit();
$term = $query->getTerm();
$offset = $query->getCursor();
$offset = $offset ? intval($offset) : 0;

$url = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'url');
$email = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'email');
$apiKey = $this->secretService->getEncryptedUserValue($user->getUID(), 'api_key');
$searchMessagesEnabled = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'search_messages_enabled', '0') === '1';
if ($url === '' || $email === '' || $apiKey === '' || !$searchMessagesEnabled) {
return SearchResult::paginated($this->getName(), [], 0);
}

$searchResult = $this->apiService->searchMessages($user->getUID(), $term, $offset, $limit);
if (isset($searchResult['error'])) {
return SearchResult::paginated($this->getName(), [], 0);
}

$formattedResults = array_map(function (array $entry) use ($url): SearchResultEntry {
$finalThumbnailUrl = $this->getThumbnailUrl($entry);
return new SearchResultEntry(
$finalThumbnailUrl,
$this->getMainText($entry),
$this->getSubline($entry),
$this->getLinkToZulip($entry, $url),
$finalThumbnailUrl === '' ? 'icon-zulip-search-fallback' : '',
true
);
}, $searchResult);

return SearchResult::paginated(
$this->getName(),
$formattedResults,
$offset + $limit
);
}

/**
* @param array $entry
* @return string
*/
protected function getMainText(array $entry): string {
return strip_tags($entry['content']);
}

/**
* @param array $entry
* @return string
*/
protected function getSubline(array $entry): string {
if ($entry['type'] === 'stream') {
return $this->l10n->t('%s in #%s > %s at %s', [$entry['sender_full_name'], $entry['display_recipient'], $entry['subject'], $this->getFormattedDate($entry['timestamp'])]);
}

$recipients = array_map(fn (array $user): string => $user['full_name'], $entry['display_recipient']);
$displayRecipients = '@' . $recipients[0] . (count($recipients) > 1 ? ' (+' . strval(count($recipients) - 1) . ')' : '');
return $this->l10n->t('%s in %s at %s', [$entry['sender_full_name'], $displayRecipients, $this->getFormattedDate($entry['timestamp'])]);
}

protected function getFormattedDate(int $timestamp): string {
return $this->dateTimeFormatter->formatDateTime($timestamp, 'long', 'short', $this->dateTimeZone->getTimeZone());
}

/**
* @param array $entry
* @param string $url
* @return string
*/
protected function getLinkToZulip(array $entry, string $url): string {
if ($entry['type'] === 'private') {
$userIds = array_map(fn (array $recipient): string => strval($recipient['id']), $entry['display_recipient']);
return rtrim($url, '/') . '/#narrow/dm/' . implode(',', $userIds) . '/near/' . $entry['id'];
}

$topic = str_replace('%', '.', rawurlencode($entry['subject']));
return rtrim($url, '/') . '/#narrow/channel/' . $entry['stream_id'] . '/topic/' . $topic . '/near/' . $entry['id'];
}

/**
* @param array $entry
* @return string
*/
protected function getThumbnailUrl(array $entry): string {
return '';
// $senderId = $entry['sender_id'] ?? '';
// return $senderId
// ? $this->urlGenerator->getAbsoluteURL(
// $this->urlGenerator->linkToRoute('integration_zulip.zulipAPI.getUserAvatar', ['zulipUserId' => $senderId])
// )
// : '';
}
}
23 changes: 23 additions & 0 deletions lib/Service/ZulipAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ public function __construct(
$this->client = $clientService->newClient();
}

/**
* @param string $userId
* @return array
* @throws PreConditionNotMetException
*/
public function searchMessages(string $userId, string $term, int $offset = 0, int $limit = 5): array {
$result = $this->request($userId, 'messages', [
'anchor' => 'newest',
'num_before' => $offset + $limit,
'num_after' => 0,
'narrow' => '[{"operator": "search", "operand": "' . $term . '"}]',
'client_gravatar' => 'true',
]);

if (isset($result['error'])) {
return (array) $result;
}

// sort by most recent
$messages = array_reverse($result['messages'] ?? []);
return array_slice($messages, $offset, $limit);
}

/**
* @param string $userId
* @param int $zulipUserId
Expand Down
2 changes: 2 additions & 0 deletions lib/Settings/Personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ public function getForm(): TemplateResponse {
$email = $this->config->getUserValue($this->userId, Application::APP_ID, 'email');
$apiKey = $this->secretService->getEncryptedUserValue($this->userId, 'api_key') ? 'dummyKey' : '';
$fileActionEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'file_action_enabled', '1') === '1';
$searchMessagesEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'search_messages_enabled', '0') === '1';

$userConfig = [
'url' => $url,
'email' => $email,
'api_key' => $apiKey,
'file_action_enabled' => $fileActionEnabled,
'search_messages_enabled' => $searchMessagesEnabled,
];
$this->initialStateService->provideInitialState('user-config', $userConfig);
return new TemplateResponse(Application::APP_ID, 'personalSettings');
Expand Down
5 changes: 5 additions & 0 deletions src/components/PersonalSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
@update:checked="onCheckboxChanged($event, 'file_action_enabled')">
{{ t('integration_zulip', 'Add file action to send files to Zulip') }}
</NcCheckboxRadioSwitch>
<NcCheckboxRadioSwitch
:checked.sync="state.search_messages_enabled"
@update:checked="onCheckboxChanged($event, 'search_messages_enabled')">
{{ t('integration_zulip', 'Enable searching for messages') }}
</NcCheckboxRadioSwitch>
</div>
</div>
</template>
Expand Down

0 comments on commit 4d6f8a2

Please sign in to comment.