1
0
mirror of https://github.com/flarum/core.git synced 2025-08-15 12:54:47 +02:00

feat: post search adapted with global search (#4019)

This commit is contained in:
Sami Mazouz
2024-09-19 17:01:58 +01:00
committed by GitHub
parent 06eb613c9b
commit 1ab3029e78
52 changed files with 974 additions and 188 deletions

View File

@@ -0,0 +1,3 @@
import commonExtend from '../common/extend';
export default [...commonExtend];

View File

@@ -1,5 +1,7 @@
import app from 'flarum/admin/app';
export { default as extend } from './extend';
app.initializers.add('flarum-mentions', () => {
app.extensionData
.for('flarum-mentions')

View File

@@ -0,0 +1,7 @@
import Extend from 'flarum/common/extenders';
import MentionedGambit from './query/posts/MentionedGambit';
export default [
new Extend.Search() //
.gambit('posts', MentionedGambit),
];

View File

@@ -0,0 +1,16 @@
import { KeyValueGambit } from 'flarum/common/query/IGambit';
import app from 'flarum/common/app';
export default class MentionedGambit extends KeyValueGambit {
key(): string {
return app.translator.trans('flarum-mentions.lib.gambits.posts.mentioned.key', {}, true);
}
hint(): string {
return app.translator.trans('flarum-mentions.lib.gambits.posts.mentioned.hint', {}, true);
}
filterKey(): string {
return 'mentioned';
}
}

View File

@@ -1,25 +0,0 @@
import app from 'flarum/forum/app';
import PostsUserPage from 'flarum/forum/components/PostsUserPage';
/**
* The `MentionsUserPage` component shows post which user Mentioned at
*/
export default class MentionsUserPage extends PostsUserPage {
/**
* Load a new page of the user's activity feed.
*
* @param {Integer} [offset] The position to start getting results from.
* @return {Promise}
* @protected
*/
loadResults(offset) {
return app.store.find('posts', {
filter: {
type: 'comment',
mentioned: this.user.id(),
},
page: { offset, limit: this.loadLimit },
sort: '-createdAt',
});
}
}

View File

@@ -0,0 +1,16 @@
import PostsUserPage from 'flarum/forum/components/PostsUserPage';
import type User from 'flarum/common/models/User';
/**
* The `MentionsUserPage` component shows post which user Mentioned at
*/
export default class MentionsUserPage extends PostsUserPage {
params(user: User) {
return {
filter: {
type: 'comment',
mentioned: user.id(),
},
};
}
}

View File

@@ -6,7 +6,11 @@ import PostMentionedNotification from './components/PostMentionedNotification';
import UserMentionedNotification from './components/UserMentionedNotification';
import GroupMentionedNotification from './components/GroupMentionedNotification';
import commonExtend from '../common/extend';
export default [
...commonExtend,
new Extend.Routes() //
.add('user.mentions', '/u/:username/mentions', MentionsUserPage),

View File

@@ -1,4 +1,4 @@
import { extend } from 'flarum/common/extend';
import { extend, override } from 'flarum/common/extend';
import app from 'flarum/forum/app';
import { getPlainContent } from 'flarum/common/utils/string';
import textContrastClass from 'flarum/common/helpers/textContrastClass';
@@ -79,6 +79,22 @@ app.initializers.add('flarum-mentions', () => {
this.classList.add(textContrastClass(getComputedStyle(this).getPropertyValue('--color')));
});
});
// Auto scope the search to the current user mentioned posts.
override('flarum/forum/components/SearchModal', 'defaultActiveSource', function (original) {
const orig = original();
if (!orig && app.current.data.routeName && app.current.data.routeName.includes('user.mentions') && app.current.data.user) {
return 'posts';
}
return orig;
});
extend('flarum/forum/components/SearchModal', 'defaultFilters', function (filters) {
if (app.current.data.routeName && app.current.data.routeName.includes('user.mentions') && app.current.data.user) {
filters.posts.mentioned = app.current.data.user.username();
}
});
});
export * from './utils/textFormatter';

View File

@@ -110,3 +110,13 @@ flarum-mentions:
{content}
html:
body: "{mentioner_display_name} mentioned a group you're a member of in [{title}]({url})."
# Translations in this namespace are used by the forum and admin interfaces.
lib:
# These translations are used by gambits. Gambit keys must be in snake_case, no spaces.
gambits:
posts:
mentioned:
key: mentioned
hint: The ID or username of the mentioned user

View File

@@ -13,6 +13,7 @@ use Flarum\Search\Database\DatabaseSearchState;
use Flarum\Search\Filter\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\Search\ValidateFilterTrait;
use Flarum\User\UserRepository;
/**
* @implements FilterInterface<DatabaseSearchState>
@@ -21,6 +22,11 @@ class MentionedFilter implements FilterInterface
{
use ValidateFilterTrait;
public function __construct(
protected UserRepository $users
) {
}
public function getFilterKey(): string
{
return 'mentioned';
@@ -28,7 +34,13 @@ class MentionedFilter implements FilterInterface
public function filter(SearchState $state, string|array $value, bool $negate): void
{
$mentionedId = $this->asInt($value);
$mentionedUsername = $this->asString($value);
$mentionedId = $this->users->getIdForUsername($mentionedUsername);
if (! $mentionedId) {
$mentionedId = intval($mentionedUsername);
}
$state
->getQuery()