-
-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move gambits to frontend (#3885)
* refactor: move gambits to frontend * Apply fixes from StyleCI * test: GambitManager
- Loading branch information
Showing
45 changed files
with
441 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as default } from '../common/extend'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
]; |
15 changes: 15 additions & 0 deletions
15
extensions/lock/js/src/common/query/discussions/LockedGambit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as default } from '../common/extend'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Extend from 'flarum/common/extenders'; | ||
import StickyGambit from './query/discussions/StickyGambit'; | ||
|
||
export default [ | ||
new Extend.Search() // | ||
.gambit('discussions', StickyGambit), | ||
]; |
15 changes: 15 additions & 0 deletions
15
extensions/sticky/js/src/common/query/discussions/StickyGambit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import IGambit from 'flarum/common/query/IGambit'; | ||
|
||
export default class StickyGambit implements IGambit { | ||
pattern(): string { | ||
return 'is:sticky'; | ||
} | ||
|
||
toFilter(_matches: string[], negate: boolean): Record<string, any> { | ||
const key = (negate ? '-' : '') + 'sticky'; | ||
|
||
return { | ||
[key]: true, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './src/admin'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as default } from '../common/extend'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as extend } from './extend'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Extend from 'flarum/common/extenders'; | ||
import SubscriptionGambit from './query/discussions/SubscriptionGambit'; | ||
|
||
export default [ | ||
new Extend.Search() // | ||
.gambit('discussions', SubscriptionGambit), | ||
]; |
15 changes: 15 additions & 0 deletions
15
extensions/subscriptions/js/src/common/query/discussions/SubscriptionGambit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import IGambit from 'flarum/common/query/IGambit'; | ||
|
||
export default class SubscriptionGambit implements IGambit { | ||
pattern(): string { | ||
return 'is:(follow|ignor)(?:ing|ed)'; | ||
} | ||
|
||
toFilter(matches: string[], negate: boolean): Record<string, any> { | ||
const type = matches[1] === 'follow' ? 'following' : 'ignoring'; | ||
|
||
return { | ||
subscription: type, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as default } from '../common/extend'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Extend from 'flarum/common/extenders'; | ||
import SuspendedGambit from './query/users/SuspendedGambit'; | ||
|
||
export default [ | ||
new Extend.Search() // | ||
.gambit('users', SuspendedGambit), | ||
]; |
15 changes: 15 additions & 0 deletions
15
extensions/suspend/js/src/common/query/users/SuspendedGambit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import IGambit from 'flarum/common/query/IGambit'; | ||
|
||
export default class SuspendedGambit implements IGambit { | ||
pattern(): string { | ||
return 'is:suspended'; | ||
} | ||
|
||
toFilter(_matches: string[], negate: boolean): Record<string, any> { | ||
const key = (negate ? '-' : '') + 'suspended'; | ||
|
||
return { | ||
[key]: true, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
import Extend from 'flarum/common/extenders'; | ||
import Tag from './models/Tag'; | ||
import TagGambit from './query/discussions/TagGambit'; | ||
|
||
export default [ | ||
new Extend.Store() // | ||
.add('tags', Tag), | ||
|
||
new Extend.Search() // | ||
.gambit('discussions', TagGambit), | ||
]; |
15 changes: 15 additions & 0 deletions
15
extensions/tags/js/src/common/query/discussions/TagGambit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import IGambit from 'flarum/common/query/IGambit'; | ||
|
||
export default class TagGambit implements IGambit { | ||
pattern(): string { | ||
return 'tag:(.+)'; | ||
} | ||
|
||
toFilter(matches: string[], negate: boolean): Record<string, any> { | ||
const key = (negate ? '-' : '') + 'tag'; | ||
|
||
return { | ||
[key]: matches[1].split(','), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import IGambit from './query/IGambit'; | ||
import AuthorGambit from './query/discussions/AuthorGambit'; | ||
import CreatedGambit from './query/discussions/CreatedGambit'; | ||
import HiddenGambit from './query/discussions/HiddenGambit'; | ||
import UnreadGambit from './query/discussions/UnreadGambit'; | ||
import EmailGambit from './query/users/EmailGambit'; | ||
import GroupGambit from './query/users/GroupGambit'; | ||
|
||
/** | ||
* The gambit registry. A map of resource types to gambit classes that | ||
* should be used to filter resources of that type. Gambits are automatically | ||
* converted to API filters when requesting resources. Gambits must be applied | ||
* on a filter object that has a `q` property containing the search query. | ||
*/ | ||
export default class GambitManager { | ||
gambits: Record<string, Array<new () => IGambit>> = { | ||
discussions: [AuthorGambit, CreatedGambit, HiddenGambit, UnreadGambit], | ||
users: [EmailGambit, GroupGambit], | ||
}; | ||
|
||
public apply(type: string, filter: Record<string, any>): Record<string, any> { | ||
const gambits = this.gambits[type] || []; | ||
|
||
if (gambits.length === 0) return filter; | ||
|
||
const bits: string[] = filter.q.split(' '); | ||
|
||
for (const gambitClass of gambits) { | ||
const gambit = new gambitClass(); | ||
|
||
for (const bit of bits) { | ||
const pattern = `^(-?)${gambit.pattern()}$`; | ||
let matches = bit.match(pattern); | ||
|
||
if (matches) { | ||
const negate = matches[1] === '-'; | ||
|
||
matches.splice(1, 1); | ||
|
||
Object.assign(filter, gambit.toFilter(matches, negate)); | ||
|
||
filter.q = filter.q.replace(bit, ''); | ||
} | ||
} | ||
} | ||
|
||
filter.q = filter.q.trim().replace(/\s+/g, ' '); | ||
|
||
return filter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type IExtender from './IExtender'; | ||
import type { IExtensionModule } from './IExtender'; | ||
import type Application from '../Application'; | ||
import IGambit from '../query/IGambit'; | ||
|
||
export default class Search implements IExtender { | ||
protected gambits: Record<string, Array<new () => IGambit>> = {}; | ||
|
||
public gambit(modelType: string, gambit: new () => IGambit): this { | ||
this.gambits[modelType] = this.gambits[modelType] || []; | ||
this.gambits[modelType].push(gambit); | ||
|
||
return this; | ||
} | ||
|
||
extend(app: Application, extension: IExtensionModule): void { | ||
for (const [modelType, gambits] of Object.entries(this.gambits)) { | ||
for (const gambit of gambits) { | ||
app.store.gambits.gambits[modelType] = app.store.gambits.gambits[modelType] || []; | ||
app.store.gambits.gambits[modelType].push(gambit); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default interface IGambit { | ||
pattern(): string; | ||
toFilter(matches: string[], negate: boolean): Record<string, any>; | ||
} |
15 changes: 15 additions & 0 deletions
15
framework/core/js/src/common/query/discussions/AuthorGambit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import IGambit from '../IGambit'; | ||
|
||
export default class AuthorGambit implements IGambit { | ||
public pattern(): string { | ||
return 'author:(.+)'; | ||
} | ||
|
||
public toFilter(matches: string[], negate: boolean): Record<string, any> { | ||
const key = (negate ? '-' : '') + 'author'; | ||
|
||
return { | ||
[key]: matches[1].split(','), | ||
}; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
framework/core/js/src/common/query/discussions/CreatedGambit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import IGambit from '../IGambit'; | ||
|
||
export default class CreatedGambit implements IGambit { | ||
pattern(): string { | ||
return 'created:(\\d{4}\\-\\d\\d\\-\\d\\d)(?:\\.\\.(\\d{4}\\-\\d\\d\\-\\d\\d))?'; | ||
} | ||
|
||
toFilter(matches: string[], negate: boolean): Record<string, any> { | ||
const key = (negate ? '-' : '') + 'created'; | ||
|
||
return { | ||
[key]: matches[2] | ||
? { | ||
from: matches[1], | ||
to: matches[2], | ||
} | ||
: matches[1], | ||
}; | ||
} | ||
} |
Oops, something went wrong.