Skip to content

Commit

Permalink
feat: post search adapted with global search
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Sep 10, 2024
1 parent 4cb6b8f commit 05bbd87
Show file tree
Hide file tree
Showing 47 changed files with 926 additions and 182 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.id();
}
});
});
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 of the user
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.id();
}
});
});

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 of the mentioned user
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
16 changes: 16 additions & 0 deletions framework/core/js/src/common/query/discussions/DiscussionGambit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import app from '../../app';
import { KeyValueGambit } from '../IGambit';

export default class DiscussionGambit extends KeyValueGambit {
key(): string {
return app.translator.trans('core.lib.gambits.posts.discussion.key', {}, true);
}

hint(): string {
return app.translator.trans('core.lib.gambits.posts.discussion.hint', {}, true);
}

filterKey(): string {
return 'discussion';
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import subclassOf from '../../common/utils/subclassOf';

export default class PageState {
constructor(type, data = {}) {
public type: Function | null;
public data: {
routeName?: string | null;
} & Record<string, any>;

constructor(type: Function | null, data: any = {}) {
this.type = type;
this.data = data;
}
Expand All @@ -13,7 +18,7 @@ export default class PageState {
* @param {Record<string, unknown>} data
* @return {boolean}
*/
matches(type, data = {}) {
matches(type: Function, data: any = {}) {
// Fail early when the page is of a different type
if (!subclassOf(this.type, type)) return false;

Expand All @@ -22,11 +27,11 @@ export default class PageState {
return Object.keys(data).every((key) => this.data[key] === data[key]);
}

get(key) {
get(key: string): any {
return this.data[key];
}

set(key, value) {
set(key: string, value: any) {
this.data[key] = value;
}
}
9 changes: 8 additions & 1 deletion framework/core/js/src/common/states/PaginatedListState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default abstract class PaginatedListState<T extends Model, P extends Pagi
public static DEFAULT_PAGE_SIZE = 20;

protected location!: PaginationLocation;
protected pageSize: number | null;
public pageSize: number | null;

protected pages: Page<T>[] = [];
protected params: P = {} as P;
Expand Down Expand Up @@ -267,4 +267,11 @@ export default abstract class PaginatedListState<T extends Model, P extends Pagi
.map((pg) => pg.items)
.flat();
}

/**
* In the last request, has the user searched for a model?
*/
isSearchResults(): boolean {
return !!this.params.q;
}
}
Loading

0 comments on commit 05bbd87

Please sign in to comment.