Skip to content

Commit

Permalink
Use the bootstrap mechanism and introduce unified search. See old PR #…
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasdelellis committed Mar 17, 2021
1 parent a110069 commit 67b2b65
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 136 deletions.
29 changes: 0 additions & 29 deletions appinfo/app.php

This file was deleted.

38 changes: 19 additions & 19 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php
/**
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
* @copyright Copyright (c) 2017-2018, 2020 Matias De lellis <mati86dl@gmail.com>
* @copyright Copyright (c) 2017-2021 Matias De lellis <mati86dl@gmail.com>
* @copyright Copyright (c) 2020 Xiangbin Li >dassio@icloud.com>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Matias De lellis <mati86dl@gmail.com>
Expand All @@ -25,18 +26,24 @@
namespace OCA\FaceRecognition\AppInfo;

use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\IAppContainer;

use OCP\EventDispatcher\IEventDispatcher;

use OCA\Files\Event\LoadSidebar;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IUserManager;

use OCA\Files\Event\LoadSidebar;
use OCP\IUserManager;

use OCA\FaceRecognition\Listener\LoadSidebarListener;
use OCA\FaceRecognition\Search\PersonSearchProvider;
use OCA\FaceRecognition\Watcher;

class Application extends App {
class Application extends App implements IBootstrap {

/** @var string */
public const APP_NAME = 'facerecognition';
Expand All @@ -50,8 +57,14 @@ public function __construct(array $urlParams = []) {
parent::__construct(self::APP_NAME, $urlParams);

$this->connectWatcher();
$this->connectSearch();
$this->addServiceListeners();
}

public function register(IRegistrationContext $context): void {
$context->registerSearchProvider(PersonSearchProvider::class);
$context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
}

public function boot(IBootContext $context): void {
}

private function connectWatcher() {
Expand Down Expand Up @@ -81,17 +94,4 @@ private function connectWatcher() {
});
}

private function connectSearch() {
$this->getContainer()->getServer()->getSearch()->registerProvider(
'OCA\FaceRecognition\Search\Provider',
array('app'=>'facerecognition', 'apps' => array('files'))
);
}

private function addServiceListeners() {
/** @var IEventDispatcher $dispatcher */
$dispatcher = \OC::$server->query(IEventDispatcher::class);
$dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarListener::class);
}

}
26 changes: 25 additions & 1 deletion lib/Db/PersonMapper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* @copyright Copyright (c) 2018-2020, Matias De lellis <mati86dl@gmail.com>
* @copyright Copyright (c) 2018-2021, Matias De lellis <mati86dl@gmail.com>
* @copyright Copyright (c) 2018-2019, Branko Kokanovic <branko@kokanovic.org>
*
* @author Branko Kokanovic <branko@kokanovic.org>
Expand Down Expand Up @@ -146,6 +146,30 @@ public function findDistinctNames(string $userId, int $modelId) {
return $this->findEntities($qb);
}

/**
* Search Person by name
*
*/
public function findPersonsLike(string $userId, int $modelId, string $name, $offset = null, $limit = null): array {
$qb = $this->db->getQueryBuilder();
$qb->selectDistinct('p.name')
->from($this->getTableName(), 'p')
->innerJoin('p', 'facerecog_faces', 'f', $qb->expr()->eq('f.person', 'p.id'))
->innerJoin('p', 'facerecog_images', 'i', $qb->expr()->eq('f.image', 'i.id'))
->where($qb->expr()->eq('p.user', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($modelId)))
->andWhere($qb->expr()->eq('is_processed', $qb->createNamedParameter(True)))
->andWhere($qb->expr()->like($qb->func()->lower('p.name'), $qb->createParameter('query')));

$query = '%' . $this->db->escapeLikeParameter(strtolower($name)) . '%';
$qb->setParameter('query', $query);

$qb->setFirstResult($offset);
$qb->setMaxResults($limit);

return $this->findEntities($qb);
}

/**
* Returns count of persons (clusters) found for a given user.
*
Expand Down
142 changes: 142 additions & 0 deletions lib/Search/PersonSearchProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Li Xiangbin <dassio@icloud.com>
*
* @author Li Xiangbin <dassio@icloud.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\FaceRecognition\Search;

use OCP\Search\IProvider;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
use OCP\Files\IRootFolder;

use OCA\FaceRecognition\Db\Person;
use OCA\FaceRecognition\Db\PersonMapper;
use OCA\FaceRecognition\Db\ImageMapper;
use OCA\FaceRecognition\Service\SettingsService;
use OCA\FaceRecognition\Model\IModel;
use OCA\FaceRecognition\Service\UrlService;

/**
* Provide search results from the 'facerecognition' app
*/
class PersonSearchProvider implements IProvider {

/** @var PersonMapper personMapper */
private $personMapper;

/** @var ImageMapper imageMapper */
private $imageMapper;

/** @var SettingsService Settings service */
private $settingsService;

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

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

/** @var UrlService */
private $urlService;

/** @var IRootFolder */
private $rootFolder;

/** @var int*/
private $modelId;

public function __construct(PersonMapper $personMapper,
ImageMapper $imageMapper,
SettingsService $settingsService,
IL10N $l10n,
UrlService $urlService,
IURLGenerator $urlGenerator,
IRootFolder $rootFolder) {
$this->personMapper = $personMapper;
$this->imageMapper = $imageMapper;
$this->settingsService = $settingsService;
$this->l10n = $l10n;
$this->urlService = $urlService;
$this->urlGenerator = $urlGenerator;
$this->rootFolder = $rootFolder;
$this->modelId = $this->settingsService->getCurrentFaceModel();
}

/**
* @inheritDoc
*/
public function getId(): string {
return 'facerecognition';
}

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

/**
* @inheritDoc
*/
public function getOrder(string $route, array $routeParameters): int {
if ($route === 'settings.PersonalSettings.index' &&
$routeParameters["section"] === 'facerecognition') {
return 0;
}
return 10;
}

/**
* @inheritDoc
*/
public function search(IUser $user, ISearchQuery $query) : SearchResult {
$page = $query->getCursor() ?? 0;
$limit = $query->getLimit();
return SearchResult::paginated(
$this->l10n->t('Face Recognition'),
array_map(function (Person $result) {
$personName = $result->getName();
return new SearchResultEntry(
'',
$personName,
'',
$this->urlService->getRedirectToPersonUrl($personName),
'icon-contacts-dark',
true,
);
},
$this->personMapper->findPersonsLike($user->getUID(),
$this->modelId,
$query->getTerm(),
$page * $limit,
$limit)
),
$page);
}

}
83 changes: 0 additions & 83 deletions lib/Search/Provider.php

This file was deleted.

8 changes: 4 additions & 4 deletions lib/Service/UrlService.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* @copyright Copyright (c) 2018-2020 Matias De lellis <mati86dl@gmail.com>
* @copyright Copyright (c) 2018-2021 Matias De lellis <mati86dl@gmail.com>
*
* @author Matias De lellis <mati86dl@gmail.com>
*
Expand Down Expand Up @@ -120,12 +120,12 @@ public function getRedirectToFileUrl(int $fileId) {
/**
* Redirects to the facerecognition page to show photos of an person.
*
* @param int $personId person id to show
* @param int $personName person name used as Id to show
*/
public function getRedirectToPersonUrl(string $personId) {
public function getRedirectToPersonUrl(string $personName) {
$params = [
'section' => 'facerecognition',
'name' => $personId
'name' => $personName
];
return $this->urlGenerator->linkToRoute('settings.PersonalSettings.index', $params);
}
Expand Down

0 comments on commit 67b2b65

Please sign in to comment.