Skip to content

Commit 87cf045

Browse files
authored
fix: removing a reaction outside of the latest reactions causes to add it (#2028)
1 parent ff51c76 commit 87cf045

File tree

6 files changed

+36
-55
lines changed

6 files changed

+36
-55
lines changed

examples/ExpoMessaging/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6670,10 +6670,10 @@ [email protected]:
66706670
version "0.0.0"
66716671
uid ""
66726672

6673-
stream-chat-react-native-core@5.11.2:
6674-
version "5.11.2"
6675-
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-5.11.2.tgz#fc136917f481e44151819820fe475b49c45b06e3"
6676-
integrity sha512-tWEBxstr27znQMuAhxNucBrk/WfCPLUAwmSodkVKKtwA+2Roo8dn/6FVYNLoq6TgGVKbGvtTs+ZOaUsAxFlnkQ==
6673+
stream-chat-react-native-core@5.12.0:
6674+
version "5.12.0"
6675+
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-5.12.0.tgz#84fed9d65a24b4a92daa6793ac218078d56abdf1"
6676+
integrity sha512-CJ5gdf4mT+io44cWXkT2wy1kLnyzY05AFdGe2i+KBk4fqubpRpl1MFbgvnAjkcrwvMHnvh131tMxWzOlKkqkkw==
66776677
dependencies:
66786678
"@babel/runtime" "^7.12.5"
66796679
"@gorhom/bottom-sheet" "4.4.5"

examples/SampleApp/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7499,10 +7499,10 @@ [email protected]:
74997499
resolved "https://registry.yarnpkg.com/stream-buffers/-/stream-buffers-2.2.0.tgz#91d5f5130d1cef96dcfa7f726945188741d09ee4"
75007500
integrity sha512-uyQK/mx5QjHun80FLJTfaWE7JtwfRMKBLkMne6udYOmvH0CawotVa7TfgYHzAnpphn4+TweIx1QKMnRIbipmUg==
75017501

7502-
stream-chat-react-native-core@5.11.2:
7503-
version "5.11.2"
7504-
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-5.11.2.tgz#fc136917f481e44151819820fe475b49c45b06e3"
7505-
integrity sha512-tWEBxstr27znQMuAhxNucBrk/WfCPLUAwmSodkVKKtwA+2Roo8dn/6FVYNLoq6TgGVKbGvtTs+ZOaUsAxFlnkQ==
7502+
stream-chat-react-native-core@5.12.0:
7503+
version "5.12.0"
7504+
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-5.12.0.tgz#84fed9d65a24b4a92daa6793ac218078d56abdf1"
7505+
integrity sha512-CJ5gdf4mT+io44cWXkT2wy1kLnyzY05AFdGe2i+KBk4fqubpRpl1MFbgvnAjkcrwvMHnvh131tMxWzOlKkqkkw==
75067506
dependencies:
75077507
"@babel/runtime" "^7.12.5"
75087508
"@gorhom/bottom-sheet" "4.4.5"

examples/TypeScriptMessaging/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7114,10 +7114,10 @@ statuses@~1.5.0:
71147114
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
71157115
integrity sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==
71167116

7117-
stream-chat-react-native-core@5.11.2:
7118-
version "5.11.2"
7119-
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-5.11.2.tgz#fc136917f481e44151819820fe475b49c45b06e3"
7120-
integrity sha512-tWEBxstr27znQMuAhxNucBrk/WfCPLUAwmSodkVKKtwA+2Roo8dn/6FVYNLoq6TgGVKbGvtTs+ZOaUsAxFlnkQ==
7117+
stream-chat-react-native-core@5.12.0:
7118+
version "5.12.0"
7119+
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-5.12.0.tgz#84fed9d65a24b4a92daa6793ac218078d56abdf1"
7120+
integrity sha512-CJ5gdf4mT+io44cWXkT2wy1kLnyzY05AFdGe2i+KBk4fqubpRpl1MFbgvnAjkcrwvMHnvh131tMxWzOlKkqkkw==
71217121
dependencies:
71227122
"@babel/runtime" "^7.12.5"
71237123
"@gorhom/bottom-sheet" "4.4.5"

package/expo-package/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3006,10 +3006,10 @@ [email protected]:
30063006
resolved "https://registry.yarnpkg.com/stream-buffers/-/stream-buffers-2.2.0.tgz#91d5f5130d1cef96dcfa7f726945188741d09ee4"
30073007
integrity sha512-uyQK/mx5QjHun80FLJTfaWE7JtwfRMKBLkMne6udYOmvH0CawotVa7TfgYHzAnpphn4+TweIx1QKMnRIbipmUg==
30083008

3009-
stream-chat-react-native-core@5.11.2:
3010-
version "5.11.2"
3011-
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-5.11.2.tgz#fc136917f481e44151819820fe475b49c45b06e3"
3012-
integrity sha512-tWEBxstr27znQMuAhxNucBrk/WfCPLUAwmSodkVKKtwA+2Roo8dn/6FVYNLoq6TgGVKbGvtTs+ZOaUsAxFlnkQ==
3009+
stream-chat-react-native-core@5.12.0:
3010+
version "5.12.0"
3011+
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-5.12.0.tgz#84fed9d65a24b4a92daa6793ac218078d56abdf1"
3012+
integrity sha512-CJ5gdf4mT+io44cWXkT2wy1kLnyzY05AFdGe2i+KBk4fqubpRpl1MFbgvnAjkcrwvMHnvh131tMxWzOlKkqkkw==
30133013
dependencies:
30143014
"@babel/runtime" "^7.12.5"
30153015
"@gorhom/bottom-sheet" "4.4.5"

package/native-package/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4515,10 +4515,10 @@ statuses@~1.5.0:
45154515
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
45164516
integrity sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==
45174517

4518-
stream-chat-react-native-core@5.11.2:
4519-
version "5.11.2"
4520-
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-5.11.2.tgz#fc136917f481e44151819820fe475b49c45b06e3"
4521-
integrity sha512-tWEBxstr27znQMuAhxNucBrk/WfCPLUAwmSodkVKKtwA+2Roo8dn/6FVYNLoq6TgGVKbGvtTs+ZOaUsAxFlnkQ==
4518+
stream-chat-react-native-core@5.12.0:
4519+
version "5.12.0"
4520+
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-5.12.0.tgz#84fed9d65a24b4a92daa6793ac218078d56abdf1"
4521+
integrity sha512-CJ5gdf4mT+io44cWXkT2wy1kLnyzY05AFdGe2i+KBk4fqubpRpl1MFbgvnAjkcrwvMHnvh131tMxWzOlKkqkkw==
45224522
dependencies:
45234523
"@babel/runtime" "^7.12.5"
45244524
"@gorhom/bottom-sheet" "4.4.5"

package/src/components/Message/hooks/useMessageActionHandlers.ts

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ import type { MessageResponse } from 'stream-chat';
22

33
import type { ChannelContextValue } from '../../../contexts/channelContext/ChannelContext';
44
import type { ChatContextValue } from '../../../contexts/chatContext/ChatContext';
5-
import type {
6-
MessageContextValue,
7-
Reactions,
8-
} from '../../../contexts/messageContext/MessageContext';
5+
import type { MessageContextValue } from '../../../contexts/messageContext/MessageContext';
96
import type { MessagesContextValue } from '../../../contexts/messagesContext/MessagesContext';
107

118
import type { DefaultStreamChatGenerics } from '../../../types/types';
@@ -22,7 +19,6 @@ export const useMessageActionHandlers = <
2219
sendReaction,
2320
setEditingState,
2421
setQuotedMessageState,
25-
supportedReactions,
2622
}: Pick<
2723
MessagesContextValue<StreamChatGenerics>,
2824
| 'sendReaction'
@@ -89,41 +85,26 @@ export const useMessageActionHandlers = <
8985
setEditingState(message);
9086
};
9187

