Skip to content

Commit

Permalink
refactor: filters to gambits
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Sep 26, 2023
1 parent bdbacdd commit 3d2d0c7
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class LockedGambit implements IGambit {
[key]: true,
};
}

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

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}is:locked`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class StickyGambit implements IGambit {
[key]: true,
};
}

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

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}is:sticky`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class SubscriptionGambit implements IGambit {
subscription: type,
};
}

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

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}is:${value}`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class SuspendedGambit implements IGambit {
[key]: true,
};
}

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

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}is:suspended`;
}
}
8 changes: 8 additions & 0 deletions extensions/tags/js/src/common/query/discussions/TagGambit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class TagGambit implements IGambit {
[key]: matches[1].split(','),
};
}

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

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}tag:${value}`;
}
}
21 changes: 21 additions & 0 deletions framework/core/js/src/common/GambitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,25 @@ export default class GambitManager {

return filter;
}

public from(type: string, q: string, filter: Record<string, any>): string {
const gambits = this.gambits[type] || [];

if (gambits.length === 0) return q;

Object.keys(filter).forEach((key) => {
for (const gambitClass of gambits) {
const gambit = new gambitClass();
const negate = key[0] === '-';

if (negate) key = key.substring(1);

if (gambit.filterKey() !== key) continue;

q += ` ${gambit.fromFilter(filter[key], negate)}`;
}
});

return q;
}
}
2 changes: 2 additions & 0 deletions framework/core/js/src/common/query/IGambit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default interface IGambit {
pattern(): string;
toFilter(matches: string[], negate: boolean): Record<string, any>;
filterKey(): string;
fromFilter(value: string, negate: boolean): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class AuthorGambit implements IGambit {
[key]: matches[1].split(','),
};
}

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

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}author:${value}`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class CreatedGambit implements IGambit {
[key]: matches[1],
};
}

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

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}created:${value}`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class HiddenGambit implements IGambit {
[key]: true,
};
}

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

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}is:hidden`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class UnreadGambit implements IGambit {
[key]: true,
};
}

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

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}is:unread`;
}
}
8 changes: 8 additions & 0 deletions framework/core/js/src/common/query/users/EmailGambit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class EmailGambit implements IGambit {
[key]: matches[1],
};
}

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

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}email:${value}`;
}
}
8 changes: 8 additions & 0 deletions framework/core/js/src/common/query/users/GroupGambit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class GroupGambit implements IGambit {
[key]: matches[1].split(','),
};
}

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

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}group:${value}`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ export default class DiscussionsSearchSource implements SearchSource {
);
}) as Array<Mithril.Vnode>;

const filter = app.store.gambits.apply('discussions', { q: query });
const q = filter.q || null;

delete filter.q;

return [
<li className="Dropdown-header">{app.translator.trans('core.forum.search.discussions_heading')}</li>,
<li>
<LinkButton icon="fas fa-search" href={app.route('index', { q: query })}>
<LinkButton icon="fas fa-search" href={app.route('index', { q, filter })}>
{app.translator.trans('core.forum.search.all_discussions_button', { query })}
</LinkButton>
</li>,
Expand Down
16 changes: 12 additions & 4 deletions framework/core/js/src/forum/states/GlobalSearchState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ export default class GlobalSearchState extends SearchState {
* @inheritdoc
*/
getInitialSearch(): string {
return this.currPageProvidesSearch() ? this.params().q : '';
return this.currPageProvidesSearch() ? this.searchToQuery() : '';
}

private searchToQuery(): string {
const q = this.params().q || '';
const filter = this.params().filter || {};

return app.store.gambits.from('users', app.store.gambits.from('discussions', q, filter), filter).trim();
}

/**
Expand All @@ -57,7 +64,7 @@ export default class GlobalSearchState extends SearchState {
* 'x' is clicked in the search box in the header.
*/
protected clearInitialSearch() {
const { q, ...params } = this.params();
const { q, filter, ...params } = this.params();

setRouteWithForcedRefresh(app.route(app.current.get('routeName'), params));
}
Expand All @@ -71,6 +78,9 @@ export default class GlobalSearchState extends SearchState {
return {
sort: m.route.param('sort'),
q: m.route.param('q'),
// Objects must be copied, otherwise they are passed by reference.
// Which could end up undesirably modifying the mithril route params.
filter: Object.assign({}, m.route.param('filter')),
};
}

Expand All @@ -80,8 +90,6 @@ export default class GlobalSearchState extends SearchState {
params(): SearchParams {
const params = this.stickyParams();

params.filter = m.route.param('filter');

return params;
}

Expand Down

0 comments on commit 3d2d0c7

Please sign in to comment.