Skip to content

Commit b426af4

Browse files
committed
feat: add delete reactions and fix sending reactions
1 parent ff0c911 commit b426af4

File tree

5 files changed

+72
-80
lines changed

5 files changed

+72
-80
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
14791479
];
14801480

14811481
if (enableOfflineSupport) {
1482-
addReactionToLocalState({
1482+
await addReactionToLocalState({
14831483
channel,
14841484
enforceUniqueReaction,
14851485
messageId,
@@ -1551,29 +1551,18 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
15511551

15521552
const payload: Parameters<ChannelClass['deleteReaction']> = [messageId, type];
15531553

1554-
if (!enableOfflineSupport) {
1555-
await channel.deleteReaction(...payload);
1556-
return;
1557-
}
1558-
1559-
removeReactionFromLocalState({
1560-
channel,
1561-
messageId,
1562-
reactionType: type,
1563-
user: client.user,
1564-
});
1554+
if (enableOfflineSupport) {
1555+
removeReactionFromLocalState({
1556+
channel,
1557+
messageId,
1558+
reactionType: type,
1559+
user: client.user,
1560+
});
15651561

1566-
copyMessagesStateFromChannel(channel);
1562+
copyMessagesStateFromChannel(channel);
1563+
}
15671564

1568-
await client.offlineDb.syncManager.queueTask({
1569-
task: {
1570-
channelId: channel.id,
1571-
channelType: channel.type,
1572-
messageId,
1573-
payload,
1574-
type: 'delete-reaction',
1575-
},
1576-
});
1565+
await channel.deleteReaction(...payload);
15771566
};
15781567

15791568
/**

package/src/store/OfflineDB.ts

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {
2-
AbstractOfflineDB,
1+
import { AbstractOfflineDB, StreamChat } from 'stream-chat';
2+
import type {
3+
GetChannelsForQueryType,
4+
GetChannelsType,
35
GetLastSyncedAtType,
4-
type ReactionResponse,
5-
StreamChat,
6+
UpsertReactionType,
67
UpsertUserSyncStatusType,
78
} from 'stream-chat';
8-
import type { GetChannelsForQueryType, GetChannelsType, UpsertReactionType } from 'stream-chat';
99

1010
import * as api from './apis';
1111
import { SqliteClient } from './SqliteClient';
@@ -36,47 +36,17 @@ export class OfflineDB extends AbstractOfflineDB {
3636
upsertUserSyncStatus = ({ userId, lastSyncedAt }: UpsertUserSyncStatusType) =>
3737
api.upsertUserSyncStatus({ currentUserId: userId, lastSyncedAt });
3838

39-
upsertReaction = async ({
40-
channel,
41-
enforceUniqueReaction,
42-
messageId,
43-
reactionType,
44-
user,
45-
}: UpsertReactionType) => {
46-
const message = channel.state.messages.find(({ id }) => id === messageId);
47-
48-
if (!message) {
49-
return;
50-
}
51-
52-
const hasOwnReaction = message.own_reactions && message.own_reactions.length > 0;
53-
54-
const reaction: ReactionResponse = {
55-
created_at: new Date().toISOString(),
56-
message_id: messageId,
57-
type: reactionType,
58-
updated_at: new Date().toISOString(),
59-
user,
60-
user_id: user?.id,
61-
};
62-
63-
if (enforceUniqueReaction && hasOwnReaction) {
64-
await api.updateReaction({
65-
message,
66-
reaction,
67-
});
68-
} else {
69-
await api.insertReaction({
70-
message,
71-
reaction,
72-
});
73-
}
74-
};
39+
/* eslint-disable @typescript-eslint/no-unused-vars */
40+
// @ts-expect-error Not yet impelmented
41+
upsertReaction = async (options: UpsertReactionType) => {};
42+
/* eslint-enable */
7543

7644
addPendingTask = api.addPendingTask;
7745

7846
deletePendingTask = api.deletePendingTask;
7947

48+
deleteReaction = api.deleteReaction;
49+
8050
getPendingTasks = api.getPendingTasks;
8151

8252
resetDB = SqliteClient.resetDB;
Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
1+
import { Channel } from 'stream-chat';
2+
13
import { createDeleteQuery } from '../sqlite-utils/createDeleteQuery';
4+
import { createUpdateQuery } from '../sqlite-utils/createUpdateQuery';
25
import { SqliteClient } from '../SqliteClient';
6+
import { PreparedQueries } from '../types';
37

48
export const deleteReaction = async ({
9+
channel,
510
flush = true,
611
messageId,
712
reactionType,
813
userId,
914
}: {
15+
channel: Channel;
1016
messageId: string;
1117
reactionType: string;
1218
userId: string;
1319
flush?: boolean;
1420
}) => {
15-
const query = createDeleteQuery('reactions', {
16-
messageId,
17-
type: reactionType,
18-
userId,
19-
});
21+
const queries: PreparedQueries[] = [];
22+
23+
const message = channel.state.messages.find(({ id }) => id === messageId);
24+
25+
if (!message) {
26+
return;
27+
}
28+
29+
queries.push(
30+
createDeleteQuery('reactions', {
31+
messageId,
32+
type: reactionType,
33+
userId,
34+
}),
35+
);
36+
37+
const stringifiedNewReactionGroups = JSON.stringify(message.reaction_groups);
38+
39+
queries.push(
40+
createUpdateQuery(
41+
'messages',
42+
{
43+
reactionGroups: stringifiedNewReactionGroups,
44+
},
45+
{ id: message.id },
46+
),
47+
);
2048

2149
SqliteClient.logger?.('info', 'deleteReaction', {
2250
messageId,
@@ -25,8 +53,8 @@ export const deleteReaction = async ({
2553
});
2654

2755
if (flush) {
28-
await SqliteClient.executeSql.apply(null, query);
56+
await SqliteClient.executeSqlBatch(queries);
2957
}
3058

31-
return [query];
59+
return queries;
3260
};

package/src/utils/addReactionToLocalState.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { Channel, ReactionResponse, UserResponse } from 'stream-chat';
22

3-
export const addReactionToLocalState = ({
3+
import { insertReaction, updateReaction } from '../store/apis';
4+
5+
export const addReactionToLocalState = async ({
46
channel,
57
enforceUniqueReaction,
68
messageId,
@@ -28,6 +30,7 @@ export const addReactionToLocalState = ({
2830
user_id: user?.id,
2931
};
3032

33+
const hasOwnReaction = message.own_reactions && message.own_reactions.length > 0;
3134
if (!message.own_reactions) {
3235
message.own_reactions = [];
3336
}
@@ -88,5 +91,15 @@ export const addReactionToLocalState = ({
8891
message.own_reactions = [...message.own_reactions, reaction];
8992
message.latest_reactions = [...message.latest_reactions, reaction];
9093

91-
return { message, reaction };
94+
if (enforceUniqueReaction && hasOwnReaction) {
95+
await updateReaction({
96+
message,
97+
reaction,
98+
});
99+
} else {
100+
await insertReaction({
101+
message,
102+
reaction,
103+
});
104+
}
92105
};

package/src/utils/removeReactionFromLocalState.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import type { Channel, UserResponse } from 'stream-chat';
22

3-
import { deleteReaction } from '../store/apis/deleteReaction';
4-
53
export const removeReactionFromLocalState = ({
64
channel,
75
messageId,
@@ -48,10 +46,4 @@ export const removeReactionFromLocalState = ({
4846
delete message.reaction_groups[reactionType];
4947
}
5048
}
51-
52-
deleteReaction({
53-
messageId,
54-
reactionType,
55-
userId: user.id,
56-
});
5749
};

0 commit comments

Comments
 (0)