Skip to content

Commit b39324b

Browse files
committed
fix: check the send link capability before sending message
1 parent 70b11e3 commit b39324b

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

package/src/components/Channel/__tests__/ownCapabilities.test.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,18 @@ describe('Own capabilities', () => {
343343
describe(`${allOwnCapabilities.sendLinks} capability`, () => {
344344
it(`should not allow sending links when "${allOwnCapabilities.sendLinks}" capability is disabled`, async () => {
345345
await generateChannelWithCapabilities([allOwnCapabilities.sendMessage]);
346-
const { queryByTestId } = render(
347-
getComponent({
348-
initialValue: 'Awesome repository https://github.com/GetStream/stream-chat-react-native',
349-
}),
350-
);
346+
const { queryByTestId } = render(getComponent());
347+
348+
await act(async () => {
349+
const text = 'Awesome repository https://github.com/GetStream/stream-chat-react-native';
350+
await channel.messageComposer.textComposer.handleChange({
351+
selection: {
352+
end: text.length,
353+
start: text.length,
354+
},
355+
text,
356+
});
357+
});
351358

352359
const sendMessage = jest.fn();
353360
channel.sendMessage = sendMessage;
@@ -371,10 +378,20 @@ describe('Own capabilities', () => {
371378
mockFn();
372379
return sendMessageApi();
373380
},
374-
initialValue: 'Awesome repository https://github.com/GetStream/stream-chat-react-native',
375381
}),
376382
);
377383

384+
await act(async () => {
385+
const text = 'Awesome repository https://github.com/GetStream/stream-chat-react-native';
386+
await channel.messageComposer.textComposer.handleChange({
387+
selection: {
388+
end: text.length,
389+
start: text.length,
390+
},
391+
text,
392+
});
393+
});
394+
378395
act(() => {
379396
fireEvent(queryByTestId('send-button'), 'onPress');
380397
});

package/src/contexts/messageInputContext/MessageInputContext.tsx

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
PollContentProps,
3737
StopMessageStreamingButtonProps,
3838
} from '../../components';
39+
import { parseLinksFromText } from '../../components/Message/MessageSimple/utils/parseLinks';
3940
import type { AttachButtonProps } from '../../components/MessageInput/AttachButton';
4041
import type { CommandsButtonProps } from '../../components/MessageInput/CommandsButton';
4142
import type { AttachmentUploadProgressIndicatorProps } from '../../components/MessageInput/components/AttachmentPreview/AttachmentUploadProgressIndicator';
@@ -75,6 +76,7 @@ import {
7576
import { useChannelContext } from '../channelContext/ChannelContext';
7677
import { useChatContext } from '../chatContext/ChatContext';
7778
import { useMessageComposerAPIContext } from '../messageComposerContext/MessageComposerAPIContext';
79+
import { useOwnCapabilitiesContext } from '../ownCapabilitiesContext/OwnCapabilitiesContext';
7880
import { useThreadContext } from '../threadContext/ThreadContext';
7981
import { useTranslationContext } from '../translationContext/TranslationContext';
8082
import { DEFAULT_BASE_CONTEXT_VALUE } from '../utils/defaultBaseContextValue';
@@ -183,9 +185,12 @@ export type InputMessageInputContextValue = {
183185
AutoCompleteSuggestionList: React.ComponentType<AutoCompleteSuggestionListProps>;
184186

185187
/**
186-
* Custom UI component to render [draggable handle](https://github.com/GetStream/stream-chat-react-native/blob/main/screenshots/docs/1.png) of attachment picker.
188+
* Custom UI component to render [draggable
189+
* handle](https://github.com/GetStream/stream-chat-react-native/blob/main/screenshots/docs/1.png) of attachment
190+
* picker.
187191
*
188-
* **Default** [AttachmentPickerBottomSheetHandle](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/AttachmentPickerBottomSheetHandle.tsx)
192+
* **Default**
193+
* [AttachmentPickerBottomSheetHandle](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/AttachmentPickerBottomSheetHandle.tsx)
189194
*/
190195
AttachmentPickerBottomSheetHandle: React.FC<BottomSheetHandleProps>;
191196
/**
@@ -203,7 +208,8 @@ export type InputMessageInputContextValue = {
203208
/**
204209
* Custom UI component for AttachmentPickerSelectionBar
205210
*
206-
* **Default: ** [AttachmentPickerSelectionBar](https://github.com/GetStream/stream-chat-react-native/blob/develop/package/src/components/AttachmentPicker/components/AttachmentPickerSelectionBar.tsx)
211+
* **Default: **
212+
* [AttachmentPickerSelectionBar](https://github.com/GetStream/stream-chat-react-native/blob/develop/package/src/components/AttachmentPicker/components/AttachmentPickerSelectionBar.tsx)
207213
*/
208214
AttachmentPickerSelectionBar: React.ComponentType;
209215
/**
@@ -214,33 +220,41 @@ export type InputMessageInputContextValue = {
214220
attachmentSelectionBarHeight: number;
215221

216222
/**
217-
* Custom UI component for [camera selector icon](https://github.com/GetStream/stream-chat-react-native/blob/main/screenshots/docs/1.png)
223+
* Custom UI component for [camera selector
224+
* icon](https://github.com/GetStream/stream-chat-react-native/blob/main/screenshots/docs/1.png)
218225
*
219-
* **Default: ** [CameraSelectorIcon](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/CameraSelectorIcon.tsx)
226+
* **Default: **
227+
* [CameraSelectorIcon](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/CameraSelectorIcon.tsx)
220228
*/
221229
CameraSelectorIcon: React.ComponentType<AttachmentPickerIconProps>;
222230
/**
223231
* Custom UI component for the poll creation icon.
224232
*
225-
* **Default: ** [CreatePollIcon](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/CreatePollIcon.tsx)
233+
* **Default: **
234+
* [CreatePollIcon](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/CreatePollIcon.tsx)
226235
*/
227236
CreatePollIcon: React.ComponentType;
228237
/**
229-
* Custom UI component for [file selector icon](https://github.com/GetStream/stream-chat-react-native/blob/main/screenshots/docs/1.png)
238+
* Custom UI component for [file selector
239+
* icon](https://github.com/GetStream/stream-chat-react-native/blob/main/screenshots/docs/1.png)
230240
*
231-
* **Default: ** [FileSelectorIcon](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/FileSelectorIcon.tsx)
241+
* **Default: **
242+
* [FileSelectorIcon](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/FileSelectorIcon.tsx)
232243
*/
233244
FileSelectorIcon: React.ComponentType<AttachmentPickerIconProps>;
234245
/**
235-
* Custom UI component for [image selector icon](https://github.com/GetStream/stream-chat-react-native/blob/main/screenshots/docs/1.png)
246+
* Custom UI component for [image selector
247+
* icon](https://github.com/GetStream/stream-chat-react-native/blob/main/screenshots/docs/1.png)
236248
*
237-
* **Default: ** [ImageSelectorIcon](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/ImageSelectorIcon.tsx)
249+
* **Default: **
250+
* [ImageSelectorIcon](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/ImageSelectorIcon.tsx)
238251
*/
239252
ImageSelectorIcon: React.ComponentType<AttachmentPickerIconProps>;
240253
/**
241254
* Custom UI component for Android's video recorder selector icon.
242255
*
243-
* **Default: ** [VideoRecorderSelectorIcon](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/VideoRecorderSelectorIcon.tsx)
256+
* **Default: **
257+
* [VideoRecorderSelectorIcon](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/AttachmentPicker/components/VideoRecorderSelectorIcon.tsx)
244258
*/
245259
VideoRecorderSelectorIcon: React.ComponentType<AttachmentPickerIconProps>;
246260
AudioAttachmentUploadPreview: React.ComponentType<AudioAttachmentUploadPreviewProps>;
@@ -428,6 +442,7 @@ export const MessageInputProvider = ({
428442
const { closePicker, openPicker, selectedPicker, setSelectedPicker } =
429443
useAttachmentPickerContext();
430444
const { client, enableOfflineSupport } = useChatContext();
445+
const channelCapabilities = useOwnCapabilitiesContext();
431446

432447
const { uploadAbortControllerRef } = useChannelContext();
433448
const { clearEditingState } = useMessageComposerAPIContext();
@@ -456,7 +471,8 @@ export const MessageInputProvider = ({
456471

457472
/**
458473
* These are the RN SDK specific middlewares that are added to the message composer to provide the default behaviour.
459-
* TODO: Discuss and decide if we provide them by default in the SDK or leave it to the user to add them if they want the feature.
474+
* TODO: Discuss and decide if we provide them by default in the SDK or leave it to the user to add them if they want
475+
* the feature.
460476
*/
461477
useEffect(() => {
462478
if (value.doFileUploadRequest) {
@@ -602,7 +618,15 @@ export const MessageInputProvider = ({
602618

603619
const composition = await messageComposer.compose();
604620
if (!composition || !composition.message) return;
621+
605622
const { localMessage, message, sendOptions } = composition;
623+
const linkInfos = parseLinksFromText(localMessage.text);
624+
625+
if (!channelCapabilities.sendLinks && linkInfos.length > 0) {
626+
Alert.alert(t('Links are disabled'), t('Sending links is not allowed in this conversation'));
627+
628+
return;
629+
}
606630

607631
if (editedMessage && editedMessage.type !== 'error') {
608632
try {

0 commit comments

Comments
 (0)