Skip to content

Commit

Permalink
feat: post search adapted with global search (#4019)
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 authored Sep 19, 2024
1 parent 06eb613 commit 1ab3029
Show file tree
Hide file tree
Showing 52 changed files with 974 additions and 188 deletions.
7 changes: 7 additions & 0 deletions extensions/likes/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 LikedByGambit from './query/posts/LikedByGambit';

export default [
new Extend.Search() //
.gambit('posts', LikedByGambit),
];
16 changes: 16 additions & 0 deletions extensions/likes/js/src/common/query/posts/LikedByGambit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { KeyValueGambit } from 'flarum/common/query/IGambit';
import app from 'flarum/common/app';

export default class LikedByGambit extends KeyValueGambit {
key(): string {
return app.translator.trans('flarum-likes.lib.gambits.posts.likedBy.key', {}, true);
}

hint(): string {
return app.translator.trans('flarum-likes.lib.gambits.posts.likedBy.hint', {}, true);
}

filterKey(): string {
return 'likedBy';
}
}
16 changes: 16 additions & 0 deletions extensions/likes/js/src/forum/components/LikesUserPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import PostsUserPage from 'flarum/forum/components/PostsUserPage';
import type User from 'flarum/common/models/User';

/**
* The `LikesUserPage` component shows posts which user the user liked.
*/
export default class LikesUserPage extends PostsUserPage {
params(user: User) {
return {
filter: {
type: 'comment',
likedBy: user.id(),
},
};
}
}
24 changes: 0 additions & 24 deletions extensions/likes/js/src/forum/components/LikesUserPage.tsx

This file was deleted.

4 changes: 4 additions & 0 deletions extensions/likes/js/src/forum/extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import User from 'flarum/common/models/User';
import LikesUserPage from './components/LikesUserPage';
import PostLikedNotification from './components/PostLikedNotification';

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

export default [
...commonExtend,

new Extend.Routes() //
.add('user.likes', '/u/:username/likes', LikesUserPage),

Expand Down
18 changes: 17 additions & 1 deletion extensions/likes/js/src/forum/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extend } from 'flarum/common/extend';
import { extend, override } from 'flarum/common/extend';
import app from 'flarum/forum/app';

import addLikeAction from './addLikeAction';
Expand All @@ -19,4 +19,20 @@ app.initializers.add('flarum-likes', () => {
label: app.translator.trans('flarum-likes.forum.settings.notify_post_liked_label'),
});
});

// Auto scope the search to the current user liked posts.
override('flarum/forum/components/SearchModal', 'defaultActiveSource', function (original) {
const orig = original();

if (!orig && app.current.data.routeName && app.current.data.routeName.includes('user.likes') && 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.likes') && app.current.data.user) {
filters.posts.likedBy = app.current.data.user.username();
}
});
});
10 changes: 10 additions & 0 deletions extensions/likes/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ flarum-likes:
# These translations are used in the User profile page.
user:
likes_link: Likes

# 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:
likedBy:
key: likedBy
hint: The ID or username of the user
14 changes: 13 additions & 1 deletion extensions/likes/src/Query/LikedByFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Flarum\Search\Filter\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\Search\ValidateFilterTrait;
use Flarum\User\UserRepository;