92-
const clientId = client.userID;
93-
const isMessageTypeDeleted = message.type === 'deleted';
94-
95-
const hasReactions =
96-
!isMessageTypeDeleted && !!message.latest_reactions && message.latest_reactions.length > 0;
97-
98-
const reactions = hasReactions
99-
? supportedReactions.reduce((acc, cur) => {
100-
const reactionType = cur.type;
101-
const reactionsOfReactionType = message.latest_reactions?.filter(
102-
(reaction) => reaction.type === reactionType,
103-
);
104-
105-
if (reactionsOfReactionType?.length) {
106-
const hasOwnReaction = reactionsOfReactionType.some(
107-
(reaction) => reaction.user_id === clientId,
108-
);
109-
acc.push({ own: hasOwnReaction, type: reactionType });
110-
}
111-
112-
return acc;
113-
}, [] as Reactions)
114-
: [];
115-
11688
const handleToggleReaction = async (reactionType: string) => {
11789
const messageId = message.id;
118-
const ownReaction = !!reactions.find(
119-
(reaction) => reaction.own && reaction.type === reactionType,
120-
);
121-
90+
const own_reactions = message.own_reactions ?? [];
91+
const userExistingReaction = own_reactions.find((reaction) => {
92+
// own user should only ever contain the current user id
93+
// just in case we check to prevent bugs with message updates from breaking reactions
94+
if (reaction.user && client.userID === reaction.user.id && reaction.type === reactionType) {
95+
return true;
96+
} else if (reaction.user && client.userID !== reaction.user.id) {
97+
console.warn(
98+
`message.own_reactions contained reactions from a different user, this indicates a bug`,
99+
);
100+
}
101+
return false;
102+
});
122103
// Change reaction in local state, make API call in background, revert to old message if fails
123104
try {
124105
if (channel && messageId) {
125-
if (ownReaction) {
126-
await deleteReaction(reactionType, messageId);
106+
if (userExistingReaction) {
107+
await deleteReaction(userExistingReaction.type, messageId);
127108
} else {
128109
await sendReaction(reactionType, messageId);
129110
}

0 commit comments

Comments
 (0)