|
1 | 1 | import type { Event, StreamChat } from 'stream-chat'; |
2 | 2 |
|
3 | 3 | import { deleteChannel } from '../../../store/apis/deleteChannel'; |
4 | | -import { deleteMember } from '../../../store/apis/deleteMember'; |
| 4 | +// import { deleteMember } from '../../../store/apis/deleteMember'; |
5 | 5 | import { deleteMessagesForChannel } from '../../../store/apis/deleteMessagesForChannel'; |
6 | | -import { updateMessage } from '../../../store/apis/updateMessage'; |
| 6 | +// import { updateMessage } from '../../../store/apis/updateMessage'; |
7 | 7 | import { upsertChannelData } from '../../../store/apis/upsertChannelData'; |
8 | | -import { upsertChannelDataFromChannel } from '../../../store/apis/upsertChannelDataFromChannel'; |
9 | | -import { upsertMembers } from '../../../store/apis/upsertMembers'; |
| 8 | +// import { upsertChannelDataFromChannel } from '../../../store/apis/upsertChannelDataFromChannel'; |
| 9 | +// import { upsertMembers } from '../../../store/apis/upsertMembers'; |
10 | 10 | // import { upsertReads } from '../../../store/apis/upsertReads'; |
11 | | -import { createSelectQuery } from '../../../store/sqlite-utils/createSelectQuery'; |
12 | | -import { SqliteClient } from '../../../store/SqliteClient'; |
13 | | -import { PreparedQueries } from '../../../store/types'; |
| 11 | +// import { createSelectQuery } from '../../../store/sqlite-utils/createSelectQuery'; |
| 12 | +// import { SqliteClient } from '../../../store/SqliteClient'; |
| 13 | +// import { PreparedQueries } from '../../../store/types'; |
14 | 14 |
|
15 | 15 | export const handleEventToSyncDB = async (event: Event, client: StreamChat, flush?: boolean) => { |
16 | 16 | const { type } = event; |
| 17 | + console.log('client', !!client); |
17 | 18 |
|
18 | 19 | // This function is used to guard the queries that require channel to be present in the db first |
19 | 20 | // If channel is not present in the db, we first fetch the channel data from the channel object |
20 | 21 | // and then add the queries with a channel create query first |
21 | | - const queriesWithChannelGuard = async ( |
22 | | - createQueries: (flushOverride?: boolean) => Promise<PreparedQueries[]>, |
23 | | - ) => { |
24 | | - const cid = event.cid || event.channel?.cid; |
25 | | - |
26 | | - if (!cid) { |
27 | | - return await createQueries(flush); |
28 | | - } |
29 | | - const channels = await SqliteClient.executeSql.apply( |
30 | | - null, |
31 | | - createSelectQuery('channels', ['cid'], { |
32 | | - cid, |
33 | | - }), |
34 | | - ); |
35 | | - // a channel is not present in the db, we first fetch the channel data from the channel object. |
36 | | - // this can happen for example when a message.new event is received for a channel that is not in the db due to a channel being hidden. |
37 | | - if (channels.length === 0) { |
38 | | - const channel = |
39 | | - event.channel_type && event.channel_id |
40 | | - ? client.channel(event.channel_type, event.channel_id) |
41 | | - : undefined; |
42 | | - if (channel && channel.initialized && !channel.disconnected) { |
43 | | - const channelQuery = await upsertChannelDataFromChannel({ |
44 | | - channel, |
45 | | - flush, |
46 | | - }); |
47 | | - if (channelQuery) { |
48 | | - const createdQueries = await createQueries(false); |
49 | | - const newQueries = [...channelQuery, ...createdQueries]; |
50 | | - if (flush !== false) { |
51 | | - await SqliteClient.executeSqlBatch(newQueries); |
52 | | - } |
53 | | - return newQueries; |
54 | | - } else { |
55 | | - console.warn( |
56 | | - `Couldnt create channel queries on ${type} event for an initialized channel that is not in DB, skipping event`, |
57 | | - { event }, |
58 | | - ); |
59 | | - return []; |
60 | | - } |
61 | | - } else { |
62 | | - console.warn( |
63 | | - `Received ${type} event for a non initialized channel that is not in DB, skipping event`, |
64 | | - { event }, |
65 | | - ); |
66 | | - return []; |
67 | | - } |
68 | | - } |
69 | | - return createQueries(flush); |
70 | | - }; |
| 22 | + // const queriesWithChannelGuard = async ( |
| 23 | + // createQueries: (flushOverride?: boolean) => Promise<PreparedQueries[]>, |
| 24 | + // ) => { |
| 25 | + // const cid = event.cid || event.channel?.cid; |
| 26 | + // |
| 27 | + // if (!cid) { |
| 28 | + // return await createQueries(flush); |
| 29 | + // } |
| 30 | + // const channels = await SqliteClient.executeSql.apply( |
| 31 | + // null, |
| 32 | + // createSelectQuery('channels', ['cid'], { |
| 33 | + // cid, |
| 34 | + // }), |
| 35 | + // ); |
| 36 | + // // a channel is not present in the db, we first fetch the channel data from the channel object. |
| 37 | + // // this can happen for example when a message.new event is received for a channel that is not in the db due to a channel being hidden. |
| 38 | + // if (channels.length === 0) { |
| 39 | + // const channel = |
| 40 | + // event.channel_type && event.channel_id |
| 41 | + // ? client.channel(event.channel_type, event.channel_id) |
| 42 | + // : undefined; |
| 43 | + // if (channel && channel.initialized && !channel.disconnected) { |
| 44 | + // const channelQuery = await upsertChannelDataFromChannel({ |
| 45 | + // channel, |
| 46 | + // flush, |
| 47 | + // }); |
| 48 | + // if (channelQuery) { |
| 49 | + // const createdQueries = await createQueries(false); |
| 50 | + // const newQueries = [...channelQuery, ...createdQueries]; |
| 51 | + // if (flush !== false) { |
| 52 | + // await SqliteClient.executeSqlBatch(newQueries); |
| 53 | + // } |
| 54 | + // return newQueries; |
| 55 | + // } else { |
| 56 | + // console.warn( |
| 57 | + // `Couldnt create channel queries on ${type} event for an initialized channel that is not in DB, skipping event`, |
| 58 | + // { event }, |
| 59 | + // ); |
| 60 | + // return []; |
| 61 | + // } |
| 62 | + // } else { |
| 63 | + // console.warn( |
| 64 | + // `Received ${type} event for a non initialized channel that is not in DB, skipping event`, |
| 65 | + // { event }, |
| 66 | + // ); |
| 67 | + // return []; |
| 68 | + // } |
| 69 | + // } |
| 70 | + // return createQueries(flush); |
| 71 | + // }; |
71 | 72 |
|
72 | 73 | // if (type === 'message.read' || type === 'notification.mark_read') { |
73 | 74 | // const cid = event.cid; |
@@ -146,19 +147,19 @@ export const handleEventToSyncDB = async (event: Event, client: StreamChat, flus |
146 | 147 | // } |
147 | 148 | // } |
148 | 149 |
|
149 | | - if (type === 'message.updated') { |
150 | | - const message = event.message; |
151 | | - if (message && !message.parent_id) { |
152 | | - // Update only if it exists, otherwise event could be related |
153 | | - // to a message which is not in database. |
154 | | - return await queriesWithChannelGuard((flushOverride) => |
155 | | - updateMessage({ |
156 | | - flush: flushOverride, |
157 | | - message, |
158 | | - }), |
159 | | - ); |
160 | | - } |
161 | | - } |
| 150 | + // if (type === 'message.updated') { |
| 151 | + // const message = event.message; |
| 152 | + // if (message && !message.parent_id) { |
| 153 | + // // Update only if it exists, otherwise event could be related |
| 154 | + // // to a message which is not in database. |
| 155 | + // return await queriesWithChannelGuard((flushOverride) => |
| 156 | + // updateMessage({ |
| 157 | + // flush: flushOverride, |
| 158 | + // message, |
| 159 | + // }), |
| 160 | + // ); |
| 161 | + // } |
| 162 | + // } |
162 | 163 |
|
163 | 164 | // if (type === 'reaction.updated') { |
164 | 165 | // const message = event.message; |
@@ -224,33 +225,33 @@ export const handleEventToSyncDB = async (event: Event, client: StreamChat, flus |
224 | 225 | } |
225 | 226 | } |
226 | 227 |
|
227 | | - if (type === 'member.added' || type === 'member.updated') { |
228 | | - const member = event.member; |
229 | | - const cid = event.cid; |
230 | | - if (member && cid) { |
231 | | - return await queriesWithChannelGuard((flushOverride) => |
232 | | - upsertMembers({ |
233 | | - cid, |
234 | | - flush: flushOverride, |
235 | | - members: [member], |
236 | | - }), |
237 | | - ); |
238 | | - } |
239 | | - } |
240 | | - |
241 | | - if (type === 'member.removed') { |
242 | | - const member = event.member; |
243 | | - const cid = event.cid; |
244 | | - if (member && cid) { |
245 | | - return await queriesWithChannelGuard((flushOverride) => |
246 | | - deleteMember({ |
247 | | - cid, |
248 | | - flush: flushOverride, |
249 | | - member, |
250 | | - }), |
251 | | - ); |
252 | | - } |
253 | | - } |
| 228 | + // if (type === 'member.added' || type === 'member.updated') { |
| 229 | + // const member = event.member; |
| 230 | + // const cid = event.cid; |
| 231 | + // if (member && cid) { |
| 232 | + // return await queriesWithChannelGuard((flushOverride) => |
| 233 | + // upsertMembers({ |
| 234 | + // cid, |
| 235 | + // flush: flushOverride, |
| 236 | + // members: [member], |
| 237 | + // }), |
| 238 | + // ); |
| 239 | + // } |
| 240 | + // } |
| 241 | + // |
| 242 | + // if (type === 'member.removed') { |
| 243 | + // const member = event.member; |
| 244 | + // const cid = event.cid; |
| 245 | + // if (member && cid) { |
| 246 | + // return await queriesWithChannelGuard((flushOverride) => |
| 247 | + // deleteMember({ |
| 248 | + // cid, |
| 249 | + // flush: flushOverride, |
| 250 | + // member, |
| 251 | + // }), |
| 252 | + // ); |
| 253 | + // } |
| 254 | + // } |
254 | 255 |
|
255 | 256 | return []; |
256 | 257 | }; |
0 commit comments