Skip to content

Commit

Permalink
feat: require Channel instance as the first argument to doSendMessage…
Browse files Browse the repository at this point in the history
…Request (GetStream#2171)

Avoid forcing integrators to recreate `Channel` instance from CID inside
`doSendMessageRequest` and provide it directly as the first argument.

BREAKING CHANGE: the first argument to `doSendMessageRequest` is now Channel instance instead of `Channel.cid`
  • Loading branch information
MartinCupela authored and arnautov-anton committed Nov 27, 2023
1 parent e8047f2 commit 2a06b88
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
id: minor-breaking-changes-v11
sidebar_position: 4
title: Minor breaking changes 11.0.0
keywords: [migration guide, upgrade, breaking changes, v11]
---

### Channel instance as a first argument to doSendMessageRequest

The `doSendMessageRequest` will from now on be passed the `Channel` instance instead of its CID to avoid forcing the developers to recreate a reference to the `Channel` instance inside the `doSendMessageRequest` function. The developers should adjust their implementation of `doSendMessageRequest` to call directly `await channel.sendMessage(messageData, options)`:

```ts
import { ChannelProps } from 'stream-chat-react';

const doSendMessageRequest: ChannelProps['doSendMessageRequest'] = async (channel, messageData, options) => {
// optional custom logic
await channel.sendMessage(messageData, options);
// optional custom logic
}
```
4 changes: 2 additions & 2 deletions src/components/Channel/Channel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export type ChannelProps<
) => Promise<MessageResponse<StreamChatGenerics>> | void;
/** Custom action handler to override the default `channel.sendMessage` request function (advanced usage only) */
doSendMessageRequest?: (
channelId: string,
channel: StreamChannel<StreamChatGenerics>,
message: Message<StreamChatGenerics>,
options?: SendMessageOptions,
) => ReturnType<StreamChannel<StreamChatGenerics>['sendMessage']> | void;
Expand Down Expand Up @@ -771,7 +771,7 @@ const ChannelInner = <
let messageResponse: void | SendMessageAPIResponse<StreamChatGenerics>;

if (doSendMessageRequest) {
messageResponse = await doSendMessageRequest(channel.cid, messageData, options);
messageResponse = await doSendMessageRequest(channel, messageData, options);
} else {
messageResponse = await channel.sendMessage(messageData, options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Channel/__tests__/Channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ describe('Channel', () => {

await waitFor(() =>
expect(doSendMessageRequest).toHaveBeenCalledWith(
channel.cid,
channel,
expect.objectContaining(message),
undefined,
),
Expand Down
15 changes: 8 additions & 7 deletions src/components/MessageInput/__tests__/MessageInput.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ function axeNoViolations(container) {
await act(() => submit());

expect(submitMock).toHaveBeenCalledWith(
channel.cid,
channel,
expect.objectContaining({
text: messageText,
}),
Expand Down Expand Up @@ -697,9 +697,10 @@ function axeNoViolations(container) {
await act(() => submit());

await waitFor(() => {
const calledMock = componentName === 'EditMessageForm' ? editMock : submitMock;
const isEdit = componentName === 'EditMessageForm';
const calledMock = isEdit ? editMock : submitMock;
expect(calledMock).toHaveBeenCalledWith(
expect.stringMatching(/.+:.+/),
isEdit ? channel.cid : channel,
expect.objectContaining(customMessageData),
undefined,
);
Expand Down Expand Up @@ -765,7 +766,7 @@ function axeNoViolations(container) {
await act(() => submit());

expect(submitMock).toHaveBeenCalledWith(
channel.cid,
channel,
expect.objectContaining({
attachments: expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -799,7 +800,7 @@ function axeNoViolations(container) {
await act(() => submit());

expect(submitMock).toHaveBeenCalledWith(
channel.cid,
channel,
expect.objectContaining({
attachments: expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -835,7 +836,7 @@ function axeNoViolations(container) {
await act(() => submit());

expect(submitMock).toHaveBeenCalledWith(
channel.cid,
channel,
expect.objectContaining({
attachments: expect.arrayContaining([
expect.objectContaining({
Expand Down Expand Up @@ -1040,7 +1041,7 @@ function axeNoViolations(container) {
await act(() => submit());

expect(submitMock).toHaveBeenCalledWith(
channel.cid,
channel,
expect.objectContaining({
mentioned_users: expect.arrayContaining([mentionId]),
}),
Expand Down

0 comments on commit 2a06b88

Please sign in to comment.