Skip to content

Commit efcd573

Browse files
authored
fix: check the send link capability before sending message (#3116)
* fix: check the send link capability before sending message * fix: formatting
1 parent 82c9c8f commit efcd573

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
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: 11 additions & 0 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';
@@ -428,6 +430,7 @@ export const MessageInputProvider = ({
428430
const { closePicker, openPicker, selectedPicker, setSelectedPicker } =
429431
useAttachmentPickerContext();
430432
const { client, enableOfflineSupport } = useChatContext();
433+
const channelCapabilities = useOwnCapabilitiesContext();
431434

432435
const { uploadAbortControllerRef } = useChannelContext();
433436
const { clearEditingState } = useMessageComposerAPIContext();
@@ -602,7 +605,15 @@ export const MessageInputProvider = ({
602605

603606
const composition = await messageComposer.compose();
604607
if (!composition || !composition.message) return;
608+
605609
const { localMessage, message, sendOptions } = composition;
610+
const linkInfos = parseLinksFromText(localMessage.text);
611+
612+
if (!channelCapabilities.sendLinks && linkInfos.length > 0) {
613+
Alert.alert(t('Links are disabled'), t('Sending links is not allowed in this conversation'));
614+
615+
return;
616+
}
606617

607618
if (editedMessage && editedMessage.type !== 'error') {
608619
try {

0 commit comments

Comments
 (0)