mirror of
https://github.com/flarum/core.git
synced 2025-08-06 08:27:42 +02:00
Implement user searching & minor search refactor
This commit is contained in:
@@ -77,13 +77,23 @@ class CoreServiceProvider extends ServiceProvider
|
||||
|
||||
public function registerGambits()
|
||||
{
|
||||
$this->app->bind('Flarum\Core\Search\GambitManager', function () {
|
||||
$gambits = new GambitManager($this->app);
|
||||
$gambits->add('Flarum\Core\Search\Discussions\Gambits\AuthorGambit');
|
||||
$gambits->add('Flarum\Core\Search\Discussions\Gambits\UnreadGambit');
|
||||
$gambits->setFulltextGambit('Flarum\Core\Search\Discussions\Gambits\FulltextGambit');
|
||||
return $gambits;
|
||||
});
|
||||
$this->app->when('Flarum\Core\Search\Discussions\DiscussionSearcher')
|
||||
->needs('Flarum\Core\Search\GambitManager')
|
||||
->give(function () {
|
||||
$gambits = new GambitManager($this->app);
|
||||
$gambits->add('Flarum\Core\Search\Discussions\Gambits\AuthorGambit');
|
||||
$gambits->add('Flarum\Core\Search\Discussions\Gambits\UnreadGambit');
|
||||
$gambits->setFulltextGambit('Flarum\Core\Search\Discussions\Gambits\FulltextGambit');
|
||||
return $gambits;
|
||||
});
|
||||
|
||||
$this->app->when('Flarum\Core\Search\Users\UserSearcher')
|
||||
->needs('Flarum\Core\Search\GambitManager')
|
||||
->give(function () {
|
||||
$gambits = new GambitManager($this->app);
|
||||
$gambits->setFulltextGambit('Flarum\Core\Search\Users\Gambits\FulltextGambit');
|
||||
return $gambits;
|
||||
});
|
||||
}
|
||||
|
||||
public function registerPostTypes()
|
||||
|
@@ -5,7 +5,7 @@ use Flarum\Core\Models\User;
|
||||
interface DiscussionRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Get a new query builder for ths discussions table.
|
||||
* Get a new query builder for the discussions table.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
|
@@ -7,7 +7,7 @@ use Flarum\Core\Models\User;
|
||||
class EloquentDiscussionRepository implements DiscussionRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Get a new query builder for ths discussions table.
|
||||
* Get a new query builder for the discussions table.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
|
@@ -5,6 +5,16 @@ use Flarum\Core\Models\User;
|
||||
|
||||
class EloquentUserRepository implements UserRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Get a new query builder for the users table.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function query()
|
||||
{
|
||||
return User::query();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a user by ID, optionally making sure it is visible to a certain
|
||||
* user, or throw an exception.
|
||||
@@ -49,6 +59,24 @@ class EloquentUserRepository implements UserRepositoryInterface
|
||||
return $this->scopeVisibleForUser($query, $user)->pluck('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Find users by matching a string of words against their username,
|
||||
* optionally making sure they are visible to a certain user.
|
||||
*
|
||||
* @param string $string
|
||||
* @param \Flarum\Core\Models\User|null $user
|
||||
* @return array
|
||||
*/
|
||||
public function getIdsForUsername($string, User $user = null)
|
||||
{
|
||||
$query = User::select('id')
|
||||
->where('username', 'like', '%'.$string.'%')
|
||||
->orderByRaw('username = ? desc', [$string])
|
||||
->orderByRaw('username like ? desc', [$string.'%']);
|
||||
|
||||
return $this->scopeVisibleForUser($query, $user)->lists('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to only include records that are visible to a user.
|
||||
*
|
||||
|
@@ -4,6 +4,13 @@ use Flarum\Core\Models\User;
|
||||
|
||||
interface UserRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Get a new query builder for the users table.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function query();
|
||||
|
||||
/**
|
||||
* Find a user by ID, optionally making sure it is visible to a certain
|
||||
* user, or throw an exception.
|
||||
@@ -32,4 +39,14 @@ interface UserRepositoryInterface
|
||||
* @return integer|null
|
||||
*/
|
||||
public function getIdForUsername($username, User $user = null);
|
||||
|
||||
/**
|
||||
* Find users by matching a string of words against their username,
|
||||
* optionally making sure they are visible to a certain user.
|
||||
*
|
||||
* @param string $string
|
||||
* @param \Flarum\Core\Models\User|null $user
|
||||
* @return array
|
||||
*/
|
||||
public function getIdsForUsername($string, User $user = null);
|
||||
}
|
||||
|
@@ -1,18 +1,19 @@
|
||||
<?php namespace Flarum\Core\Search\Discussions;
|
||||
|
||||
use Flarum\Core\Models\Discussion;
|
||||
use Flarum\Core\Search\SearcherInterface;
|
||||
use Flarum\Core\Search\GambitManager;
|
||||
use Flarum\Core\Repositories\DiscussionRepositoryInterface;
|
||||
use Flarum\Core\Repositories\PostRepositoryInterface;
|
||||
|
||||
class DiscussionSearcher
|
||||
class DiscussionSearcher implements SearcherInterface
|
||||
{
|
||||
public $query;
|
||||
|
||||
protected $sortMap = [
|
||||
'lastPost' => ['last_time', 'desc'],
|
||||
'replies' => ['comments_count', 'desc'],
|
||||
'created' => ['start_time', 'desc']
|
||||
'created' => ['start_time', 'asc']
|
||||
];
|
||||
|
||||
protected $defaultSort = 'lastPost';
|
||||
@@ -43,6 +44,11 @@ class DiscussionSearcher
|
||||
$this->defaultSort = $defaultSort;
|
||||
}
|
||||
|
||||
public function query()
|
||||
{
|
||||
return $this->query;
|
||||
}
|
||||
|
||||
public function search(DiscussionSearchCriteria $criteria, $count = null, $start = 0, $load = [])
|
||||
{
|
||||
$this->user = $criteria->user;
|
||||
@@ -56,7 +62,6 @@ class DiscussionSearcher
|
||||
if (empty($sort)) {
|
||||
$sort = $this->defaultSort;
|
||||
}
|
||||
// dd($sort);
|
||||
if (is_array($sort)) {
|
||||
foreach ($sort as $id) {
|
||||
$this->query->orderByRaw('id != '.(int) $id);
|
||||
|
@@ -1,7 +1,7 @@
|
||||
<?php namespace Flarum\Core\Search\Discussions\Gambits;
|
||||
|
||||
use Flarum\Core\Repositories\UserRepositoryInterface as UserRepository;
|
||||
use Flarum\Core\Search\Discussions\DiscussionSearcher;
|
||||
use Flarum\Core\Search\SearcherInterface;
|
||||
use Flarum\Core\Search\GambitAbstract;
|
||||
|
||||
class AuthorGambit extends GambitAbstract
|
||||
@@ -19,12 +19,12 @@ class AuthorGambit extends GambitAbstract
|
||||
$this->users = $users;
|
||||
}
|
||||
|
||||
public function conditions($matches, DiscussionSearcher $searcher)
|
||||
public function conditions($matches, SearcherInterface $searcher)
|
||||
{
|
||||
$username = trim($matches[1], '"');
|
||||
|
||||
$id = $this->users->getIdForUsername($username);
|
||||
|
||||
$searcher->query->where('start_user_id', $id);
|
||||
$searcher->query()->where('start_user_id', $id);
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
<?php namespace Flarum\Core\Search\Discussions\Gambits;
|
||||
|
||||
use Flarum\Core\Repositories\PostRepositoryInterface;
|
||||
use Flarum\Core\Search\Discussions\DiscussionSearcher;
|
||||
use Flarum\Core\Search\SearcherInterface;
|
||||
use Flarum\Core\Search\GambitAbstract;
|
||||
|
||||
class FulltextGambit extends GambitAbstract
|
||||
@@ -13,7 +13,7 @@ class FulltextGambit extends GambitAbstract
|
||||
$this->posts = $posts;
|
||||
}
|
||||
|
||||
public function apply($string, DiscussionSearcher $searcher)
|
||||
public function apply($string, SearcherInterface $searcher)
|
||||
{
|
||||
$posts = $this->posts->findByContent($string, $searcher->user);
|
||||
|
||||
@@ -24,7 +24,7 @@ class FulltextGambit extends GambitAbstract
|
||||
}
|
||||
$discussions = array_unique($discussions);
|
||||
|
||||
$searcher->query->whereIn('id', $discussions);
|
||||
$searcher->query()->whereIn('id', $discussions);
|
||||
|
||||
$searcher->setDefaultSort($discussions);
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
<?php namespace Flarum\Core\Search\Discussions\Gambits;
|
||||
|
||||
use Flarum\Core\Repositories\DiscussionRepositoryInterface as DiscussionRepository;
|
||||
use Flarum\Core\Search\Discussions\DiscussionSearcher;
|
||||
use Flarum\Core\Search\SearcherInterface;
|
||||
use Flarum\Core\Search\GambitAbstract;
|
||||
|
||||
class UnreadGambit extends GambitAbstract
|
||||
@@ -19,7 +19,7 @@ class UnreadGambit extends GambitAbstract
|
||||
$this->discussions = $discussions;
|
||||
}
|
||||
|
||||
protected function conditions($matches, DiscussionSearcher $searcher)
|
||||
protected function conditions($matches, SearcherInterface $searcher)
|
||||
{
|
||||
$user = $searcher->user;
|
||||
|
||||
@@ -27,9 +27,9 @@ class UnreadGambit extends GambitAbstract
|
||||
$readIds = $this->discussions->getReadIds($user);
|
||||
|
||||
if ($matches[1] === 'true') {
|
||||
$searcher->query->whereNotIn('id', $readIds)->where('last_time', '>', $user->read_time ?: 0);
|
||||
$searcher->query()->whereNotIn('id', $readIds)->where('last_time', '>', $user->read_time ?: 0);
|
||||
} else {
|
||||
$searcher->query->whereIn('id', $readIds)->orWhere('last_time', '<=', $user->read_time ?: 0);
|
||||
$searcher->query()->whereIn('id', $readIds)->orWhere('last_time', '<=', $user->read_time ?: 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,12 +1,10 @@
|
||||
<?php namespace Flarum\Core\Search;
|
||||
|
||||
use Flarum\Core\Search\Discussions\DiscussionSearcher;
|
||||
|
||||
abstract class GambitAbstract
|
||||
{
|
||||
protected $pattern;
|
||||
|
||||
public function apply($bit, DiscussionSearcher $searcher)
|
||||
public function apply($bit, SearcherInterface $searcher)
|
||||
{
|
||||
if ($matches = $this->match($bit)) {
|
||||
$this->conditions($matches, $searcher);
|
||||
|
8
src/Core/Search/SearcherInterface.php
Normal file
8
src/Core/Search/SearcherInterface.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php namespace Flarum\Core\Search;
|
||||
|
||||
interface SearcherInterface
|
||||
{
|
||||
public function query();
|
||||
|
||||
public function setDefaultSort($defaultSort);
|
||||
}
|
24
src/Core/Search/Users/Gambits/FulltextGambit.php
Normal file
24
src/Core/Search/Users/Gambits/FulltextGambit.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php namespace Flarum\Core\Search\Users\Gambits;
|
||||
|
||||
use Flarum\Core\Repositories\UserRepositoryInterface;
|
||||
use Flarum\Core\Search\SearcherInterface;
|
||||
use Flarum\Core\Search\GambitAbstract;
|
||||
|
||||
class FulltextGambit extends GambitAbstract
|
||||
{
|
||||
protected $users;
|
||||
|
||||
public function __construct(UserRepositoryInterface $users)
|
||||
{
|
||||
$this->users = $users;
|
||||
}
|
||||
|
||||
public function apply($string, SearcherInterface $searcher)
|
||||
{
|
||||
$users = $this->users->getIdsForUsername($string, $searcher->user);
|
||||
|
||||
$searcher->query()->whereIn('id', $users);
|
||||
|
||||
$searcher->setDefaultSort($users);
|
||||
}
|
||||
}
|
20
src/Core/Search/Users/UserSearchCriteria.php
Normal file
20
src/Core/Search/Users/UserSearchCriteria.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php namespace Flarum\Core\Search\Users;
|
||||
|
||||
class UserSearchCriteria
|
||||
{
|
||||
public $user;
|
||||
|
||||
public $query;
|
||||
|
||||
public $sort;
|
||||
|
||||
public $order;
|
||||
|
||||
public function __construct($user, $query, $sort, $order)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->query = $query;
|
||||
$this->sort = $sort;
|
||||
$this->order = $order;
|
||||
}
|
||||
}
|
32
src/Core/Search/Users/UserSearchResults.php
Normal file
32
src/Core/Search/Users/UserSearchResults.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php namespace Flarum\Core\Search\Users;
|
||||
|
||||
class UserSearchResults
|
||||
{
|
||||
protected $users;
|
||||
|
||||
protected $areMoreResults;
|
||||
|
||||
protected $total;
|
||||
|
||||
public function __construct($users, $areMoreResults, $total)
|
||||
{
|
||||
$this->users = $users;
|
||||
$this->areMoreResults = $areMoreResults;
|
||||
$this->total = $total;
|
||||
}
|
||||
|
||||
public function getUsers()
|
||||
{
|
||||
return $this->users;
|
||||
}
|
||||
|
||||
public function getTotal()
|
||||
{
|
||||
return $this->total;
|
||||
}
|
||||
|
||||
public function areMoreResults()
|
||||
{
|
||||
return $this->areMoreResults;
|
||||
}
|
||||
}
|
79
src/Core/Search/Users/UserSearcher.php
Normal file
79
src/Core/Search/Users/UserSearcher.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php namespace Flarum\Core\Search\Users;
|
||||
|
||||
use Flarum\Core\Models\User;
|
||||
use Flarum\Core\Search\SearcherInterface;
|
||||
use Flarum\Core\Search\GambitManager;
|
||||
use Flarum\Core\Repositories\UserRepositoryInterface;
|
||||
|
||||
class UserSearcher implements SearcherInterface
|
||||
{
|
||||
public $query;
|
||||
|
||||
protected $sortMap = [
|
||||
'username' => ['username', 'asc'],
|
||||
'posts' => ['comments_count', 'desc'],
|
||||
'discussions' => ['discussions_count', 'desc'],
|
||||
'lastActive' => ['last_seen_time', 'desc'],
|
||||
'created' => ['join_time', 'asc']
|
||||
];
|
||||
|
||||
protected $defaultSort = 'username';
|
||||
|
||||
protected $users;
|
||||
|
||||
public function __construct(GambitManager $gambits, UserRepositoryInterface $users)
|
||||
{
|
||||
$this->gambits = $gambits;
|
||||
$this->users = $users;
|
||||
}
|
||||
|
||||
public function setDefaultSort($defaultSort)
|
||||
{
|
||||
$this->defaultSort = $defaultSort;
|
||||
}
|
||||
|
||||
public function query()
|
||||
{
|
||||
return $this->query;
|
||||
}
|
||||
|
||||
public function search(UserSearchCriteria $criteria, $count = null, $start = 0, $load = [])
|
||||
{
|
||||
$this->user = $criteria->user;
|
||||
$this->query = $this->users->query()->whereCan($criteria->user, 'view');
|
||||
|
||||
$this->gambits->apply($criteria->query, $this);
|
||||
|
||||
$total = $this->query->count();
|
||||
|
||||
$sort = $criteria->sort;
|
||||
if (empty($sort)) {
|
||||
$sort = $this->defaultSort;
|
||||
}
|
||||
if (is_array($sort)) {
|
||||
foreach ($sort as $id) {
|
||||
$this->query->orderByRaw('id != '.(int) $id);
|
||||
}
|
||||
} else {
|
||||
list($column, $order) = $this->sortMap[$sort];
|
||||
$this->query->orderBy($column, $criteria->order ?: $order);
|
||||
}
|
||||
|
||||
if ($start > 0) {
|
||||
$this->query->skip($start);
|
||||
}
|
||||
if ($count > 0) {
|
||||
$this->query->take($count + 1);
|
||||
}
|
||||
|
||||
$users = $this->query->get();
|
||||
|
||||
if ($count > 0 && $areMoreResults = $users->count() > $count) {
|
||||
$users->pop();
|
||||
}
|
||||
|
||||
$users->load($load);
|
||||
|
||||
return new UserSearchResults($users, $areMoreResults, $total);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user