Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified search #600

Merged
merged 2 commits into from
Oct 1, 2020
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
1 change: 1 addition & 0 deletions css/global.scss
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
@include icon-black-white('notes', 'notes', 1);
@include icon-black-white('notes-trans', 'notes', 1);
1 change: 1 addition & 0 deletions img/notes-trans.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(array $urlParams = []) {

public function register(IRegistrationContext $context): void {
$context->registerCapability(Capabilities::class);
$context->registerSearchProvider(SearchProvider::class);
$context->registerDashboardWidget(DashboardWidget::class);
$context->registerEventListener(
BeforeTemplateRenderedEvent::class,
Expand Down
84 changes: 84 additions & 0 deletions lib/AppInfo/SearchProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace OCA\Notes\AppInfo;

use OCA\Notes\Service\Note;
use OCA\Notes\Service\NotesService;
use OCA\Notes\Service\Util;

use OCP\IUser;
use OCP\IURLGenerator;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;

class SearchProvider implements IProvider {

/** @var Util */
private $util;
/** @var NotesService */
private $notesService;
/** @var IURLGenerator */
private $url;

public function __construct(
Util $util,
NotesService $notesService,
IURLGenerator $url
) {
$this->util = $util;
$this->notesService = $notesService;
$this->url = $url;
}


public function getId(): string {
return Application::APP_ID;
}

public function getName(): string {
return $this->util->l10n->t('Notes');
}

public function getOrder(string $route, array $routeParameters): int {
if (strpos($route, 'files' . '.') === 0) {
return 25;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you put yourself lower when you're on files?

} elseif (strpos($route, Application::APP_ID . '.') === 0) {
return -1;
}
return 4;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default is far too high.
Do not inject yourself above default nextcloud apps please

Comment on lines +47 to +52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (strpos($route, 'files' . '.') === 0) {
return 25;
} elseif (strpos($route, Application::APP_ID . '.') === 0) {
return -1;
}
return 4;
if (strpos($route, Application::APP_ID . '.') === 0) {
return -1;
}
return 40;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So yeah, as korel already mentioned. Notes are files, but the notes app can present them better. So it should be the prefered app when you search anywhere over files, hence the 4 in general, because it should be before files which has 5. Then again on files you might prefer the files search (or not) which is why 25 was picked there, and -1 when in notes.

}

public function search(IUser $user, ISearchQuery $query): SearchResult {
$notes = $this->notesService->search($user->getUID(), $query->getTerm());
// sort by modified time
usort($notes, function (Note $a, Note $b) {
return $b->getModified() - $a->getModified();
});
// create SearchResultEntry from Note
$result = array_map(
function (Note $note) : SearchResultEntry {
$excerpt = $note->getCategory();
try {
$excerpt = $note->getExcerpt();
} catch (\Throwable $e) {
}
return new SearchResultEntry(
'',
$note->getTitle(),
$excerpt,
$this->url->linkToRouteAbsolute('notes.page.index') . 'note/'.$note->getId(),
'icon-notes-trans'
);
},
$notes
);
return SearchResult::complete(
$this->getName(),
$result
);
}
}
33 changes: 33 additions & 0 deletions lib/Service/NotesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,39 @@ public function get(string $userId, int $id) : Note {
return $note;
}

public function search(string $userId, string $search) : array {
$terms = preg_split('/\s+/', $search);
$notes = $this->getAll($userId)['notes'];
return array_values(array_filter(
$notes,
function (Note $note) use ($terms) : bool {
return $this->searchTermsInNote($note, $terms);
}
));
}
private function searchTermsInNote(Note $note, array $terms) : bool {
try {
$d = $note->getData();
$strings = [ $d['title'], $d['category'], $d['content'] ];
foreach ($terms as $term) {
if (!$this->searchTermInData($strings, $term)) {
return false;
}
}
return true;
} catch (\Throwable $e) {
return false;
}
}
private function searchTermInData(array $strings, string $term) : bool {
foreach ($strings as $str) {
if (stripos($str, $term) !== false) {
return true;
}
}
return false;
}


/**
* @throws \OCP\Files\NotPermittedException
Expand Down
24 changes: 0 additions & 24 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<NavigationList v-show="!loading.notes"
:filtered-notes="filteredNotes"
:category="filter.category"
:search="filter.search"
@category-selected="onSelectCategory"
@note-deleted="onNoteDeleted"
/>
Expand Down Expand Up @@ -45,7 +44,6 @@ import {
Content,
} from '@nextcloud/vue'
import { showSuccess } from '@nextcloud/dialogs'
import { emit } from '@nextcloud/event-bus'
import '@nextcloud/dialogs/styles/toast.scss'

import { config } from './config'
Expand All @@ -70,7 +68,6 @@ export default {
return {
filter: {
category: null,
search: '',
},
loading: {
notes: true,
Expand All @@ -89,20 +86,12 @@ export default {
},

filteredNotes() {
const search = this.filter.search.toLowerCase()

const notes = this.notes.filter(note => {
if (this.filter.category !== null
&& this.filter.category !== note.category
&& !note.category.startsWith(this.filter.category + '/')) {
return false
}
const searchFields = ['title', 'category']
if (search !== '') {
return searchFields.some(
searchField => note[searchField].toLowerCase().indexOf(search) !== -1
)
}
return true
})

Expand All @@ -128,9 +117,6 @@ export default {

created() {
store.commit('setDocumentTitle', document.title)
if (typeof OCA.Search === 'function') {
this.search = new OCA.Search(this.onSearch, this.onResetSearch)
}
window.addEventListener('beforeunload', this.onClose)
this.loadNotes()
},
Expand Down Expand Up @@ -205,16 +191,6 @@ export default {
}
},

onSearch(query) {
this.filter.search = query

emit('toggle-navigation', { open: true })
},

onResetSearch() {
this.filter.search = ''
},

onNewNote() {
if (this.loading.create) {
return
Expand Down
50 changes: 0 additions & 50 deletions src/components/NavigationList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,6 @@
@category-selected="$emit('category-selected', $event)"
/>

<!-- search result header -->
<AppNavigationCaption v-if="search && filteredNotes.length" :title="searchResultTitle" class="search-result-header" />

<!-- nothing found -->
<li v-if="search && !filteredNotes.length" class="no-search-result">
<span class="nav-entry">
<div id="emptycontent" class="emptycontent-search">
<div class="icon-search" />
<h2 v-if="category!==null">
{{ t('notes', 'No search result for “{search}” in {category}', { search: search, category: categoryTitle(category) }) }}
</h2>
<h2 v-else>
{{ t('notes', 'No search result for “{search}”', { search: search }) }}
</h2>
</div>
</span>
</li>

<!-- list of notes -->
<template v-for="item in noteItems">
<AppNavigationCaption v-if="category!==null && category!==item.category"
Expand Down Expand Up @@ -92,10 +74,6 @@ export default {
type: String,
default: null,
},
search: {
type: String,
default: '',
},
},

data() {
Expand Down Expand Up @@ -145,14 +123,6 @@ export default {
noteItems() {
return this.groupedNotes
},

searchResultTitle() {
if (this.category !== null) {
return t('notes', 'Search result for “{search}” in {category}', { search: this.search, category: this.categoryTitle(this.category) })
} else {
return t('notes', 'Search result for “{search}”', { search: this.search })
}
},
},

watch: {
Expand Down Expand Up @@ -209,23 +179,3 @@ export default {
},
}
</script>
<style scoped>
.search-result-header {
color: inherit;
}

li.no-search-result {
order: 1;
}

li .nav-entry .emptycontent-search {
white-space: normal;
}

@media (max-height: 600px) {
li .nav-entry .emptycontent-search {
margin-top: inherit;
}
}

</style>