Skip to content

Commit

Permalink
feat: revamp search (#3893)
Browse files Browse the repository at this point in the history
* refactor: move gambits to frontend (#3885)
* refactor: move gambits to frontend
* test: GambitManager
* refactor: merge filterer and searcher concepts (#3892)
* chore: drop remaining backend regex gambits
* refactor: merge filterer & searcher concept
* refactor: adapt extenders
* refactor: no longer need to push gambits to `q`
* refactor: filters to gambits
* refactor: drop shred `Query` namespace
* chore: cleanup
* chore: leftover gambit references on the backend (#3894)
* chore: leftover gambit references on the backend
* chore: namespace
* feat: search driver backend extension API (#3902)
* 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
* feat: advanced page and localized driver settings (#3905)
* feat: allow getting total search results and replacing filters (#3906)
* feat: allow accessing total search results
* feat: allow replacing filters
* chore: phpstan
  • Loading branch information
SychO9 authored Nov 11, 2023
1 parent 9e04b01 commit 4b126d9
Show file tree
Hide file tree
Showing 161 changed files with 2,721 additions and 2,184 deletions.
13 changes: 6 additions & 7 deletions extensions/likes/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
use Flarum\Likes\Notification\PostLikedBlueprint;
use Flarum\Likes\Query\LikedByFilter;
use Flarum\Likes\Query\LikedFilter;
use Flarum\Post\Filter\PostFilterer;
use Flarum\Post\Filter\PostSearcher;
use Flarum\Post\Post;
use Flarum\User\Filter\UserFilterer;
use Flarum\Search\Database\DatabaseSearchDriver;
use Flarum\User\Search\UserSearcher;
use Flarum\User\User;

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

(new Extend\Filter(PostFilterer::class))
->addFilter(LikedByFilter::class),

(new Extend\Filter(UserFilterer::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
16 changes: 10 additions & 6 deletions 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\Filter\FilterInterface;
use Flarum\Filter\FilterState;
use Flarum\Filter\ValidateFilterTrait;
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 All @@ -22,11 +26,11 @@ public function getFilterKey(): string
return 'likedBy';
}

public function filter(FilterState $filterState, string|array $filterValue, bool $negate): void
public function filter(SearchState $state, string|array $value, bool $negate): void
{
$likedId = $this->asInt($filterValue);
$likedId = $this->asInt($value);

$filterState
$state
->getQuery()
->whereIn('id', function ($query) use ($likedId, $negate) {
$query->select('post_id')
Expand Down
16 changes: 10 additions & 6 deletions 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\Filter\FilterInterface;
use Flarum\Filter\FilterState;
use Flarum\Filter\ValidateFilterTrait;
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 All @@ -22,11 +26,11 @@ public function getFilterKey(): string
return 'liked';
}

public function filter(FilterState $filterState, string|array $filterValue, bool $negate): void
public function filter(SearchState $state, string|array $value, bool $negate): void
{
$likedId = $this->asString($filterValue);
$likedId = $this->asString($value);

$filterState
$state
->getQuery()
->whereIn('id', function ($query) use ($likedId) {
$query->select('user_id')
Expand Down
11 changes: 4 additions & 7 deletions extensions/lock/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Saving;
use Flarum\Discussion\Filter\DiscussionFilterer;
use Flarum\Discussion\Search\DiscussionSearcher;
use Flarum\Extend;
use Flarum\Lock\Access;
use Flarum\Lock\Event\DiscussionWasLocked;
use Flarum\Lock\Event\DiscussionWasUnlocked;
use Flarum\Lock\Filter\LockedFilter;
use Flarum\Lock\Listener;
use Flarum\Lock\Notification\DiscussionLockedBlueprint;
use Flarum\Lock\Post\DiscussionLockedPost;
use Flarum\Lock\Query\LockedFilterGambit;
use Flarum\Search\Database\DatabaseSearchDriver;

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

(new Extend\Filter(DiscussionFilterer::class))
->addFilter(LockedFilterGambit::class),

(new Extend\SimpleFlarumSearch(DiscussionSearcher::class))
->addGambit(LockedFilterGambit::class),
(new Extend\SearchDriver(DatabaseSearchDriver::class))
->addFilter(DiscussionSearcher::class, LockedFilter::class),
];
1 change: 1 addition & 0 deletions extensions/lock/js/src/admin/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as default } from '../common/extend';
2 changes: 2 additions & 0 deletions extensions/lock/js/src/admin/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import app from 'flarum/admin/app';

export { default as extend } from './extend';

app.initializers.add('lock', () => {
app.extensionData.for('flarum-lock').registerPermission(
{
Expand Down
7 changes: 7 additions & 0 deletions extensions/lock/js/src/common/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Extend from 'flarum/common/extenders';
import LockedGambit from './query/discussions/LockedGambit';

export default [
new Extend.Search() //
.gambit('discussions', LockedGambit),
];
23 changes: 23 additions & 0 deletions extensions/lock/js/src/common/query/discussions/LockedGambit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import IGambit from 'flarum/common/query/IGambit';

export default class LockedGambit implements IGambit {
pattern(): string {
return 'is:locked';
}

toFilter(_matches: string[], negate: boolean): Record<string, any> {
const key = (negate ? '-' : '') + 'locked';

return {
[key]: true,
};
}

filterKey(): string {
return 'locked';
}

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}is:locked`;
}
}
4 changes: 4 additions & 0 deletions extensions/lock/js/src/forum/extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import Extend from 'flarum/common/extenders';
import Discussion from 'flarum/common/models/Discussion';
import DiscussionLockedPost from './components/DiscussionLockedPost';

import commonExtend from '../common/extend';

export default [
...commonExtend,

new Extend.PostTypes() //
.add('discussionLocked', DiscussionLockedPost),

Expand Down
36 changes: 36 additions & 0 deletions extensions/lock/src/Filter/LockedFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Lock\Filter;

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
{
return 'locked';
}

public function filter(SearchState $state, string|array $value, bool $negate): void
{
$this->constrain($state->getQuery(), $negate);
}

protected function constrain(Builder $query, bool $negate): void
{
$query->where('is_locked', ! $negate);
}
}
44 changes: 0 additions & 44 deletions extensions/lock/src/Query/LockedFilterGambit.php

This file was deleted.

9 changes: 5 additions & 4 deletions extensions/mentions/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
use Flarum\Post\Event\Posted;
use Flarum\Post\Event\Restored;
use Flarum\Post\Event\Revised;
use Flarum\Post\Filter\PostFilterer;
use Flarum\Post\Filter\PostSearcher;
use Flarum\Post\Post;
use Flarum\Search\Database\DatabaseSearchDriver;
use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\User\User;

Expand Down Expand Up @@ -114,9 +115,9 @@
->listen(Hidden::class, Listener\UpdateMentionsMetadataWhenInvisible::class)
->listen(Deleted::class, Listener\UpdateMentionsMetadataWhenInvisible::class),

(new Extend\Filter(PostFilterer::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
16 changes: 10 additions & 6 deletions 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\Filter\FilterInterface;
use Flarum\Filter\FilterState;
use Flarum\Filter\ValidateFilterTrait;
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 All @@ -22,11 +26,11 @@ public function getFilterKey(): string
return 'mentioned';
}

public function filter(FilterState $filterState, string|array $filterValue, bool $negate): void
public function filter(SearchState $state, string|array $value, bool $negate): void
{
$mentionedId = $this->asInt($filterValue);
$mentionedId = $this->asInt($value);

$filterState
$state
->getQuery()
->join('post_mentions_user', 'posts.id', '=', 'post_mentions_user.post_id')
->where('post_mentions_user.mentions_user_id', $negate ? '!=' : '=', $mentionedId);
Expand Down
14 changes: 9 additions & 5 deletions extensions/mentions/src/Filter/MentionedPostFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@

namespace Flarum\Mentions\Filter;

use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
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
{
return 'mentionedPost';
}

public function filter(FilterState $filterState, string|array $filterValue, bool $negate): void
public function filter(SearchState $state, string|array $value, bool $negate): void
{
$mentionedId = trim($filterValue, '"');
$mentionedId = trim($value, '"');

$filterState
$state
->getQuery()
->join('post_mentions_post', 'posts.id', '=', 'post_mentions_post.post_id')
->where('post_mentions_post.mentions_post_id', $negate ? '!=' : '=', $mentionedId);
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 @@ -52,8 +53,8 @@
(new Extend\Validator(UserValidator::class))
->configure(AddNicknameValidation::class),

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

(new Extend\Policy())
->modelPolicy(User::class, UserPolicy::class),
Expand Down
7 changes: 7 additions & 0 deletions extensions/nicknames/js/src/admin/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import app from 'flarum/admin/app';
import Alert from 'flarum/common/components/Alert';
import Link from 'flarum/common/components/Link';
import BasicsPage from 'flarum/admin/components/BasicsPage';
import extractText from 'flarum/common/utils/extractText';
import { extend } from 'flarum/common/extend';

app.initializers.add('flarum/nicknames', () => {
app.extensionData
Expand Down Expand Up @@ -55,4 +58,8 @@ app.initializers.add('flarum/nicknames', () => {
},
'start'
);

extend(BasicsPage.prototype, 'driverLocale', function (locale) {
locale.display_name['nickname'] = extractText(app.translator.trans('flarum-nicknames.admin.basics.display_name_driver_options.nickname'));
});
});
3 changes: 3 additions & 0 deletions extensions/nicknames/locale/en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
flarum-nicknames:
admin:
basics:
display_name_driver_options:
nickname: Nickname
permissions:
edit_own_nickname_label: Edit own nickname
settings:
Expand Down
Loading

0 comments on commit 4b126d9

Please sign in to comment.