Skip to content

Commit e98cd59

Browse files
committed
fix: properly resolve own_votes and latest_answers
1 parent 3926c11 commit e98cd59

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

package/src/components/Chat/hooks/handleEventToSyncDB.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,14 @@ export const handleEventToSyncDB = <
233233
'poll.vote_removed',
234234
].includes(type)
235235
) {
236-
const { poll, poll_vote } = event;
236+
const { poll, poll_vote, type } = event;
237237
if (poll) {
238238
return updatePollMessage({
239-
client,
239+
eventType: type,
240240
flush,
241241
poll,
242242
poll_vote,
243+
userID: client?.userID || '',
243244
});
244245
}
245246
}

package/src/store/apis/updatePollMessage.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isVoteAnswer, PollAnswer, PollResponse, PollVote, StreamChat } from 'stream-chat';
1+
import { isVoteAnswer, PollAnswer, PollResponse, PollVote } from 'stream-chat';
22

33
import { DefaultStreamChatGenerics } from '../../types/types';
44
import { mapPollToStorable } from '../mappers/mapPollToStorable';
@@ -11,13 +11,15 @@ import type { PreparedQueries } from '../types';
1111
export const updatePollMessage = <
1212
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
1313
>({
14-
client,
14+
eventType,
1515
flush = true,
1616
poll,
1717
poll_vote,
18+
userID,
1819
}: {
19-
client: StreamChat<StreamChatGenerics>;
20+
eventType: string;
2021
poll: PollResponse<StreamChatGenerics>;
22+
userID: string;
2123
flush?: boolean;
2224
poll_vote?: PollVote<StreamChatGenerics> | PollAnswer<StreamChatGenerics>;
2325
}) => {
@@ -33,21 +35,37 @@ export const updatePollMessage = <
3335
for (const pollFromDB of pollsFromDB) {
3436
const serializedPoll = mapStorableToPoll(pollFromDB);
3537
const { latest_answers = [], own_votes = [] } = serializedPoll;
36-
const newOwnVotes =
37-
poll_vote && poll_vote.user?.id === client.userID
38-
? [poll_vote, ...own_votes.filter((vote) => vote.id !== poll_vote.id)]
39-
: own_votes;
40-
const newLatestAnswers =
41-
poll_vote && isVoteAnswer(poll_vote)
42-
? [poll_vote, ...latest_answers.filter((answer) => answer.id !== poll_vote?.id)]
43-
: latest_answers;
38+
console.log(eventType);
39+
let newOwnVotes = own_votes;
40+
if (poll_vote && poll_vote.user?.id === userID) {
41+
newOwnVotes =
42+
eventType === 'poll.vote_removed'
43+
? newOwnVotes.filter((vote) => vote.id !== poll_vote.id)
44+
: [poll_vote, ...newOwnVotes.filter((vote) => vote.id !== poll_vote.id)];
45+
}
46+
let newLatestAnswers = latest_answers;
47+
if (poll_vote && isVoteAnswer(poll_vote)) {
48+
newLatestAnswers =
49+
eventType === 'poll.vote_removed'
50+
? newLatestAnswers.filter((answer) => answer.id !== poll_vote?.id)
51+
: [poll_vote, ...newLatestAnswers.filter((answer) => answer.id !== poll_vote?.id)];
52+
}
53+
// const newOwnVotes =
54+
// poll_vote && poll_vote.user?.id === userID
55+
// ? [poll_vote, ...own_votes.filter((vote) => vote.id !== poll_vote.id)]
56+
// : own_votes;
57+
// const newLatestAnswers =
58+
// poll_vote && isVoteAnswer(poll_vote)
59+
// ? [poll_vote, ...latest_answers.filter((answer) => answer.id !== poll_vote?.id)]
60+
// : latest_answers;
61+
4462
const storablePoll = mapPollToStorable({
4563
...poll,
4664
latest_answers: newLatestAnswers,
4765
own_votes: newOwnVotes,
4866
});
4967

50-
console.log('STORABLE POLL: ', storablePoll);
68+
console.log('STORABLE POLL: ', newOwnVotes);
5169

5270
queries.push(
5371
createUpdateQuery('poll', storablePoll, {

package/src/store/mappers/mapMessageToStorable.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import type { FormatMessageResponse, MessageResponse } from 'stream-chat';
22

33
import { mapDateTimeToStorable } from './mapDateTimeToStorable';
44

5-
import { mapPollToStorable } from './mapPollToStorable';
6-
75
import type { TableRow } from '../types';
86

97
export const mapMessageToStorable = (
@@ -20,6 +18,7 @@ export const mapMessageToStorable = (
2018
message_text_updated_at,
2119
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2220
own_reactions,
21+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2322
poll,
2423
poll_id,
2524
reaction_groups,
@@ -30,8 +29,6 @@ export const mapMessageToStorable = (
3029
...extraData
3130
} = message;
3231

33-
const pollInMessage = poll && mapPollToStorable(poll);
34-
3532
return {
3633
attachments: JSON.stringify(attachments),
3734
cid: cid || '',
@@ -46,6 +43,5 @@ export const mapMessageToStorable = (
4643
type,
4744
updatedAt: mapDateTimeToStorable(updated_at),
4845
userId: user?.id,
49-
...(pollInMessage ? { poll: pollInMessage } : {}),
5046
};
5147
};

package/src/store/schema.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ export const tables: Tables = {
103103
extraData: 'TEXT',
104104
id: 'TEXT',
105105
messageTextUpdatedAt: 'TEXT',
106-
poll: 'TEXT',
107106
poll_id: 'TEXT',
108107
reactionGroups: 'TEXT',
109108
text: "TEXT DEFAULT ''",
@@ -298,7 +297,6 @@ export type Schema = {
298297
extraData: string;
299298
id: string;
300299
messageTextUpdatedAt: string;
301-
poll: string;
302300
poll_id: string;
303301
reactionGroups: string;
304302
type: MessageLabel;

0 commit comments

Comments
 (0)