-
Notifications
You must be signed in to change notification settings - Fork 77
feat: handle command injection optionally #1548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
971ed00
feat: handle command injection optionally
khushal87 318e03c
test: fix broken test
khushal87 04988f0
test: fix broken test
khushal87 780c52c
test: fix broken test
khushal87 84d861a
Merge branch 'master' of github.com:GetStream/stream-chat-js into han…
khushal87 c095a38
Merge branch 'master' of github.com:GetStream/stream-chat-js into han…
khushal87 0b3eccd
fix: add setCommand setter
khushal87 9c9644e
fix: add changes to improve tthe middlware
khushal87 274793c
fix: add searchSource
khushal87 b72490c
fix: handle whitespaces
khushal87 ad400a8
test: fix broken tests
khushal87 febad52
fix: refine the implementation
khushal87 d2d54ac
fix: composition command injection middleware
khushal87 3e7229e
refactor: change name for command middleware
khushal87 5983996
Merge branch 'master' of github.com:GetStream/stream-chat-js into han…
khushal87 19355ce
Merge branch 'master' of github.com:GetStream/stream-chat-js into han…
khushal87 ef19c9d
fix: use search source for the command data
khushal87 136eb09
fix: name of the middleware
khushal87 0a53752
fix: trigger with command
khushal87 cb2ada6
fix: command search
khushal87 114e367
fix: isTextMatched name
khushal87 fdb352b
Merge branch 'master' of github.com:GetStream/stream-chat-js into han…
khushal87 aacb8a9
feat: add active command guard middlware
khushal87 cf88672
test: change file name
khushal87 e14c318
fix: use complete instead of discard
khushal87 f17349e
fix: add improvements
khushal87 d525ae3
refactor: streamline the complete command identification and extracti…
MartinCupela 19edce6
chore: resolve conflicts and add text composer change
khushal87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/messageComposer/middleware/messageComposer/commandInjection.ts
This file contains hidden or 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,72 @@ | ||
| import type { MessageComposer } from '../../messageComposer'; | ||
| import type { | ||
| MessageComposerMiddlewareState, | ||
| MessageCompositionMiddleware, | ||
| MessageDraftComposerMiddlewareValueState, | ||
| MessageDraftCompositionMiddleware, | ||
| } from '../messageComposer/types'; | ||
| import type { MiddlewareHandlerParams } from '../../../middleware'; | ||
|
|
||
| export const createCommandInjectionMiddleware = ( | ||
| composer: MessageComposer, | ||
| ): MessageCompositionMiddleware => ({ | ||
| handlers: { | ||
| compose: ({ | ||
| forward, | ||
| next, | ||
| state, | ||
| }: MiddlewareHandlerParams<MessageComposerMiddlewareState>) => { | ||
| const command = composer.textComposer.command; | ||
| if (!command) { | ||
| return forward(); | ||
| } | ||
| const { text } = state.localMessage; | ||
|
|
||
| const injection = `/${command?.name}`; | ||
| const enrichedText = `${injection} ${text}`; | ||
|
|
||
| return next({ | ||
| ...state, | ||
| localMessage: { | ||
| ...state.localMessage, | ||
| text: enrichedText, | ||
| }, | ||
| message: { | ||
| ...state.message, | ||
| text: enrichedText, | ||
| }, | ||
| }); | ||
| }, | ||
| }, | ||
| id: 'stream-io/message-composer-middleware/command-string-injection', | ||
| }); | ||
|
|
||
| export const createDraftCommandInjectionMiddleware = ( | ||
| composer: MessageComposer, | ||
| ): MessageDraftCompositionMiddleware => ({ | ||
| handlers: { | ||
| compose: ({ | ||
| forward, | ||
| next, | ||
| state, | ||
| }: MiddlewareHandlerParams<MessageDraftComposerMiddlewareValueState>) => { | ||
| const command = composer.textComposer.command; | ||
| if (!command) { | ||
| return forward(); | ||
| } | ||
| const { text } = state.draft; | ||
|
|
||
| const injection = `/${command?.name}`; | ||
| const enrichedText = `${injection} ${text}`; | ||
|
|
||
| return next({ | ||
| ...state, | ||
| draft: { | ||
| ...state.draft, | ||
| text: enrichedText, | ||
| }, | ||
| }); | ||
| }, | ||
| }, | ||
| id: 'stream-io/message-composer-middleware/draft-command-string-injection', | ||
| }); | ||
This file contains hidden or 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
20 changes: 20 additions & 0 deletions
20
src/messageComposer/middleware/textComposer/activeCommandGuard.ts
This file contains hidden or 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 type { Middleware } from '../../../middleware'; | ||
| import type { TextComposerMiddlewareExecutorState } from './TextComposerMiddlewareExecutor'; | ||
|
|
||
| export type PreCommandMiddleware = Middleware< | ||
| TextComposerMiddlewareExecutorState, | ||
| 'onChange' | 'onSuggestionItemSelect' | ||
| >; | ||
|
|
||
| export const createActiveCommandGuardMiddleware = (): PreCommandMiddleware => ({ | ||
| handlers: { | ||
| onChange: ({ complete, forward, state }) => { | ||
| if (state.command) { | ||
| return complete(state); | ||
| } | ||
| return forward(); | ||
| }, | ||
| onSuggestionItemSelect: ({ forward }) => forward(), | ||
| }, | ||
| id: 'stream-io/text-composer/active-command-guard', | ||
| }); |
56 changes: 56 additions & 0 deletions
56
src/messageComposer/middleware/textComposer/commandStringExtraction.ts
This file contains hidden or 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,56 @@ | ||
| import type { TextComposerMiddlewareExecutorState } from './TextComposerMiddlewareExecutor'; | ||
| import type { CommandSuggestion } from './types'; | ||
| import type { Middleware } from '../../../middleware'; | ||
| import { escapeRegExp } from './textMiddlewareUtils'; | ||
|
|
||
| export type CommandStringExtractionMiddleware = Middleware< | ||
| TextComposerMiddlewareExecutorState<CommandSuggestion>, | ||
| 'onChange' | 'onSuggestionItemSelect' | ||
| >; | ||
|
|
||
| const stripCommandFromText = (text: string, commandName: string) => | ||
| text.replace(new RegExp(`^${escapeRegExp(`/${commandName}`)}\\s*`), ''); | ||
|
|
||
| export const createCommandStringExtractionMiddleware = | ||
| (): CommandStringExtractionMiddleware => ({ | ||
| handlers: { | ||
| onChange: ({ complete, forward, state }) => { | ||
| const { command } = state; | ||
|
|
||
| if (!command?.name) { | ||
| return forward(); | ||
| } | ||
|
|
||
| const newText = stripCommandFromText(state.text, command.name); | ||
|
|
||
| return complete({ | ||
| ...state, | ||
| selection: { | ||
| end: newText.length, | ||
| start: newText.length, | ||
| }, | ||
| text: newText, | ||
| }); | ||
| }, | ||
| onSuggestionItemSelect: ({ next, forward, state }) => { | ||
| const { command } = state; | ||
|
|
||
| if (!command) { | ||
| return forward(); | ||
| } | ||
|
|
||
| const triggerWithCommand = `/${command?.name} `; | ||
|
|
||
| const newText = state.text.slice(triggerWithCommand.length); | ||
| return next({ | ||
| ...state, | ||
| selection: { | ||
| end: newText.length, | ||
| start: newText.length, | ||
| }, | ||
| text: newText, | ||
| }); | ||
| }, | ||
| }, | ||
| id: 'stream-io/text-composer/command-string-extraction', | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.