Skip to content

Commit

Permalink
feat: search driver backend extension API (#3902)
Browse files Browse the repository at this point in the history
* feat: first iteration of search drivers
* feat: indexer API & tweaks
* feat: changes after POC driver
* fix: properly fire custom observables
* chore: remove debugging code
* fix: phpstan
* fix: custom eloquent events
* chore: drop POC usage
* test: indexer extender API
* fix: extension searcher fails without filters
* fix: phpstan
* fix: frontend created gambit
  • Loading branch information
SychO9 authored Oct 18, 2023
1 parent 3769340 commit 1f235a2
Show file tree
Hide file tree
Showing 79 changed files with 1,213 additions and 354 deletions.
9 changes: 4 additions & 5 deletions extensions/likes/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Flarum\Likes\Query\LikedFilter;
use Flarum\Post\Filter\PostSearcher;
use Flarum\Post\Post;
use Flarum\Search\Database\DatabaseSearchDriver;
use Flarum\User\Search\UserSearcher;
use Flarum\User\User;

Expand Down Expand Up @@ -76,11 +77,9 @@
->listen(PostWasUnliked::class, Listener\SendNotificationWhenPostIsUnliked::class)
->subscribe(Listener\SaveLikesToDatabase::class),

(new Extend\SimpleFlarumSearch(PostSearcher::class))
->addFilter(LikedByFilter::class),

(new Extend\SimpleFlarumSearch(UserSearcher::class))
->addFilter(LikedFilter::class),
(new Extend\SearchDriver(DatabaseSearchDriver::class))
->addFilter(PostSearcher::class, LikedByFilter::class)
->addFilter(UserSearcher::class, LikedFilter::class),

(new Extend\Settings())
->default('flarum-likes.like_own_post', true),
Expand Down
6 changes: 5 additions & 1 deletion extensions/likes/src/Query/LikedByFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@

namespace Flarum\Likes\Query;

use Flarum\Search\FilterInterface;
use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\Filter\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\Search\ValidateFilterTrait;

/**
* @implements FilterInterface<DatabaseSearchState>
*/
class LikedByFilter implements FilterInterface
{
use ValidateFilterTrait;
Expand Down
6 changes: 5 additions & 1 deletion extensions/likes/src/Query/LikedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@

namespace Flarum\Likes\Query;

use Flarum\Search\FilterInterface;
use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\Filter\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\Search\ValidateFilterTrait;

/**
* @implements FilterInterface<DatabaseSearchState>
*/
class LikedFilter implements FilterInterface
{
use ValidateFilterTrait;
Expand Down
5 changes: 3 additions & 2 deletions extensions/lock/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Flarum\Lock\Listener;
use Flarum\Lock\Notification\DiscussionLockedBlueprint;
use Flarum\Lock\Post\DiscussionLockedPost;
use Flarum\Search\Database\DatabaseSearchDriver;

return [
(new Extend\Frontend('forum'))
Expand Down Expand Up @@ -56,6 +57,6 @@
(new Extend\Policy())
->modelPolicy(Discussion::class, Access\DiscussionPolicy::class),

(new Extend\SimpleFlarumSearch(DiscussionSearcher::class))
->addFilter(LockedFilter::class),
(new Extend\SearchDriver(DatabaseSearchDriver::class))
->addFilter(DiscussionSearcher::class, LockedFilter::class),
];
6 changes: 5 additions & 1 deletion extensions/lock/src/Filter/LockedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@

namespace Flarum\Lock\Filter;

use Flarum\Search\FilterInterface;
use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\Filter\FilterInterface;
use Flarum\Search\SearchState;
use Illuminate\Database\Query\Builder;

/**
* @implements FilterInterface<DatabaseSearchState>
*/
class LockedFilter implements FilterInterface
{
public function getFilterKey(): string
Expand Down
7 changes: 4 additions & 3 deletions extensions/mentions/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Flarum\Post\Event\Revised;
use Flarum\Post\Filter\PostSearcher;
use Flarum\Post\Post;
use Flarum\Search\Database\DatabaseSearchDriver;
use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\Tags\Tag;
use Flarum\User\User;
Expand Down Expand Up @@ -115,9 +116,9 @@
->listen(Hidden::class, Listener\UpdateMentionsMetadataWhenInvisible::class)
->listen(Deleted::class, Listener\UpdateMentionsMetadataWhenInvisible::class),

(new Extend\SimpleFlarumSearch(PostSearcher::class))
->addFilter(Filter\MentionedFilter::class)
->addFilter(Filter\MentionedPostFilter::class),
(new Extend\SearchDriver(DatabaseSearchDriver::class))
->addFilter(PostSearcher::class, Filter\MentionedFilter::class)
->addFilter(PostSearcher::class, Filter\MentionedPostFilter::class),

(new Extend\ApiSerializer(CurrentUserSerializer::class))
->attribute('canMentionGroups', function (CurrentUserSerializer $serializer, User $user): bool {
Expand Down
6 changes: 5 additions & 1 deletion extensions/mentions/src/Filter/MentionedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@

namespace Flarum\Mentions\Filter;

use Flarum\Search\FilterInterface;
use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\Filter\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\Search\ValidateFilterTrait;

/**
* @implements FilterInterface<DatabaseSearchState>
*/
class MentionedFilter implements FilterInterface
{
use ValidateFilterTrait;
Expand Down
6 changes: 5 additions & 1 deletion extensions/mentions/src/Filter/MentionedPostFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

namespace Flarum\Mentions\Filter;

use Flarum\Search\FilterInterface;
use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\Filter\FilterInterface;
use Flarum\Search\SearchState;

/**
* @implements FilterInterface<DatabaseSearchState>
*/
class MentionedPostFilter implements FilterInterface
{
public function getFilterKey(): string
Expand Down
5 changes: 3 additions & 2 deletions extensions/nicknames/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Flarum\Api\Serializer\UserSerializer;
use Flarum\Extend;
use Flarum\Nicknames\Access\UserPolicy;
use Flarum\Search\Database\DatabaseSearchDriver;
use Flarum\User\Event\Saving;
use Flarum\User\Search\UserSearcher;
use Flarum\User\User;
Expand Down Expand Up @@ -51,8 +52,8 @@
(new Extend\Validator(UserValidator::class))
->configure(AddNicknameValidation::class),

(new Extend\SimpleFlarumSearch(UserSearcher::class))
->setFullTextFilter(NicknameFullTextFilter::class),
(new Extend\SearchDriver(DatabaseSearchDriver::class))
->setFulltext(UserSearcher::class, NicknameFullTextFilter::class),

(new Extend\Policy())
->modelPolicy(User::class, UserPolicy::class),
Expand Down
8 changes: 6 additions & 2 deletions extensions/nicknames/src/NicknameFullTextFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
namespace Flarum\Nicknames;

use Flarum\Search\AbstractFulltextFilter;
use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\SearchState;
use Flarum\User\UserRepository;
use Illuminate\Database\Eloquent\Builder;

/**
* @extends AbstractFulltextFilter<DatabaseSearchState>
*/
class NicknameFullTextFilter extends AbstractFulltextFilter
{
public function __construct(
Expand All @@ -30,12 +34,12 @@ private function getUserSearchSubQuery(string $searchValue): Builder
->orWhere('nickname', 'like', "{$searchValue}%");
}

public function search(SearchState $state, string $query): void
public function search(SearchState $state, string $value): void
{
$state->getQuery()
->whereIn(
'id',
$this->getUserSearchSubQuery($query)
$this->getUserSearchSubQuery($value)
);
}
}
7 changes: 4 additions & 3 deletions extensions/sticky/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Flarum\Discussion\Event\Saving;
use Flarum\Discussion\Search\DiscussionSearcher;
use Flarum\Extend;
use Flarum\Search\Database\DatabaseSearchDriver;
use Flarum\Sticky\Event\DiscussionWasStickied;
use Flarum\Sticky\Event\DiscussionWasUnstickied;
use Flarum\Sticky\Listener;
Expand Down Expand Up @@ -53,7 +54,7 @@
->listen(DiscussionWasStickied::class, [Listener\CreatePostWhenDiscussionIsStickied::class, 'whenDiscussionWasStickied'])
->listen(DiscussionWasUnstickied::class, [Listener\CreatePostWhenDiscussionIsStickied::class, 'whenDiscussionWasUnstickied']),

(new Extend\SimpleFlarumSearch(DiscussionSearcher::class))
->addFilter(StickyFilter::class)
->addSearchMutator(PinStickiedDiscussionsToTop::class),
(new Extend\SearchDriver(DatabaseSearchDriver::class))
->addFilter(DiscussionSearcher::class, StickyFilter::class)
->addMutator(DiscussionSearcher::class, PinStickiedDiscussionsToTop::class),
];
4 changes: 2 additions & 2 deletions extensions/sticky/src/PinStickiedDiscussionsToTop.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

namespace Flarum\Sticky;

use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\SearchCriteria;
use Flarum\Search\SearchState;
use Flarum\Tags\Search\Filter\TagFilter;

class PinStickiedDiscussionsToTop
{
public function __invoke(SearchState $state, SearchCriteria $criteria): void
public function __invoke(DatabaseSearchState $state, SearchCriteria $criteria): void
{
if ($criteria->sortIsDefault && ! $state->isFulltextSearch()) {
$query = $state->getQuery();
Expand Down
6 changes: 5 additions & 1 deletion extensions/sticky/src/Query/StickyFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@

namespace Flarum\Sticky\Query;

use Flarum\Search\FilterInterface;
use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\Filter\FilterInterface;
use Flarum\Search\SearchState;
use Illuminate\Database\Query\Builder;

/**
* @implements FilterInterface<DatabaseSearchState>
*/
class StickyFilter implements FilterInterface
{
public function getFilterKey(): string
Expand Down
7 changes: 4 additions & 3 deletions extensions/subscriptions/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Flarum\Post\Event\Hidden;
use Flarum\Post\Event\Posted;
use Flarum\Post\Event\Restored;
use Flarum\Search\Database\DatabaseSearchDriver;
use Flarum\Subscriptions\Filter\SubscriptionFilter;
use Flarum\Subscriptions\HideIgnoredFromAllDiscussionsPage;
use Flarum\Subscriptions\Listener;
Expand Down Expand Up @@ -69,9 +70,9 @@
->listen(Deleted::class, Listener\DeleteNotificationWhenPostIsHiddenOrDeleted::class)
->listen(Posted::class, Listener\FollowAfterReply::class),

(new Extend\SimpleFlarumSearch(DiscussionSearcher::class))
->addFilter(SubscriptionFilter::class)
->addSearchMutator(HideIgnoredFromAllDiscussionsPage::class),
(new Extend\SearchDriver(DatabaseSearchDriver::class))
->addFilter(DiscussionSearcher::class, SubscriptionFilter::class)
->addMutator(DiscussionSearcher::class, HideIgnoredFromAllDiscussionsPage::class),

(new Extend\User())
->registerPreference('flarum-subscriptions.notify_for_all_posts', 'boolval', false),
Expand Down
6 changes: 5 additions & 1 deletion extensions/subscriptions/src/Filter/SubscriptionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@

namespace Flarum\Subscriptions\Filter;

use Flarum\Search\FilterInterface;
use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\Filter\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\Search\ValidateFilterTrait;
use Flarum\User\User;
use Illuminate\Database\Query\Builder;

/**
* @implements FilterInterface<DatabaseSearchState>
*/
class SubscriptionFilter implements FilterInterface
{
use ValidateFilterTrait;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

namespace Flarum\Subscriptions;

use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\SearchCriteria;
use Flarum\Search\SearchState;

class HideIgnoredFromAllDiscussionsPage
{
public function __invoke(SearchState $state, SearchCriteria $criteria): void
public function __invoke(DatabaseSearchState $state, SearchCriteria $criteria): void
{
// We only want to hide on the "all discussions" page.
if (count($state->getActiveFilters()) === 0 && ! $state->isFulltextSearch()) {
Expand Down
5 changes: 3 additions & 2 deletions extensions/suspend/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Flarum\Api\Serializer\BasicUserSerializer;
use Flarum\Api\Serializer\UserSerializer;
use Flarum\Extend;
use Flarum\Search\Database\DatabaseSearchDriver;
use Flarum\Suspend\Access\UserPolicy;
use Flarum\Suspend\AddUserSuspendAttributes;
use Flarum\Suspend\Event\Suspended;
Expand Down Expand Up @@ -57,8 +58,8 @@
(new Extend\User())
->permissionGroups(RevokeAccessFromSuspendedUsers::class),

(new Extend\SimpleFlarumSearch(UserSearcher::class))
->addFilter(SuspendedFilter::class),
(new Extend\SearchDriver(DatabaseSearchDriver::class))
->addFilter(UserSearcher::class, SuspendedFilter::class),

(new Extend\View())
->namespace('flarum-suspend', __DIR__.'/views'),
Expand Down
6 changes: 5 additions & 1 deletion extensions/suspend/src/Query/SuspendedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
namespace Flarum\Suspend\Query;

use Carbon\Carbon;
use Flarum\Search\FilterInterface;
use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\Filter\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\User\Guest;
use Flarum\User\UserRepository;
use Illuminate\Database\Query\Builder;

/**
* @implements FilterInterface<DatabaseSearchState>
*/
class SuspendedFilter implements FilterInterface
{
public function __construct(
Expand Down
16 changes: 7 additions & 9 deletions extensions/tags/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Flarum\Http\RequestUtil;
use Flarum\Post\Filter\PostSearcher;
use Flarum\Post\Post;
use Flarum\Search\Database\DatabaseSearchDriver;
use Flarum\Tags\Access;
use Flarum\Tags\Api\Controller;
use Flarum\Tags\Api\Serializer\TagSerializer;
Expand Down Expand Up @@ -134,15 +135,12 @@
->listen(DiscussionWasTagged::class, Listener\CreatePostWhenTagsAreChanged::class)
->subscribe(Listener\UpdateTagMetadata::class),

(new Extend\SimpleFlarumSearch(PostSearcher::class))
->addFilter(PostTagFilter::class),

(new Extend\SimpleFlarumSearch(DiscussionSearcher::class))
->addFilter(TagFilter::class)
->addSearchMutator(HideHiddenTagsFromAllDiscussionsPage::class),

(new Extend\SimpleFlarumSearch(TagSearcher::class))
->setFullTextFilter(FulltextFilter::class),
(new Extend\SearchDriver(DatabaseSearchDriver::class))
->addFilter(PostSearcher::class, PostTagFilter::class)
->addFilter(DiscussionSearcher::class, TagFilter::class)
->addMutator(DiscussionSearcher::class, HideHiddenTagsFromAllDiscussionsPage::class)
->addSearcher(Tag::class, TagSearcher::class)
->setFulltext(TagSearcher::class, FulltextFilter::class),

(new Extend\ModelUrl(Tag::class))
->addSlugDriver('default', Utf8SlugDriver::class),
Expand Down
8 changes: 5 additions & 3 deletions extensions/tags/src/Api/Controller/ListTagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
use Flarum\Http\RequestUtil;
use Flarum\Http\UrlGenerator;
use Flarum\Search\SearchCriteria;
use Flarum\Search\SearchManager;
use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\Tags\Search\TagSearcher;
use Flarum\Tags\Tag;
use Flarum\Tags\TagRepository;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
Expand All @@ -35,7 +36,7 @@ class ListTagsController extends AbstractListController

public function __construct(
protected TagRepository $tags,
protected TagSearcher $searcher,
protected SearchManager $search,
protected UrlGenerator $url
) {
}
Expand All @@ -53,7 +54,8 @@ protected function data(ServerRequestInterface $request, Document $document): it
}

if (array_key_exists('q', $filters)) {
$results = $this->searcher->search(new SearchCriteria($actor, $filters), $limit, $offset);
$results = $this->search->query(Tag::class, new SearchCriteria($actor, $filters, $limit, $offset));

$tags = $results->getResults();

$document->addPaginationLinks(
Expand Down
Loading

0 comments on commit 1f235a2

Please sign in to comment.