Skip to content

Commit 32523d3

Browse files
committed
feat: move truncated event to llc
1 parent c9513f2 commit 32523d3

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { Event, StreamChat } from 'stream-chat';
1+
// import type { Event, StreamChat } from 'stream-chat';
22

33
// import { deleteChannel } from '../../../store/apis/deleteChannel';
44
// import { deleteMember } from '../../../store/apis/deleteMember';
5-
import { deleteMessagesForChannel } from '../../../store/apis/deleteMessagesForChannel';
5+
// import { deleteMessagesForChannel } from '../../../store/apis/deleteMessagesForChannel';
66
// import { updateMessage } from '../../../store/apis/updateMessage';
77
// import { upsertChannelData } from '../../../store/apis/upsertChannelData';
88
// import { upsertChannelDataFromChannel } from '../../../store/apis/upsertChannelDataFromChannel';
@@ -13,9 +13,9 @@ import { deleteMessagesForChannel } from '../../../store/apis/deleteMessagesForC
1313
// import { PreparedQueries } from '../../../store/types';
1414

1515
// eslint-disable-next-line require-await
16-
export const handleEventToSyncDB = async (event: Event, client: StreamChat, flush?: boolean) => {
17-
const { type } = event;
18-
console.log('client', !!client);
16+
export const handleEventToSyncDB = async () => {
17+
// const { type } = event;
18+
// console.log('client', !!client);
1919

2020
// This function is used to guard the queries that require channel to be present in the db first
2121
// If channel is not present in the db, we first fetch the channel data from the channel object
@@ -217,14 +217,16 @@ export const handleEventToSyncDB = async (event: Event, client: StreamChat, flus
217217
// }
218218
// }
219219

220-
if (type === 'channel.truncated') {
221-
if (event.channel) {
222-
return deleteMessagesForChannel({
223-
cid: event.channel.cid,
224-
flush,
225-
});
226-
}
227-
}
220+
// if (type === 'channel.truncated') {
221+
// console.log('TRUNCATINO: ', event);
222+
// if (event.channel) {
223+
// return deleteMessagesForChannel({
224+
// cid: event.channel.cid,
225+
// flush,
226+
// truncated_at: event.channel.truncated_at,
227+
// });
228+
// }
229+
// }
228230

229231
// if (type === 'member.added' || type === 'member.updated') {
230232
// const member = event.member;

package/src/store/OfflineDB.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export class OfflineDB extends AbstractOfflineDB {
6868

6969
deleteChannel = api.deleteChannel;
7070

71+
deleteMessagesForChannel = api.deleteMessagesForChannel;
72+
7173
hardDeleteMessage = api.deleteMessage;
7274

7375
softDeleteMessage = api.softDeleteMessage;

package/src/store/apis/deleteMessagesForChannel.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
import { createDeleteQuery } from '../sqlite-utils/createDeleteQuery';
21
import { SqliteClient } from '../SqliteClient';
32

43
export const deleteMessagesForChannel = async ({
54
cid,
5+
truncated_at,
66
flush = true,
77
}: {
88
cid: string;
9+
truncated_at?: string;
910
flush?: boolean;
1011
}) => {
11-
const query = createDeleteQuery('messages', {
12-
cid,
13-
});
12+
const timestamp = truncated_at ? new Date(truncated_at).toISOString() : new Date().toISOString();
13+
const query: [string, (string | number)[]] = [
14+
`DELETE FROM messages WHERE cid = ? AND createdAt <= ?`,
15+
[cid, timestamp],
16+
];
1417

1518
SqliteClient.logger?.('info', 'deleteMessagesForChannel', {
1619
cid,
1720
flush,
21+
truncated_at,
1822
});
1923

2024
if (flush) {

package/src/store/apis/queries/selectMessagesForChannels.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const selectMessagesForChannels = async (
3030
*,
3131
ROW_NUMBER() OVER (
3232
PARTITION BY cid
33-
ORDER BY cast(strftime('%s', createdAt) AS INTEGER) DESC
33+
ORDER BY createdAt DESC
3434
) RowNum
3535
FROM messages
3636
WHERE cid in (${questionMarks})
@@ -39,7 +39,7 @@ export const selectMessagesForChannels = async (
3939
users b
4040
ON b.id = a.userId
4141
WHERE RowNum < 200
42-
ORDER BY cast(strftime('%s', a.createdAt) AS INTEGER) ASC`,
42+
ORDER BY a.createdAt ASC`,
4343
cids,
4444
);
4545

0 commit comments

Comments
 (0)