/**
* @implements FilterInterface<DatabaseSearchState>
Expand All @@ -21,14 +22,25 @@ class LikedByFilter implements FilterInterface
{
use ValidateFilterTrait;

public function __construct(
protected UserRepository $users
) {
}

public function getFilterKey(): string
{
return 'likedBy';
}

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

$likedId = $this->users->getIdForUsername($likedUsername);

if (! $likedId) {
$likedId = intval($likedUsername);
}

$state
->getQuery()
Expand Down
6 changes: 6 additions & 0 deletions extensions/likes/src/Query/LikedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Flarum\Search\Filter\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\Search\ValidateFilterTrait;
use Flarum\User\UserRepository;

/**
* @implements FilterInterface<DatabaseSearchState>
Expand All @@ -21,6 +22,11 @@ class LikedFilter implements FilterInterface
{
use ValidateFilterTrait;

public function __construct(
protected UserRepository $users
) {
}

public function getFilterKey(): string
{
return 'liked';
Expand Down
3 changes: 3 additions & 0 deletions extensions/mentions/js/src/admin/extend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import commonExtend from '../common/extend';

export default [...commonExtend];
2 changes: 2 additions & 0 deletions extensions/mentions/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('flarum-mentions', () => {
app.extensionData
.for('flarum-mentions')
Expand Down
7 changes: 7 additions & 0 deletions extensions/mentions/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 MentionedGambit from './query/posts/MentionedGambit';

export default [
new Extend.Search() //
.gambit('posts', MentionedGambit),
];
16 changes: 16 additions & 0 deletions extensions/mentions/js/src/common/query/posts/MentionedGambit.ts
Original file line number Diff line number Diff line change
@@ -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';
}
}
25 changes: 0 additions & 25 deletions extensions/mentions/js/src/forum/components/MentionsUserPage.js

This file was deleted.

16 changes: 16 additions & 0 deletions extensions/mentions/js/src/forum/components/MentionsUserPage.ts
Original file line number Diff line number Diff line change
@@ -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(),
},
};
}
}
4 changes: 4 additions & 0 deletions extensions/mentions/js/src/forum/extend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down
18 changes: 17 additions & 1 deletion extensions/mentions/js/src/forum/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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';
Expand Down
10 changes: 10 additions & 0 deletions extensions/mentions/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 13 additions & 1 deletion extensions/mentions/src/Filter/MentionedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Flarum\Search\Filter\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\Search\ValidateFilterTrait;
use Flarum\User\UserRepository;

/**
* @implements FilterInterface<DatabaseSearchState>
Expand All @@ -21,14 +22,25 @@ class MentionedFilter implements FilterInterface
{
use ValidateFilterTrait;

public function __construct(
protected UserRepository $users
) {
}

public function getFilterKey(): string
{
return 'mentioned';
}

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()
Expand Down
4 changes: 3 additions & 1 deletion framework/core/js/src/common/GambitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import HiddenGambit from './query/discussions/HiddenGambit';
import UnreadGambit from './query/discussions/UnreadGambit';
import EmailGambit from './query/users/EmailGambit';
import GroupGambit from './query/users/GroupGambit';
import DiscussionGambit from './query/discussions/DiscussionGambit';

/**
* The gambit registry. A map of resource types to gambit classes that
Expand All @@ -15,6 +16,7 @@ import GroupGambit from './query/users/GroupGambit';
export default class GambitManager {
gambits: Record<string, Array<new () => IGambit>> = {
discussions: [AuthorGambit, CreatedGambit, HiddenGambit, UnreadGambit],
posts: [AuthorGambit, DiscussionGambit],
users: [EmailGambit, GroupGambit],
};

Expand Down Expand Up @@ -43,7 +45,7 @@ export default class GambitManager {

for (const gambit of gambits) {
for (const bit of bits) {
const pattern = `^(-?)${gambit.pattern()}$`;
const pattern = new RegExp(`^(-?)${gambit.pattern()}$`, 'i');
let matches = bit.match(pattern);

if (matches) {
Expand Down
8 changes: 6 additions & 2 deletions framework/core/js/src/common/helpers/highlight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { truncate } from '../utils/string';
* @param phrase The word or words to highlight.
* @param [length] The number of characters to truncate the string to.
* The string will be truncated surrounding the first match.
* @param safe Whether the content is safe to render as HTML or
* should be escaped (HTML entities encoded).
*/
export default function highlight(string: string, phrase?: string | RegExp, length?: number): Mithril.Vnode<any, any> | string {
export default function highlight(string: string, phrase?: string | RegExp, length?: number, safe = false): Mithril.Vnode<any, any> | string {
if (!phrase && !length) return string;

// Convert the word phrase into a global regular expression (if it isn't
Expand All @@ -29,7 +31,9 @@ export default function highlight(string: string, phrase?: string | RegExp, leng

// Convert the string into HTML entities, then highlight all matches with
// <mark> tags. Then we will return the result as a trusted HTML string.
highlighted = $('<div/>').text(highlighted).html();
if (!safe) {
highlighted = $('<div/>').text(highlighted).html();
}

if (phrase) highlighted = highlighted.replace(regexp, '<mark>$&</mark>');

Expand Down
Loading

0 comments on commit 1ab3029

Please sign in to comment.