Skip to content

Commit 85fe679

Browse files
committed
fix: non-unique reactions and db writes
1 parent 56a3c80 commit 85fe679

File tree

2 files changed

+6
-59
lines changed

2 files changed

+6
-59
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
14841484
];
14851485

14861486
if (enableOfflineSupport) {
1487-
addReactionToLocalState({
1487+
await addReactionToLocalState({
14881488
channel,
14891489
enforceUniqueReaction,
14901490
messageId,

package/src/utils/addReactionToLocalState.ts

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -31,74 +31,21 @@ export const addReactionToLocalState = async ({
3131
};
3232

3333
const hasOwnReaction = message.own_reactions && message.own_reactions.length > 0;
34-
if (!message.own_reactions) {
35-
message.own_reactions = [];
36-
}
37-
38-
if (!message.latest_reactions) {
39-
message.latest_reactions = [];
40-
}
4134

42-
if (enforceUniqueReaction) {
43-
const currentReaction = message.own_reactions[0];
44-
message.own_reactions = [];
45-
if (!message.latest_reactions) {
46-
message.latest_reactions = [];
47-
}
48-
message.latest_reactions = message.latest_reactions.filter((r) => r.user_id !== user.id);
35+
const messageWithReaction = channel.state.addReaction(reaction, undefined, enforceUniqueReaction);
4936

50-
if (
51-
currentReaction &&
52-
message.reaction_groups &&
53-
message.reaction_groups[currentReaction.type] &&
54-
message.reaction_groups[currentReaction.type].count > 0 &&
55-
message.reaction_groups[currentReaction.type].sum_scores > 0
56-
) {
57-
message.reaction_groups[currentReaction.type].count =
58-
message.reaction_groups[currentReaction.type].count - 1;
59-
message.reaction_groups[currentReaction.type].sum_scores =
60-
message.reaction_groups[currentReaction.type].sum_scores - 1;
61-
}
62-
63-
if (!message.reaction_groups) {
64-
message.reaction_groups = {
65-
[reactionType]: {
66-
count: 1,
67-
first_reaction_at: new Date().toISOString(),
68-
last_reaction_at: new Date().toISOString(),
69-
sum_scores: 1,
70-
},
71-
};
72-
} else {
73-
if (!message.reaction_groups[reactionType]) {
74-
message.reaction_groups[reactionType] = {
75-
count: 1,
76-
first_reaction_at: new Date().toISOString(),
77-
last_reaction_at: new Date().toISOString(),
78-
sum_scores: 1,
79-
};
80-
} else {
81-
message.reaction_groups[reactionType] = {
82-
...message.reaction_groups[reactionType],
83-
count: message.reaction_groups[reactionType].count + 1,
84-
last_reaction_at: new Date().toISOString(),
85-
sum_scores: message.reaction_groups[reactionType].sum_scores + 1,
86-
};
87-
}
88-
}
37+
if (!messageWithReaction) {
38+
return;
8939
}
9040

91-
message.own_reactions = [...message.own_reactions, reaction];
92-
message.latest_reactions = [...message.latest_reactions, reaction];
93-
9441
if (enforceUniqueReaction && hasOwnReaction) {
9542
await updateReaction({
96-
message,
43+
message: messageWithReaction,
9744
reaction,
9845
});
9946
} else {
10047
await insertReaction({
101-
message,
48+
message: messageWithReaction,
10249
reaction,
10350
});
10451
}

0 commit comments

Comments
 (0)