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

add Nextcloud 20 unified searching #344

Closed
wants to merge 2 commits into from
Closed
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
Binary file added img/avatar.webp
Binary file not shown.
101 changes: 19 additions & 82 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
@@ -1,97 +1,34 @@
<?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) 2020 Xiangbin Li >dassio@icloud.com>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Matias De lellis <mati86dl@gmail.com>
* @author Xiangbin Li <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 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.
* 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/>.
* 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\AppInfo;

use OCP\AppFramework\App;
use OCP\AppFramework\IAppContainer;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IUserManager;

use OCA\Files\Event\LoadSidebar;

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

class Application extends App {

/** @var string */
public const APP_NAME = 'facerecognition';

/**
* Application constructor.
*
* @param array $urlParams
*/
public function __construct(array $urlParams = []) {
parent::__construct(self::APP_NAME, $urlParams);

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

private function connectWatcher() {
/** @var IRootFolder $root */
$root = $this->getContainer()->query(IRootFolder::class);

$root->listen('\OC\Files', 'postWrite', function (Node $node) {
/** @var Watcher $watcher */
$watcher = \OC::$server->query(Watcher::class);
$watcher->postWrite($node);
});

// We want to react on postDelete and not preDelete as in preDelete we don't know if
// file actually got deleted (locked, other errors...)
$root->listen('\OC\Files', 'postDelete', function (Node $node) {
/** @var Watcher $watcher */
$watcher = \OC::$server->query(Watcher::class);
$watcher->postDelete($node);
});

// Watch for user deletion, so we clean up user data, after user gets deleted
$userManager = $this->getContainer()->query(IUserManager::class);
$userManager->listen('\OC\User', 'postDelete', function (\OC\User\User $user) {
/** @var Watcher $watcher */
$watcher = \OC::$server->query(Watcher::class);
$watcher->postUserDelete($user);
});
}
namespace OCA\FaceRecognition\AppInfo;

private function connectSearch() {
$this->getContainer()->getServer()->getSearch()->registerProvider(
'OCA\FaceRecognition\Search\Provider',
array('app'=>'facerecognition', 'apps' => array('files'))
);
$version = \OCP\Util::getVersion()[0];
if ($version >= 20) {
matiasdelellis marked this conversation as resolved.
Show resolved Hide resolved
class Application extends Application20 {
}

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

}

98 changes: 98 additions & 0 deletions lib/AppInfo/Application20.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
* @copyright Copyright (c) 2017-2018, 2020 Matias De lellis <mati86dl@gmail.com>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Matias De lellis <mati86dl@gmail.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\AppInfo;

use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\App;
use OCP\AppFramework\IAppContainer;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IUserManager;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Bootstrap\IBootContext;

use OCA\Files\Event\LoadSidebar;
use OCA\FaceRecognition\Listener\LoadSidebarListener;
use OCA\FaceRecognition\Watcher;
use OCA\FaceRecognition\Search\PersonSearchProvider;

class Application20 extends App implements IBootstrap {

public const APP_NAME= 'facerecognition';
/**
* Application constructor.
*
* @param array $urlParams
*/
public function __construct(array $urlParams = []) {
parent::__construct(self::APP_NAME, $urlParams);

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

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

public function boot(IBootContext $context): void {

}

private function connectWatcher() {
/** @var IRootFolder $root */
$root = $this->getContainer()->query(IRootFolder::class);

$root->listen('\OC\Files', 'postWrite', function (Node $node) {
/** @var Watcher $watcher */
$watcher = \OC::$server->query(Watcher::class);
$watcher->postWrite($node);
});

// We want to react on postDelete and not preDelete as in preDelete we don't know if
// file actually got deleted (locked, other errors...)
$root->listen('\OC\Files', 'postDelete', function (Node $node) {
/** @var Watcher $watcher */
$watcher = \OC::$server->query(Watcher::class);
$watcher->postDelete($node);
});

// Watch for user deletion, so we clean up user data, after user gets deleted
$userManager = $this->getContainer()->query(IUserManager::class);
$userManager->listen('\OC\User', 'postDelete', function (\OC\User\User $user) {
/** @var Watcher $watcher */
$watcher = \OC::$server->query(Watcher::class);
$watcher->postUserDelete($user);
});
}

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

}
97 changes: 97 additions & 0 deletions lib/AppInfo/ApplicationLegacy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
* @copyright Copyright (c) 2017-2018, 2020 Matias De lellis <mati86dl@gmail.com>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Matias De lellis <mati86dl@gmail.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\AppInfo;

use OCP\AppFramework\App;
use OCP\AppFramework\IAppContainer;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IUserManager;

use OCA\Files\Event\LoadSidebar;

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

class ApplicationLegacy extends App {

/** @var string */
public const APP_NAME = 'facerecognition';

/**
* Application constructor.
*
* @param array $urlParams
*/
public function __construct(array $urlParams = []) {
parent::__construct(self::APP_NAME, $urlParams);

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

private function connectWatcher() {
/** @var IRootFolder $root */
$root = $this->getContainer()->query(IRootFolder::class);

$root->listen('\OC\Files', 'postWrite', function (Node $node) {
/** @var Watcher $watcher */
$watcher = \OC::$server->query(Watcher::class);
$watcher->postWrite($node);
});

// We want to react on postDelete and not preDelete as in preDelete we don't know if
// file actually got deleted (locked, other errors...)
$root->listen('\OC\Files', 'postDelete', function (Node $node) {
/** @var Watcher $watcher */
$watcher = \OC::$server->query(Watcher::class);
$watcher->postDelete($node);
});

// Watch for user deletion, so we clean up user data, after user gets deleted
$userManager = $this->getContainer()->query(IUserManager::class);
$userManager->listen('\OC\User', 'postDelete', function (\OC\User\User $user) {
/** @var Watcher $watcher */
$watcher = \OC::$server->query(Watcher::class);
$watcher->postUserDelete($user);
});
}

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);
}

}
25 changes: 25 additions & 0 deletions lib/Db/PersonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,29 @@ private function isFaceInClusters(int $faceId, array $clusters): bool {
}
return false;
}

/**
* 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);
}

}
Loading