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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

### Added
- Make recipes searchable through unified search
[#611](https://github.com/nextcloud/cookbook/pull/611) @PFischbeck

### Fixed
- Calling reindex
[#653](https://github.com/nextcloud/cookbook/pull/653) @seyfeb
Expand Down
39 changes: 39 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace OCA\Cookbook\AppInfo;

use OCA\Cookbook\Search\Provider;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Util;

// IBootstrap requies NC >= 20
// Remove conditional once we end support for NC 19
if (Util::getVersion()[0] >= 20) {
class Application extends App implements IBootstrap
{
public const APP_ID = 'cookbook';

public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);
}

public function register(IRegistrationContext $context): void {
$context->registerSearchProvider(Provider::class);
}

public function boot(IBootContext $context): void {
}
}
} else {
class Application extends App
{
public const APP_ID = 'cookbook';

public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);
}
}
}
96 changes: 96 additions & 0 deletions lib/Search/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace OCA\Cookbook\Search;

use OCA\Cookbook\AppInfo\Application;
use OCA\Cookbook\Db\RecipeDb;
use OCA\Cookbook\Service\RecipeService;
use OCP\IL10N;
use OCP\IUser;
use OCP\IURLGenerator;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
use OCP\Util;

// IProvider requies NC >= 20
// Remove conditional once we end support for NC 19
if (Util::getVersion()[0] >= 20) {
class Provider implements IProvider
{

/** @var IL10N */
private $l;

/** @var IURLGenerator */
private $urlGenerator;

/** @var RecipeDb */
private $recipeDb;

/** @var RecipeService */
private $recipeService;

public function __construct(IL10n $il10n, IURLGenerator $urlGenerator,
RecipeDb $recipeDb, RecipeService $recipeService) {
$this->l = $il10n;
$this->urlGenerator = $urlGenerator;
$this->recipeDb = $recipeDb;
$this->recipeService = $recipeService;
}

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

public function getName(): string {
return $this->l->t('Recipes');
}

public function getOrder(string $route, array $routeParameters): int {
if (strpos($route, 'files' . '.') === 0) {
return 25;
} elseif (strpos($route, Application::APP_ID . '.') === 0) {
return -1;
}
return 4;
}

public function search(IUser $user, ISearchQuery $query): SearchResult {
$recipes = $this->recipeService->findRecipesInSearchIndex($query->getTerm());
$result = array_map(
function (array $recipe) use ($user) : SearchResultEntry {
$id = $recipe['recipe_id'];

$subline = '';
$category = $this->recipeDb->getCategoryOfRecipe($id, $user->getUID());
if ($category !== null) {
// TRANSLATORS Will be shown in search results, listing the recipe category, e.g., 'in Salads'
$subline = $this->l->t('in %s', [$category]);
}

return new SearchResultEntry(
// Thumb image
$this->urlGenerator->linkToRoute('cookbook.recipe.image', ['id' => $id, 'size' => 'thumb']),
// Name as title
$recipe['name'],
// Category as subline
$subline,
// Link to Vue route of recipe
$this->urlGenerator->linkToRouteAbsolute('cookbook.main.index') . '#/recipe/' . $id
);
}, $recipes
);

return SearchResult::complete(
$this->getName(),
$result
);
}
}
} else {
class Provider
{
}
}